Elasticsearch基本概念和使用

- 日理万妓 2022-09-16 06:11 317阅读 0赞

一、基本概念

Node 与 Cluster

  1. Elastic 本质上是一个分布式数据库,允许多台服务器协同工作,每台服务器可以运行多个 Elastic 实例。
  2. 单个 Elastic 实例称为一个节点(node)。一组节点构成一个集群(cluster)。

Index

  1. Elastic 会索引所有字段,经过处理后写入一个反向索引(Inverted Index)。查找数据的时候,直接查找该索引。
  2. 所以,Elastic 数据管理的顶层单位就叫做 Index(索引)。它是单个数据库的同义词。每个 Index (即数据库)的名字必须是小写。
  3. 下面的命令可以查看当前节点的所有 Index``
  4. curl -X GET 'http://localhost:9200/_cat/indices?v'

Document

  1. Index 里面单条的记录称为 Document(文档)。许多条 Document 构成了一个 Index
  2. Document 使用 JSON 格式表示,下面是一个例子。
  3. {
  4. "user": "张三",
  5. "title": "工程师",
  6. "desc": "数据库管理"
  7. }
  8. 同一个 Index 里面的 Document,不要求有相同的结构(scheme),但是最好保持相同,这样有利于提高搜索效率。

2.4 Type

  1. Document 可以分组,比如`weather`这个 Index 里面,可以按城市分组(北京和上海),也可以按气候分组(晴天和雨天)。这种分组就叫做 Type,它是虚拟的逻辑分组,用来过滤 Document
  2. 不同的 Type 应该有相似的结构(schema),举例来说,`id`字段不能在这个组是字符串,在另一个组是数值。这是与关系型数据库的表的[一个区别][Link 1]。性质完全不同的数据(比如`products``logs`)应该存成两个 Index,而不是一个 Index 里面的两个 Type(虽然可以做到)。
  3. 下面的命令可以列出每个 Index 所包含的 Type
  4. $ curl 'localhost:9200/_mapping?pretty=true'
  5. 根据[规划][Link 2],Elastic 6.x 版只允许每个 Index 包含一个 Type7.x 版将会彻底移除 Type

二、新建和删除Index

  1. 新建 Index,可以直接向 Elastic 服务器发出 PUT 请求。下面的例子是新建一个名叫`weather` Index
  2. $ curl -X PUT 'localhost:9200/weather'
  3. 服务器返回一个 JSON 对象,里面的`acknowledged`字段表示操作成功。
  4. {
  5. "acknowledged":true,
  6. "shards_acknowledged":true
  7. }
  8. 然后,我们发出 DELETE 请求,删除这个 Index
  9. $ curl -X DELETE 'localhost:9200/weather'

三、中文分词设置

  1. 首先,安装中文分词插件。这里使用的是 [ik][],也可以考虑其他插件(比如 [smartcn][])。[Releases · medcl/elasticsearch-analysis-ik · GitHub][Releases _ medcl_elasticsearch-analysis-ik _ GitHub]。
  2. ./elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.15.1/elasticsearch-analysis-ik-7.15.1.zip
  3. 重新启动 Elastic,就会自动加载这个新安装的插件。然后,新建一个 Index,指定需要分词的字段。这一步根据数据结构而异,下面的命令只针对本文。基本上,凡是需要搜索的中文字段,都要单独设置一下。
  4. $ curl -X PUT 'localhost:9200/accounts' -d '
  5. {
  6. "mappings": {
  7. "person": {
  8. "properties": {
  9. "user": {
  10. "type": "text",
  11. "analyzer": "ik_max_word",
  12. "search_analyzer": "ik_max_word"
  13. },
  14. "title": {
  15. "type": "text",
  16. "analyzer": "ik_max_word",
  17. "search_analyzer": "ik_max_word"
  18. },
  19. "desc": {
  20. "type": "text",
  21. "analyzer": "ik_max_word",
  22. "search_analyzer": "ik_max_word"
  23. }
  24. }
  25. }
  26. }
  27. }
  28. 上面代码中,首先新建一个名称为`accounts`的 Index,里面有一个名称为`person`的 Type。`person`有三个字段。
  • user
  • title
  • desc

    1. 这三个字段都是中文,而且类型都是文本(text),所以需要指定中文分词器,不能使用默认的英文分词器。Elastic 的分词器称为 [analyzer][]。我们对每个字段指定分词器。

    “user”: {
    “type”: “text”,
    “analyzer”: “ik_max_word”,
    “search_analyzer”: “ik_max_word”
    }

    1. 上面代码中,`analyzer`是字段文本的分词器,`search_analyzer`是搜索词的分词器。`ik_max_word`分词器是插件`ik`提供的,可以对文本进行最大数量的分词。

