转自:https://blog.csdn.net/u013089490/article/details/84318293

1、基本查询

【基本查询语法】

  1. GET /索引库名/_search
  2. {
  3. "query":{
  4. "查询类型":{
  5. "查询条件":"查询条件值"
  6. }
  7. }
  8. }

上面语句中的query表示一个查询对象,可以有不同的查询属性:
(1)查询类型,如:match_all、match、trem、range等等;
(2)查询条件。

1.1、查询所有match_all

【查询所有语句】

  1. GET my_index/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. }
  6. }

【查询所有的结果说明】

  1. #查询结果
  2. {
  3. "took": 2,
  4. "timed_out": false,
  5. "_shards": {
  6. "total": 5,
  7. "successful": 5,
  8. "skipped": 0,
  9. "failed": 0
  10. },
  11. "hits": {
  12. "total": 3,
  13. "max_score": 1,
  14. "hits": [
  15. {
  16. "_index": "my_index",
  17. "_type": "_doc",
  18. "_id": "1",
  19. "_score": 1,
  20. "_source": {
  21. "city": "New York"
  22. }
  23. },
  24. {
  25. "_index": "my_index",
  26. "_type": "_doc",
  27. "_id": "3",
  28. "_score": 1,
  29. "_source": {
  30. "city": "郑州"
  31. }
  32. },
  33. {
  34. "_index": "my_index",
  35. "_type": "_doc",
  36. "_id": "bd3RNGcBJDFdjua0b6RE",
  37. "_score": 1,
  38. "_source": {
  39. "city": "shanghai"
  40. }
  41. }
  42. ]
  43. }
  44. }

(1)took:查询花费时间,单位是毫秒
(2)time_out:是否超时;
(3)_shards:分片信息
(4)hits:搜索结果总对象

  • total:搜索到的总条数;
  • max_score:所有结果中文档得分最高分;
  • hits:搜索结果文档对象数组,每一个元素就是一条搜索到的文档信息;_index表示索引库,_type表示文档类型都是_doc,_id表示当前文档id,_score表示文档得分,_source表示文档的源数据。

1.2、匹配查询match

【or操作】

  1. GET my_index/_search
  2. {
  3. "query": {
  4. "match": {
  5. "city": "York"
  6. }
  7. }
  8. }

【and操作】

  1. GET my_index/_search
  2. {
  3. "query": {
  4. "match": {
  5. "city": {
  6. "query": "zhengzhou"
  7. , "operator": "and"
  8. }
  9. }
  10. }
  11. }

1.3、多字段查询multi_match

  1. GET my_index/_search
  2. {
  3. "query": {
  4. "multi_match": {
  5. "query": "zhengzhou",
  6. "fields": ["city","country"]
  7. }
  8. }
  9. }

1.4、词条匹配term

term 查询被用于精确 匹配,这些精确值可能是数字、时间、布尔或者那些未分词的字符串。

  1. GET my_index/_search
  2. {
  3. "query": {
  4. "term": {
  5. "city": "zhengzhou"
  6. }
  7. }
  8. }

1.5、多词条匹配查询terms

terms 查询和 term 查询一样,但它允许你指定多值进行匹配。如果这个字段包含了指定值中的任何一个值,那么这个文档满足条件:

  1. GET my_index/_search
  2. {
  3. "query": {
  4. "terms": {
  5. "city": ["zhengzhou", "wuhan" ]
  6. }
  7. }
  8. }

2、过滤结果

  1. 默认情况下,elasticsearch在搜索的结果中,会把文档中保存在_source的所有字段都返回。如果我们只想获取其中的部分字段,我们可以添加**_source**的过滤。

2.1、直接指定字段过滤结果_source

  1. GET my_index/_search
  2. {
  3. "_source": ["city","country"]
  4. }

2.2、指定includes和excludes过滤结果

