1. rest接口
    2. 现在我们已经有一个正常运行的节点(和集群)了,下一步就是要去理解怎样与其通信了。幸运的是,
    3. Elasticsearch提供了非常全面和强大的 REST API,利用这个REST API你可以同你的集群交互。
    4. 下面是利用这个API,可以做的几件事情:
    5. - 检查你的集群、节点和索引的健康状态、和各种统计信息
    6. - 管理你的集群、节点、索引数据和元数据
    7. - 对你的索引进行CRUD(创建、读取、更新和删除)和搜索操作
    8. - 执行高级的查询操作,像是分页、排序、过滤、脚本编写(scripting)、
    9. 小平面刻画(faceting)、聚合(aggregations)和许多其它操作
    10. 集群健康
    11. 让我们以基本的健康检查作为开始,我们可以利用它来查看我们集群的状态。此过程中,
    12. 我们使用curl,当然,你也可以使用任何可以创建HTTP/REST 调用的工具。
    13. 我们假设我们还在我们启动Elasticsearch的节点上并打开另外一个shell窗口。
    14. 要检查集群健康,我们将使用_cat API。需要事先记住的是,我们的节点HTTP的端口是9200
    15. curl 'localhost:9200/_cat/health?v'
    16. 相应的响应是:
    17. epoch timestamp cluster status node.total node.data shards pri relo init unassign
    18. 1394735289 14:28:09 elasticsearch green 1 1 0 0 0 0 0
    19. 可以看到,我们集群的名字是“elasticsearch”,正常运行,并且状态是绿色。
    20. 当我们询问集群状态的时候,我们要么得到绿色、黄色或红色。绿色代表一切正常(集群功能齐全),
    21. 黄色意味着所有的数据都是可用的,但是某些复制没有被分配 (集群功能齐全),红色则代表因为某些
    22. 原因,某些数据不可用。注意,即使是集群状态是红色的,集群仍然是部分可用的(它仍然会利用可用的
    23. 分片来响应搜索请 求),但是可能你需要尽快修复它,因为你有丢失的数据。
    24. 也是从上面的响应中,我们可以看到,一共有一个节点,由于里面没有数据,我们有0个分片。
    25. 注意,由于我们使用默认的集群名字 elasticsearch),并且由于Elasticsearch默认使用网络多
    26. 播(multicast)发现其它节点,如果你在你的网络中启动了多个节点,你就已经把她们加入到一个集
    27. 群中了。在这种情形下,你可能在上面的响应中看到多个节点。
    28. 我们也可以获得节集群中的节点列表:
    29. curl 'localhost:9200/_cat/nodes?v'
    30. 对应的响应是:
    31. curl 'localhost:9200/_cat/nodes?v'
    32. host ip heap.percent ram.percent load node.role master name
    33. mwubuntu1 127.0.1.1 8 4 0.00 d * New Goblin
    34. 这儿,我们可以看到我们叫做“New Goblin”的节点,这个节点是我们集群中的唯一节点。
    35. 列出所有的索引
    36. 让我们看一下我们的索引:
    37. curl 'localhost:9200/_cat/indices?v'
    38. 响应是:
    39. curl 'localhost:9200/_cat/indices?v'
    40. health index pri rep docs.count docs.deleted store.size pri.store.size
    41. 这个结果意味着,在我们的集群中,我们没有任何索引。
    42. 创建一个索引
    43. 现在让我们创建一个叫做“customer”的索引,然后再列出所有的索引:
    44. curl -XPUT 'localhost:9200/customer?pretty'
    45. curl 'localhost:9200/_cat/indices?v'
    46. 第一个命令使用PUT创建了一个叫做“customer”的索引。我们简单地将pretty附加到调用的尾部,使其
    47. 以美观的形式打印出JSON响应(如果有的话)。
    48. 响应如下:
    49. curl -XPUT 'localhost:9200/customer?pretty'
    50. {
    51. "acknowledged" : true
    52. }
    53. curl 'localhost:9200/_cat/indices?v'
    54. health index pri rep docs.count docs.deleted store.size pri.store.size
    55. yellow customer 5 1 0 0 495b 495b
    56. 第二个命令的结果告知我们,我们现在有一个叫做customer的索引,并且它有5个主分片和1份复制(都
    57. 是默认值),其中包含0个文档。
    58. 你可能也注意到了这个customer索引有一个黄色健康标签。回顾我们之前的讨论,黄色意味着某些复制
    59. 没有(或者还未)被分配。这个索引之所以这样,是 因为Elasticsearch默认为这个索引创建一份复制。
    60. 由于现在我们只有一个节点在运行,那一份复制就分配不了了(为了高可用),直到当另外一个节 点加
    61. 入到这个集群后,才能分配。一旦那份复制在第二个节点上被复制,这个节点的健康状态就会变成绿色。

    索引并查询一个文档
    现在让我们放一些东西到customer索引中。首先要知道的是,为了索引一个文档,我们必须告诉Elasticsearch这个文档要到这个索引的哪个类型(type)下。

    1. 让我们将一个简单的客户文档索引到customer索引、“external”类型中,这个文档的ID1,操作如下:
    2. curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '
    3. {
    4. "name": "John Doe"
    5. }'
    6. 响应如下:
    7. curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '
    8. {
    9. "name": "John Doe"
    10. }'
    11. {
    12. "_index" : "customer",
    13. "_type" : "external",
    14. "_id" : "1",
    15. "_version" : 1,
    16. "created" : true
    17. }
    18. 从上面的响应中,我们可以看到,一个新的客户文档在customer索引和external类型中被成功创建。
    19. 文档也有一个内部id 1 这个id是我们在索引的时候指定的。

    注意:若出现以下错误

    1. {
    2. "error" : "Content-Type header [application/x-www-form-urlencoded] is not supported",
    3. "status" : 406
    4. }

    此原因时由于ES增加了安全机制,
    进行严格的内容类型检查,严格检查内容类型也可以作为防止跨站点请求伪造攻击的一层保护。
    官网解释
    Strict checking of content-type is also useful as a layer of protection against Cross Site Request Forgery attacks.

    Because the Elasticsearch REST API uses simple HTTP requests, what’s easy to do with curl, is often easy to do with your web browser. If your internal network allows it, you can point your favourite browser at the /_cluster/settings endpoint on one of your Elasticsearch nodes and see the settings for your cluster.

    Unfortunately, if an attacker has the right knowledge about your internal network and Elasticsearch cluster, they can craft a malicious webpage that would use that same technique to perform unwanted updates to your cluster. Web browsers implement a number of security policies that help protect from such attacks, and part of that protection is to place limits on the content-types that may be used when sending data to remote servers.

    I mentioned earlier that you can enable strict content-type checking in recent releases of Elasticsearch 5 by enabling the http.content_type.required configuration option. Given the security reasons mentioned above, you should consider whether that is something that would be of value to you right now.

    If you’re deploying a brand new Elasticsearch cluster, it’s probably a good idea to require strict content-types from the start. It will be one less thing to worry about when you do upgrade to 6.x, and it gives you an added layer of protection against Cross Site Request Forgery attacks.

    If you have an existing Elasticsearch installation, then turning on that setting may be a little trickier - you need to know that all of your clients are sending the correct content-type. But if you can tackle that problem now that will get you one step closer to being able to migrate to Elasticsearch 6 when it is officially available.
    添加请求头即可正常查询

    1. curl -H "Content-Type: application/json" -XPUT 'localhost:9200/customer/external/1?pretty' -d '{"name":"John Doe"}'
    1. 有一个关键点需要注意,Elasticsearch在你想将文档索引到某个索引的时候,并不强制要求这个索引被显
    2. 式地创建。在前面这个例子中,如果customer索引不存在,Elasticsearch将会自动地创建这个索引。
    3. 现在,让我们把刚刚索引的文档取出来:
    4. curl -XGET 'localhost:9200/customer/external/1?pretty'
    5. 响应如下:
    6. curl -XGET 'localhost:9200/customer/external/1?pretty'
    7. {
    8. "_index" : "customer",
    9. "_type" : "external",
    10. "_id" : "1",
    11. "_version" : 1,
    12. "found" : true, "_source" : { "name": "John Doe" }
    13. }
    14. 除了一个叫做found的字段来指明我们找到了一个ID1的文档,和另外一个字段——_source——返回我们前一步
    15. 中索引的完整JSON文档之外,其它的都没有什么特别之处。

    删除一个文档

    1. 现在让我们删除我们刚刚创建的索引,并再次列出所有的索引:
    2. curl -XDELETE 'localhost:9200/customer?pretty'
    3. curl 'localhost:9200/_cat/indices?v'
    4. 响应如下:
    5. curl -XDELETE 'localhost:9200/customer?pretty'
    6. {
    7. "acknowledged" : true
    8. }
    9. curl 'localhost:9200/_cat/indices?v'
    10. health index pri rep docs.count docs.deleted store.size pri.store.size
    11. 这表明我们成功地删除了这个索引,现在我们回到了集群中空无所有的状态。
    12. 在更进一步之前,我们再细看一下一些我们学过的API命令:
    13. curl -XPUT 'localhost:9200/customer'
    14. curl -XPUT 'localhost:9200/customer/external/1' -d '
    15. {
    16. "name": "John Doe"
    17. }'
    18. curl 'localhost:9200/customer/external/1'
    19. curl -XDELETE 'localhost:9200/customer'
    20. 如果我们仔细研究以上的命令,我们可以发现访问Elasticsearch中数据的一个模式。这个模式可以被总结为:
    21. curl - ://
    22. 这个REST访问模式普遍适用于所有的API命令,如果你能记住它,你就会为掌握Elasticsearch开一个好头。

    修改你的数据

    1. Elasticsearch提供了近乎实时的数据操作和搜索功能。默认情况下,从你索引/更新/删除你的数据动作开始
    2. 到它出现在你的搜索结果中,大概会有1秒钟的延迟。这和其它类似SQL的平台不同,数据在一个事务完成之后
    3. 就会立即可用。
    4. 索引/替换文档
    5. 我们先前看到,怎样索引一个文档。现在我们再次调用那个命令:
    6. curl -H "Content-Type: application/json" -XPUT 'localhost:9200/customer/external/1?pretty' -d '
    7. {
    8. "name": "John Doe"
    9. }'
    10. 再次,以上的命令将会把这个文档索引到customer索引、external类型中,其ID1。如果我们对一个
    11. 不同(或相同)的文档应用以上的命令,Elasticsearch将会用一个新的文档来替换(重新索引)当前
    12. ID1的那个文档。
    13. curl -H "Content-Type: application/json" -XPUT 'localhost:9200/customer/external/1?pretty' -d '
    14. {
    15. "name": "Jane Doe"
    16. }'
    17. 以上的命令将ID1的文档的name字段的值从“John Doe”改成了“Jane Doe”。如果我们使用一个不同的
    18. ID,一个新的文档将会被索引,当前已经在索引中的文档不会受到影响。
    19. curl -H "Content-Type: application/json" -XPUT 'localhost:9200/customer/external/2?pretty' -d '
    20. {
    21. "name": "Jane Doe"
    22. }'
    23. 以上的命令,将会索引一个ID2的新文档。
    24. 在索引的时候,ID部分是可选的。如果不指定,Elasticsearch将产生一个随机的ID来索引这个文档。
    25. Elasticsearch生成的ID会作为索引API调用的一部分被返回。
    26. 以下的例子展示了怎样在没有指定ID的情况下来索引一个文档:
    27. curl -H "Content-Type: application/json" -XPOST 'localhost:9200/customer/external?pretty' -d '
    28. {
    29. "name": "Jane Doe"
    30. }'
    31. 注意,在上面的情形中,由于我们没有指定一个ID,我们使用的是POST而不是PUT

    更新文档

    1. 除了可以索引、替换文档之外,我们也可以更新一个文档。但要注意,Elasticsearch底层并不支持原地更新。
    2. 在我们想要做一次更新的时候,Elasticsearch先删除旧文档,然后在索引一个更新过的新文档。
    3. 下面的例子展示了怎样将我们ID1的文档的name字段改成“Jane Doe”:
    4. curl -H "Content-Type: application/json" -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '
    5. {
    6. "doc": { "name": "Jane Doe" }
    7. }'
    8. 下面的例子展示了怎样将我们ID1的文档的name字段改成“Jane Doe”的同时,给它加上age字段:
    9. curl -H "Content-Type: application/json" -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '
    10. {
    11. "doc": { "name": "Jane Doe", "age": 20 }
    12. }'
    13. 更新也可以通过使用简单的脚本来进行。这个例子使用一个脚本将age5
    14. curl -H "Content-Type: application/json" -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '
    15. {
    16. "script" : "ctx._source.age += 5"
    17. }'
    18. 在上面的例子中,ctx._source指向当前要被更新的文档。
    19. 注意,在写作本文时,更新操作只能一次应用在一个文档上。将来,Elasticsearch将提供同时更新符合
    20. 指定查询条件的多个文档的功能(类似于SQLUPDATE-WHERE语句)。

    删除文档

    1. 删除文档是相当直观的。以下的例子展示了我们怎样删除ID2的文档:
    2. curl -XDELETE 'localhost:9200/customer/external/2?pretty'
    3. 我们也能够一次删除符合某个查询条件的多个文档。以下的例子展示了如何删除名字中包含“John”的所有的
    4. 客户:
    5. curl -H "Content-Type: application/json" -XDELETE 'localhost:9200/customer/external/_query?pretty' -d '
    6. {
    7. "query": { "match": { "name": "John" } }
    8. }'
    9. 注意,以上的URI变成了/_query,以此来表明这是一个“查询删除”API,其中删除查询标准放在请求体中,
    10. 但是我们仍然使用DELETE。现在先不要担心查询语法,我们将会在本教程后面的部分中涉及。

    批处理:

    1. 除了能够对单个的文档进行索引、更新和删除之外,Elasticsearch也提供了以上操作的批量处理功能,
    2. 这是通过使用_bulk API实现的。这个功能之所以重要,在于它提供了非常高效的机制来尽可能快的完成
    3. 多个操作,与此同时使用尽可能少的网络往返。
    4. 作为一个快速的例子,以下调用在一次bulk操作中索引了
    5. 两个文档(ID 1 - John Doe and ID 2 - Jane Doe):
    6. curl -H "Content-Type: application/json" -XPOST 'localhost:9200/customer/external/_bulk?pretty' -d '
    7. {"index":{"_id":"1"}}
    8. {"name": "John Doe" }
    9. {"index":{"_id":"2"}}
    10. {"name": "Jane Doe" }
    11. '
    12. 以下例子在一个bulk操作中,首先更新第一个文档(ID1),然后删除第二个文档(ID2):
    13. curl -H "Content-Type: application/json" -XPOST 'localhost:9200/customer/external/_bulk?pretty' -d '
    14. {"update":{"_id":"1"}}
    15. {"doc": { "name": "John Doe becomes Jane Doe" } }
    16. {"delete":{"_id":"2"}}
    17. '
    18. 注意上面的delete动作,由于删除动作只需要被删除文档的ID,所以并没有对应的源文档。
    19. bulk API按顺序执行这些动作。如果其中一个动作因为某些原因失败了,将会继续处理它后面的动作。
    20. bulk API返回时,它将提供每个动作的状态(按照同样的顺序),所以你能够看到某个动作成功与否。

    探索你的数据
    dataset.json
    es.py

    1. 样本数据集
    2. 现在我们对于基本的东西已经有了一些感觉,现在让我们尝试使用一些更加贴近现实的数据集。
    3. 我已经准备了一些假想的客户的银行账户信息的JSON文档的样本。文档具有以下的模式(schema):
    4. {
    5. "account_number": 0,
    6. "balance": 16623,
    7. "firstname": "Bradshaw",
    8. "lastname": "Mckenzie",
    9. "age": 29,
    10. "gender": "F",
    11. "address": "244 Columbus Place",
    12. "employer": "Euron",
    13. "email": "bradshawmckenzie@euron.com",
    14. "city": "Hobucken",
    15. "state": "CO"
    16. }
    17. 我是在http://www.json-generator.com/上生成这些数据的。
    18. 载入样本数据
    19. 将其解压到当前目录下,如下,将其加载到我们的集群里:
    20. curl -H "Content-Type: application/json" -XPOST 'localhost:9200/bank/account/_bulk?pretty' --data-binary @accounts.json
    21. 此方法对json文件格式要求非常严格,容易出错误,可以使用python将数据加载到我们的集群中,
    22. 数据集dataset.jsonpython文件es.py在上面
    23. curl 'localhost:9200/_cat/indices?v'
    24. 响应是:
    25. curl 'localhost:9200/_cat/indices?v'
    26. health index pri rep docs.count docs.deleted store.size pri.store.size
    27. yellow bank 5 1 1000 0 424.4kb 424.4kb
    28. 这意味着我们成功批量索引了1000个文档到银行索引中(account类型)。
    29. 搜索API
    30. 现在,让我们以一些简单的搜索来开始。有两种基本的方式来运行搜索:一种是在REST请求的URI中发送
    31. 搜索参数,另一种是将搜索参数发送到REST请求 体中。请求体方法的表达能力更好,并且你可以使用更
    32. 加可读的JSON格式来定义搜索。我们将尝试使用一次请求URI作为例子,但是教程的后面部分,我们将
    33. 仅仅使用请求体方法。
    34. 搜索的REST API可以通过_search端点来访问。下面这个例子返回bank索引中的所有的文档:
    35. curl 'localhost:9200/bank/_search?q=*&pretty'
    36. 我们仔细研究一下这个查询调用。我们在bank索引中搜索(_search端点),并且q=*参数
    37. 指示Elasticsearch去匹配这个索引中所有的文档。pretty参数,和以前一样,仅仅是告
    38. Elasticsearch返回美观的JSON结果。
    39. 以下是响应(部分列出):
    40. curl 'localhost:9200/bank/_search?q=*&pretty'
    41. {
    42. "took" : 63,
    43. "timed_out" : false,
    44. "_shards" : {
    45. "total" : 5,
    46. "successful" : 5,
    47. "failed" : 0
    48. },
    49. "hits" : {
    50. "total" : 1000,
    51. "max_score" : 1.0,
    52. "hits" : [ {
    53. "_index" : "bank",
    54. "_type" : "account",
    55. "_id" : "1",
    56. "_score" : 1.0, "_source" : {"account_number":1,"balance":39225,"firstname":"Amber","lastname":"Duke","age":32,"gender":"M","address":"880 Holmes Lane","employer":"Pyrami","email":"amberduke@pyrami.com","city":"Brogan","state":"IL"}
    57. }, {
    58. "_index" : "bank",
    59. "_type" : "account",
    60. "_id" : "6",
    61. "_score" : 1.0, "_source" : {"account_number":6,"balance":5686,"firstname":"Hattie","lastname":"Bond","age":36,"gender":"M","address":"671 Bristol Street","employer":"Netagy","email":"hattiebond@netagy.com","city":"Dante","state":"TN"}
    62. }, {
    63. "_index" : "bank",
    64. "_type" : "account",
    65. 对于这个响应,我们看到了以下的部分:
    66. - took —— Elasticsearch执行这个搜索的耗时,以毫秒为单位
    67. - timed_out —— 指明这个搜索是否超时
    68. - _shards —— 指出多少个分片被搜索了,同时也指出了成功/失败的被搜索的shards的数量
    69. - hits —— 搜索结果
    70. - hits.total —— 能够匹配我们查询标准的文档的总数目
    71. - hits.hits —— 真正的搜索结果数据(默认只显示前10个文档)
    72. - _scoremax_score —— 现在先忽略这些字段
    73. 使用请求体方法的等价搜索是:
    74. curl -H "Content-Type: application/json" -XPOST 'localhost:9200/bank/_search?pretty' -d '
    75. {
    76. "query": { "match_all": {} }
    77. }'
    78. 这里的不同之处在于,并不是向URI中传递q=*,取而代之的是,我们在_search API的请求体中POST
    79. 一个JSON格式请求体。我们将在下一部分中讨论这个JSON查询。
    80. 响应是:
    81. curl -H "Content-Type: application/json" -XPOST 'localhost:9200/bank/_search?pretty' -d '
    82. {
    83. "query": { "match_all": {} }
    84. }'
    85. {
    86. "took" : 26,
    87. "timed_out" : false,
    88. "_shards" : {
    89. "total" : 5,
    90. "successful" : 5,
    91. "failed" : 0
    92. },
    93. "hits" : {
    94. "total" : 1000,
    95. "max_score" : 1.0,
    96. "hits" : [ {
    97. "_index" : "bank",
    98. "_type" : "account",
    99. "_id" : "1",
    100. "_score" : 1.0, "_source" : {"account_number":1,"balance":39225,"firstname":"Amber","lastname":"Duke","age":32,"gender":"M","address":"880 Holmes Lane","employer":"Pyrami","email":"amberduke@pyrami.com","city":"Brogan","state":"IL"}
    101. }, {
    102. "_index" : "bank",
    103. "_type" : "account",
    104. "_id" : "6",
    105. "_score" : 1.0, "_source" : {"account_number":6,"balance":5686,"firstname":"Hattie","lastname":"Bond","age":36,"gender":"M","address":"671 Bristol Street","employer":"Netagy","email":"hattiebond@netagy.com","city":"Dante","state":"TN"}
    106. }, {
    107. "_index" : "bank",
    108. "_type" : "account",
    109. "_id" : "13",
    110. 有一点需要重点理解一下,一旦你取回了你的搜索结果,Elasticsearch就完成了使命,它不会维护任
    111. 何服务器端的资源或者在你的结果中打开游标。 这是和其它类似SQL的平台的一个鲜明的对比, 在那
    112. 些平台上,你可以在前面先获取你查询结果的一部分,然后如果你想获取结果的剩余部分,你必须继续
    113. 返回服务端去取,这个过程使用一种有状态的服务器端游标技术。

    介绍查询语言

    1. Elasticsearch提供一种JSON风格的特定领域语言,利用它你可以执行查询。这被称为查询DSL。这个查询语
    2. 言相当全面,第一眼看上去可能有些咄咄逼人,但是最好的学习方法就是以几个基础的例子来开始。
    3. 回到我们上一个例子,我们执行了这个查询:
    4. {
    5. "query": { "match_all": {} }
    6. }
    7. 分解以上的这个查询,其中的query部分告诉我查询的定义,match_all部分就是我们想要运行的查询的类型。
    8. match_all查询,就是简单地查询一个指定索引下的所有的文档。
    9. 除了这个query参数之外,我们也可以通过传递其它的参数来影响搜索结果。比如,下面做了一次match_all
    10. 只返回第一个文档:
    11. curl -H "Content-Type:application/json" -XPOST 'localhost:9200/bank/_search?pretty' -d '
    12. {
    13. "query": { "match_all": {} },
    14. "size": 1
    15. }'
    16. 注意,如果没有指定size的值,那么它默认就是10
    17. 下面的例子,做了一次match_all并且返回第11到第20个文档:
    18. curl -H "Content-Type:application/json" -XPOST 'localhost:9200/bank/_search?pretty' -d '
    19. {
    20. "query": { "match_all": {} },
    21. "from": 10,
    22. "size": 10
    23. }'
    24. 其中的from参数(0-based)从哪个文档开始,size参数指明从from参数开始,要返回多少个文档。
    25. 这个特性对于搜索结果分页来说非常有帮助。注意,如果不指定from的值,它默认就是0
    26. 下面这个例子做了一次match_all并且以账户余额降序排序,最后返前十个文档:
    27. curl -H "Content-Type:application/json" -XPOST 'localhost:9200/bank/_search?pretty' -d '
    28. {
    29. "query": { "match_all": {} },
    30. "sort": { "balance": { "order": "desc" } }
    31. }'

    执行搜索

    1. 现在我们已经知道了几个基本的参数,让我们进一步发掘查询语言吧。首先我们看一下返回文档的字段。
    2. 默认情况下,是返回完整的JSON文档的。这可以通过 source来引用(搜索hits中的_sourcei字段)。
    3. 如果我们不想返回完整的源文档,我们可以指定返回的几个字段。
    4. 下面这个例子说明了怎样返回两个字段account_numberbalance(当然,这两个字段都是指_source
    5. 的字段),以下是具体的搜索:
    6. curl -H "Content-Type:application/json" -XPOST 'localhost:9200/bank/_search?pretty' -d '
    7. {
    8. "query": { "match_all": {} },
    9. "_source": ["account_number", "balance"]
    10. }'
    11. 注意到上面的例子仅仅是简化了_source字段。它仍将会返回一个叫做_source的字段,但是仅仅
    12. 包含account_numberbalance来年改革字段。
    13. 如果你有SQL背景,上述查询在概念上有些像SQLSELECT FROM
    14. 现在让我们进入到查询部分。之前,我们看到了match_all查询是怎样匹配到所有的文档的。现在我们介绍
    15. 一种新的查询,叫做match查询,这可以看成是一个简单的字段搜索查询(比如对应于某个或某些特定字段
    16. 的搜索)。
    17. 下面这个例子返回账户编号为20的文档:
    18. curl -H "Content-Type:application/json" -XPOST 'localhost:9200/bank/_search?pretty' -d '
    19. {
    20. "query": { "match": { "account_number": 20 } }
    21. }'
    22. 下面这个例子返回地址中包含“mill”的所有账户:
    23. curl -H "Content-Type:application/json" -XPOST 'localhost:9200/bank/_search?pretty' -d '
    24. {
    25. "query": { "match": { "address": "mill" } }
    26. }'
    27. 下面这个例子返回地址中包含“mill”或者包含“lane”的账户:
    28. curl -H "Content-Type:application/json" -XPOST 'localhost:9200/bank/_search?pretty' -d '
    29. {
    30. "query": { "match": { "address": "mill lane" } }
    31. }'
    32. 下面这个例子是match的变体(match_phrase),它会去匹配短语“mill lane”:
    33. curl -H "Content-Type:application/json" -XPOST 'localhost:9200/bank/_search?pretty' -d '
    34. {
    35. "query": { "match_phrase": { "address": "mill lane" } }
    36. }'
    37. 现在,让我们介绍一下布尔查询。布尔查询允许我们利用布尔逻辑将较小的查询组合成较大的查询。
    38. 现在这个例子组合了两个match查询,这个组合查询返回包含“mill”和“lane”的所有的账户:
    39. curl -H "Content-Type:application/json" -XPOST 'localhost:9200/bank/_search?pretty' -d '
    40. {
    41. "query": {
    42. "bool": {
    43. "must": [
    44. { "match": { "address": "mill" } },
    45. { "match": { "address": "lane" } }
    46. ]
    47. }
    48. }
    49. }'
    50. 在上面的例子中,bool must语句指明了,对于一个文档,所有的查询都必须为真,这个文档才能够匹配成功。
    51. 相反的,下面的例子组合了两个match查询,它返回的是地址中包含“mill”或者“lane”的所有的账户:
    52. curl -H "Content-Type:application/json" -XPOST 'localhost:9200/bank/_search?pretty' -d '
    53. {
    54. "query": {
    55. "bool": {
    56. "should": [
    57. { "match": { "address": "mill" } },
    58. { "match": { "address": "lane" } }
    59. ]
    60. }
    61. }
    62. }'
    63. 在上面的例子中,bool should语句指明,对于一个文档,查询列表中,只要有一个查询匹配,那么这个文档
    64. 就被看成是匹配的。
    65. 现在这个例子组合了两个查询,它返回地址中既不包含“mill”,同时也不包含“lane”的所有的账户信息:
    66. curl -H "Content-Type:application/json" -XPOST 'localhost:9200/bank/_search?pretty' -d '
    67. {
    68. "query": {
    69. "bool": {
    70. "must_not": [
    71. { "match": { "address": "mill" } },
    72. { "match": { "address": "lane" } }
    73. ]
    74. }
    75. }
    76. }'
    77. 在上面的例子中, bool must_not语句指明,对于一个文档,查询列表中的的所有查询都必须都不为真,
    78. 这个文档才被认为是匹配的。
    79. 我们可以在一个bool查询里一起使用mustshouldmust_not。此外,我们可以将bool查询放到这样
    80. bool语句中来模拟复杂的、多等级的布尔逻辑。
    81. 下面这个例子返回40岁并且不生活在IDdaho)的人的账户:
    82. curl -H "Content-Type:application/json" -XPOST 'localhost:9200/bank/_search?pretty' -d '
    83. {
    84. "query": {
    85. "bool": {
    86. "must": [
    87. { "match": { "age": "40" } }
    88. ],
    89. "must_not": [
    90. { "match": { "state": "ID" } }
    91. ]
    92. }
    93. }
    94. }'

    执行过滤器

    1. 在先前的章节中,我们跳过了文档得分的细节(搜索结果中的_score字段)。这个得分是与我们指定的搜索查
    2. 询匹配程度的一个相对度量。得分越高,文档越相关,得分越低文档的相关度越低。
    3. Elasticsearch中的所有的查询都会触发相关度得分的计算。对于那些我们不需要相关度得分的场景下,
    4. Elasticsearch以过滤器的形式 提供了另一种查询功能。过滤器在概念上类似于查询,但是它们有非常快的
    5. 执行速度,这种快的执行速度主要有以下两个原因
    6. - 过滤器不会计算相关度的得分,所以它们在计算上更快一些
    7. - 过滤器可以被缓存到内存中,这使得在重复的搜索查询上,其要比相应的查询快出许多。
    8. 为了理解过滤器,我们先来介绍“被过滤”的查询,这使得你可以将一个查询(像是match_allmatch
    9. bool等)和一个过滤器结合起来。作为一个例子,我们介绍一下范围过滤器,它允许我们通过一个区间的
    10. 值来过滤文档。这通常被用在数字和日期的过滤上。
    11. 这个例子使用一个被过滤的查询,其返回值是越在2000030000之间(闭区间)的账户。换句话说,我们
    12. 想要找到大于等于20000并且小于等于30000的账户。
    13. curl -H "Content-Type:application/json" -XPOST 'localhost:9200/bank/_search?pretty' -d '
    14. {
    15. "query": {
    16. "bool": {
    17. "filter": {
    18. "range": {
    19. "balance": {
    20. "gte": 20000,
    21. "lte": 30000
    22. }
    23. }
    24. }
    25. }
    26. }
    27. }'
    28. 我们可以在查询部分中放入其他查询,在 filter部分放入其它过滤器。在上面的应用场景中,由于所有的在
    29. 这个范围之内的文档都是平等的(或者说相关度都是一样的),没有一个文档比另一个文档 更相关,所以这
    30. 个时候使用范围过滤器就非常合适了。
    31. 通常情况下,要决定是使用过滤器还是使用查询,你就需要问自己是否需要相关度得分。如果相关度是不重
    32. 要的,使用过滤器,否则使用查询。如果你有SQL 景,查询和过滤器在概念上类似于SELECT WHERE语句,
    33. although more so for filters than queries
    34. 除了match_all, match, bool,filteredrange查询,还有很多其它类型的查询过滤器,我们这里不会
    35. 涉及。由于我们已经对它们的工作原理有了基本的理解,将其应用到其它类型的查询、过滤器上也不是件难
    36. 事。

    执行聚合

    1. 聚合提供了分组并统计数据的能力。理解聚合的最简单的方式是将其粗略地等同为SQLGROUP BYSQL聚合
    2. 函数。在Elasticsearch中,你可以在一个响应中同时返回命中的数据和聚合结果。你可以使用简单的API
    3. 时运行查询和多个聚合,并以一次返回,这避免了来回的网络通信,这是非常强大和高效的。
    4. 作为开始的一个例子,我们按照state分组,按照州名的计数倒序排序:
    5. curl -H "Content-Type:application/json" -XPOST 'localhost:9200/bank/_search?pretty' -d '
    6. {
    7. "size": 0,
    8. "aggs": {
    9. "group_by_state": {
    10. "terms": {
    11. "field": "state"
    12. }
    13. }
    14. }
    15. }'
    16. SQL中,上面的聚合在概念上类似于:
    17. SELECT COUNT(*) from bank GROUP BY state ORDER BY COUNT(*) DESC
    18. 响应(其中一部分)是:
    19. "hits" : {
    20. "total" : 1000,
    21. "max_score" : 0.0,
    22. "hits" : [ ]
    23. },
    24. "aggregations" : {
    25. "group_by_state" : {
    26. "buckets" : [ {
    27. "key" : "al",
    28. "doc_count" : 21
    29. }, {
    30. "key" : "tx",
    31. "doc_count" : 17
    32. }, {
    33. "key" : "id",
    34. "doc_count" : 15
    35. }, {
    36. "key" : "ma",
    37. "doc_count" : 15
    38. }, {
    39. "key" : "md",
    40. "doc_count" : 15
    41. }, {
    42. "key" : "pa",
    43. "doc_count" : 15
    44. }, {
    45. "key" : "dc",
    46. "doc_count" : 14
    47. }, {
    48. "key" : "me",
    49. "doc_count" : 14
    50. }, {
    51. "key" : "mo",
    52. "doc_count" : 14
    53. }, {
    54. "key" : "nd",
    55. "doc_count" : 14
    56. } ]
    57. }
    58. }
    59. }
    60. 我们可以看到ALabama)有21个账户,TX17个账户,IDdaho)有15个账户,依此类推。
    61. 注意我们将size设置成0,这样我们就可以只看到聚合结果了,而不会显示命中的结果。
    62. 在先前聚合的基础上,现在这个例子计算了每个州的账户的平均余额(还是按照账户数量倒序排序的
    63. 10个州):
    64. curl -H "Content-Type:application/json" -XPOST 'localhost:9200/bank/_search?pretty' -d '
    65. {
    66. "size": 0,
    67. "aggs": {
    68. "group_by_state": {
    69. "terms": {
    70. "field": "state"
    71. },
    72. "aggs": {
    73. "average_balance": {
    74. "avg": {
    75. "field": "balance"
    76. }
    77. }
    78. }
    79. }
    80. }
    81. }'
    82. 注意,我们把average_balance聚合嵌套在了group_by_state聚合之中。这是所有聚合的一个常用模式。你可以任意的聚合之中嵌套聚合,这样你就可以从你的数据中抽取出想要的概述。
    83. 基于前面的聚合,现在让我们按照平均余额进行排序:
    84. curl -H "Content-Type:application/json" -XPOST 'localhost:9200/bank/_search?pretty' -d '
    85. {
    86. "size": 0,
    87. "aggs": {
    88. "group_by_state": {
    89. "terms": {
    90. "field": "state",
    91. "order": {
    92. "average_balance": "desc"
    93. }
    94. },
    95. "aggs": {
    96. "average_balance": {
    97. "avg": {
    98. "field": "balance"
    99. }
    100. }
    101. }
    102. }
    103. }
    104. }'
    105. 下面的例子显示了如何使用年龄段(20-2930-3940-49)分组,然后在用性别分组,然后为每一个年龄段的每一个性别计算平均账户余额:
    106. curl -H "Content-Type:application/json" -XPOST 'localhost:9200/bank/_search?pretty' -d '
    107. {
    108. "size": 0,
    109. "aggs": {
    110. "group_by_age": {
    111. "range": {
    112. "field": "age",
    113. "ranges": [
    114. {
    115. "from": 20,
    116. "to": 30
    117. },
    118. {
    119. "from": 30,
    120. "to": 40
    121. },
    122. {
    123. "from": 40,
    124. "to": 50
    125. }
    126. ]
    127. },
    128. "aggs": {
    129. "group_by_gender": {
    130. "terms": {
    131. "field": "gender"
    132. },
    133. "aggs": {
    134. "average_balance": {
    135. "avg": {
    136. "field": "balance"
    137. }
    138. }
    139. }
    140. }
    141. }
    142. }
    143. }
    144. }'
    145. 有很多关于聚合的细节,我们没有涉及。如果你想做更进一步的实验,http://www.elasticsearch.org/guide/en /elasticsearch/reference/current/search-aggregations.html是一个非常好的起点。

    总结

    1. Elasticsearch既是一个简单的产品,也是一个复杂的产品。我们现在已经学习到了基础部分,它的一些原理,
    2. 以及怎样用REST API来做一些工作。我希望这个教程已经使你对Elasticsearch是什么有了一个更好的理解,
    3. 跟重要的是,能够激发你继续实验 Elasticsearch的其它特性。