四、数据操作

新增记录

  1. 向指定的 /Index/Type 发送 PUT 请求,就可以在 Index 里面新增一条记录。比如,向`/accounts/person`发送请求,就可以新增一条人员记录。
  2. $ curl -X PUT 'localhost:9200/accounts/person/1' -d '
  3. {
  4. "user": "张三",
  5. "title": "工程师",
  6. "desc": "数据库管理"
  7. }'
  8. 服务器返回的 JSON 对象,会给出 IndexTypeIdVersion 等信息。
  9. {
  10. "_index":"accounts",
  11. "_type":"person",
  12. "_id":"1",
  13. "_version":1,
  14. "result":"created",
  15. "_shards":{"total":2,"successful":1,"failed":0},
  16. "created":true
  17. }
  18. 如果你仔细看,会发现请求路径是`/accounts/person/1`,最后的`1`是该条记录的 Id。它不一定是数字,任意字符串(比如`abc`)都可以。
  19. 新增记录的时候,也可以不指定 Id,这时要改成 POST 请求。
  20. $ curl -X POST 'localhost:9200/accounts/person' -d '
  21. {
  22. "user": "李四",
  23. "title": "工程师",
  24. "desc": "系统管理"
  25. }'
  26. 上面代码中,向`/accounts/person`发出一个 POST 请求,添加一个记录。这时,服务器返回的 JSON 对象里面,`_id`字段就是一个随机字符串。
  27. {
  28. "_index":"accounts",
  29. "_type":"person",
  30. "_id":"AV3qGfrC6jMbsbXb6k1p",
  31. "_version":1,
  32. "result":"created",
  33. "_shards":{"total":2,"successful":1,"failed":0},
  34. "created":true
  35. }
  36. 注意,如果没有先创建 Index(这个例子是`accounts`),直接执行上面的命令,Elastic 也不会报错,而是直接生成指定的 Index。所以,打字的时候要小心,不要写错 Index 的名称。

查看记录

  1. `/Index/Type/Id`发出 GET 请求,就可以查看这条记录。
  2. $ curl 'localhost:9200/accounts/person/1?pretty=true'
  3. 上面代码请求查看`/accounts/person/1`这条记录,URL 的参数`pretty=true`表示以易读的格式返回。
  4. 返回的数据中,`found`字段表示查询成功,`_source`字段返回原始记录。
  5. {
  6. "_index" : "accounts",
  7. "_type" : "person",
  8. "_id" : "1",
  9. "_version" : 1,
  10. "found" : true,
  11. "_source" : {
  12. "user" : "张三",
  13. "title" : "工程师",
  14. "desc" : "数据库管理"
  15. }
  16. }
  17. 如果 Id 不正确,就查不到数据,`found`字段就是`false`
  18. $ curl 'localhost:9200/weather/beijing/abc?pretty=true'
  19. {
  20. "_index" : "accounts",
  21. "_type" : "person",
  22. "_id" : "abc",
  23. "found" : false
  24. }

删除记录

  1. 删除记录就是发出 DELETE 请求。
  2. $ curl -X DELETE 'localhost:9200/accounts/person/1'
  3. 这里先不要删除这条记录,后面还要用到。

更新记录

  1. 更新记录就是使用 PUT 请求,重新发送一次数据。
  2. $ curl -X PUT 'localhost:9200/accounts/person/1' -d '
  3. {
  4. "user" : "张三",
  5. "title" : "工程师",
  6. "desc" : "数据库管理,软件开发"
  7. }'
  8. {
  9. "_index":"accounts",
  10. "_type":"person",
  11. "_id":"1",
  12. "_version":2,
  13. "result":"updated",
  14. "_shards":{"total":2,"successful":1,"failed":0},
  15. "created":false
  16. }
  17. 上面代码中,我们将原始数据从"数据库管理"改成"数据库管理,软件开发" 返回结果里面,有几个字段发生了变化。
  18. "_version" : 2,
  19. "result" : "updated",
  20. "created" : false
  21. 可以看到,记录的 Id 没变,但是版本(version)从`1`变成`2`,操作类型(result)从`created`变成`updated``created`字段变成`false`,因为这次不是新建记录。