(1)includes:来指定想要显示的字段。
(2)excludes:来指定不想要显示的字段。

  1. GET my_index/_search
  2. {
  3. "_source":{
  4. "includes": ["city","country"]
  5. }
  6. }
  1. 1. GET my_index/_search
  2. 2. {
  3. 3. "_source":{
  4. 4. "excludes": ["level"]
  5. 5. }
  6. 6. }

【注意】意思两条查询语句结果都是一样的。

3、高级查询

3.1、布尔组合(bool)查询

bool把各种其它查询通过must(与)、must_not(非)、should(或)的方式进行组合。

  1. GET my_index/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": {"match":{"city":"zhengzhou"}},
  6. "must_not": {"match":{"city":"shanghai"}},
  7. "should": {"match":{"city":"changsha"}}
  8. }
  9. }
  10. }

3.2、范围查询(range)

range 查询找出那些落在指定区间内的数字或者时间;

  1. GET my_index/_search
  2. {
  3. "query": {
  4. "range": {
  5. "level": {
  6. "gte": 1,
  7. "lte": 20
  8. }
  9. }
  10. }
  11. }

【注意】
(1)ge操作符:大于;
(2)gte:表示大于等于;
(3)lt:表示小于;
(4)lte:表示小于等于。

3.3、模糊查询(fuzzy)

fuzzy 查询是 term 查询的模糊等价。它允许用户搜索词条与实际词条的拼写出现偏差,但是偏差的编辑距离不得超过2:

  1. 1. GET my_index/_search
  2. 2. {
  3. 3. "query": {
  4. 4. "fuzzy": {
  5. 5. "city": "shangh"
  6. 6. }
  7. 7. }
  8. 8. }

通过fuzziness来指定允许的编辑距离:

  1. 1. GET my_index/_search
  2. 2. {
  3. 3. "query": {
  4. 4. "fuzzy": {
  5. 5. "city": {
  6. 6. "value": "zhengzh"
  7. 7. , "fuzziness": 2
  8. 8. }
  9. 9. }
  10. 10. }
  11. 11. }

4、过滤filter

4.1、条件查询中过滤

所有的查询都会影响到文档的评分及排名。如果我们需要在查询结果中进行过滤,并且不希望过滤条件影响评分,那么就不要把过滤条件作为查询条件来用。而是使用filter方式:

  1. GET my_index/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": [
  6. {"match": {
  7. "city": "zhegnzhou"
  8. }}
  9. ]
  10. , "filter": {
  11. "range": {
  12. "level": {
  13. "gte": 1,
  14. "lte": 20
  15. }
  16. }
  17. }
  18. }
  19. }
  20. }

4.2、无条件查询直接过滤

如果一次查询只有过滤,没有查询条件,不希望进行评分,我们可以使用constant_score取代只有 filter 语句的 bool 查询。在性能上是完全相同的,但对于提高查询简洁性和清晰度有很大帮助。

  1. GET my_index/_search
  2. {
  3. "query": {
  4. "constant_score": {
  5. "filter": {
  6. "range": {
  7. "level": {
  8. "gte": 1,
  9. "lte": 8
  10. }
  11. }
  12. }
  13. }
  14. }
  15. }

5、排序

5.1、单字段排序

  1. GET my_index/_search
  2. {
  3. "query": {
  4. "match": {"city": "wuhan3"}
  5. }
  6. , "sort": [
  7. {
  8. "level": {
  9. "order": "desc"
  10. }
  11. }
  12. ]
  13. }

5.2、多字段排序

想要结合使用 level和 _score(得分) 进行查询,并且匹配的结果首先按照level等级排序,然后按照相关性得分排序:

  1. GET my_index/_search
  2. {
  3. "query": {
  4. "match": {"city": "wuhan3"}
  5. } ,
  6. "sort":
  7. [
  8. { "level": { "order": "desc" } },
  9. { "_score": { "order": "desc" } }
  10. ]
  11. }