五、数据查询

返回所有记录

  1. 使用 GET 方法,直接请求`/Index/Type/_search`,就会返回所有记录。
  2. $ curl 'localhost:9200/accounts/person/_search'
  3. {
  4. "took":2,
  5. "timed_out":false,
  6. "_shards":{"total":5,"successful":5,"failed":0},
  7. "hits":{
  8. "total":2,
  9. "max_score":1.0,
  10. "hits":[
  11. {
  12. "_index":"accounts",
  13. "_type":"person",
  14. "_id":"AV3qGfrC6jMbsbXb6k1p",
  15. "_score":1.0,
  16. "_source": {
  17. "user": "李四",
  18. "title": "工程师",
  19. "desc": "系统管理"
  20. }
  21. },
  22. {
  23. "_index":"accounts",
  24. "_type":"person",
  25. "_id":"1",
  26. "_score":1.0,
  27. "_source": {
  28. "user" : "张三",
  29. "title" : "工程师",
  30. "desc" : "数据库管理,软件开发"
  31. }
  32. }
  33. ]
  34. }
  35. }
  36. 上面代码中,返回结果的 `took`字段表示该操作的耗时(单位为毫秒),`timed_out`字段表示是否超时,`hits`字段表示命中的记录,里面子字段的含义如下。
  • total:返回记录数,本例是2条。
  • max_score:最高的匹配程度,本例是1.0
  • hits:返回的记录组成的数组。

    1. 返回的记录中,每条记录都有一个`_score`字段,表示匹配的程序,默认是按照这个字段降序排列。

全文搜索

  1. Elastic 的查询非常特别,使用自己的[查询语法][Link 3],要求 GET 请求带有数据体。
  2. $ curl 'localhost:9200/accounts/person/_search' -d '
  3. {
  4. "query" : { "match" : { "desc" : "软件" }}
  5. }'
  6. 上面代码使用 [Match 查询][Match],指定的匹配条件是`desc`字段里面包含"软件"这个词。返回结果如下。
  7. {
  8. "took":3,
  9. "timed_out":false,
  10. "_shards":{"total":5,"successful":5,"failed":0},
  11. "hits":{
  12. "total":1,
  13. "max_score":0.28582606,
  14. "hits":[
  15. {
  16. "_index":"accounts",
  17. "_type":"person",
  18. "_id":"1",
  19. "_score":0.28582606,
  20. "_source": {
  21. "user" : "张三",
  22. "title" : "工程师",
  23. "desc" : "数据库管理,软件开发"
  24. }
  25. }
  26. ]
  27. }
  28. }
  29. Elastic 默认一次返回10条结果,可以通过`size`字段改变这个设置。
  30. $ curl 'localhost:9200/accounts/person/_search' -d '
  31. {
  32. "query" : { "match" : { "desc" : "管理" }},
  33. "size": 1
  34. }'
  35. 上面代码指定,每次只返回一条结果。还可以通过`from`字段,指定位移。
  36. $ curl 'localhost:9200/accounts/person/_search' -d '
  37. {
  38. "query" : { "match" : { "desc" : "管理" }},
  39. "from": 1,
  40. "size": 1
  41. }'
  42. 上面代码指定,从位置1开始(默认是从位置0开始),只返回一条结果。

逻辑运算

  1. 如果有多个搜索关键字, Elastic 认为它们是`or`关系。
  2. $ curl 'localhost:9200/accounts/person/_search' -d '
  3. {
  4. "query" : { "match" : { "desc" : "软件 系统" }}
  5. }'
  6. 上面代码搜索的是`软件 or 系统`。如果要执行多个关键词的`and`搜索,必须使用[布尔查询][Link 4]。
  7. $ curl 'localhost:9200/accounts/person/_search' -d '
  8. {
  9. "query": {
  10. "bool": {
  11. "must": [
  12. { "match": { "desc": "软件" } },
  13. { "match": { "desc": "系统" } }
  14. ]
  15. }
  16. }
  17. }'

发表评论

表情:
评论列表 (有 0 条评论,317人围观)

还没有评论,来说两句吧...

相关阅读