转自:https://blog.csdn.net/u013089490/article/details/84318293
1、基本查询
【基本查询语法】
GET /索引库名/_search{"query":{"查询类型":{"查询条件":"查询条件值"}}}
上面语句中的query表示一个查询对象,可以有不同的查询属性:
(1)查询类型,如:match_all、match、trem、range等等;
(2)查询条件。
1.1、查询所有match_all
【查询所有语句】
GET my_index/_search{"query": {"match_all": {}}}
【查询所有的结果说明】
#查询结果{"took": 2,"timed_out": false,"_shards": {"total": 5,"successful": 5,"skipped": 0,"failed": 0},"hits": {"total": 3,"max_score": 1,"hits": [{"_index": "my_index","_type": "_doc","_id": "1","_score": 1,"_source": {"city": "New York"}},{"_index": "my_index","_type": "_doc","_id": "3","_score": 1,"_source": {"city": "郑州"}},{"_index": "my_index","_type": "_doc","_id": "bd3RNGcBJDFdjua0b6RE","_score": 1,"_source": {"city": "shanghai"}}]}}
(1)took:查询花费时间,单位是毫秒;
(2)time_out:是否超时;
(3)_shards:分片信息
(4)hits:搜索结果总对象
- total:搜索到的总条数;
- max_score:所有结果中文档得分最高分;
- hits:搜索结果文档对象数组,每一个元素就是一条搜索到的文档信息;_index表示索引库,_type表示文档类型都是_doc,_id表示当前文档id,_score表示文档得分,_source表示文档的源数据。
1.2、匹配查询match
【or操作】
GET my_index/_search{"query": {"match": {"city": "York"}}}
【and操作】
GET my_index/_search{"query": {"match": {"city": {"query": "zhengzhou", "operator": "and"}}}}
1.3、多字段查询multi_match
GET my_index/_search{"query": {"multi_match": {"query": "zhengzhou","fields": ["city","country"]}}}
1.4、词条匹配term
term 查询被用于精确 匹配,这些精确值可能是数字、时间、布尔或者那些未分词的字符串。
GET my_index/_search{"query": {"term": {"city": "zhengzhou"}}}
1.5、多词条匹配查询terms
terms 查询和 term 查询一样,但它允许你指定多值进行匹配。如果这个字段包含了指定值中的任何一个值,那么这个文档满足条件:
GET my_index/_search{"query": {"terms": {"city": ["zhengzhou", "wuhan" ]}}}
2、过滤结果
默认情况下,elasticsearch在搜索的结果中,会把文档中保存在_source的所有字段都返回。如果我们只想获取其中的部分字段,我们可以添加**_source**的过滤。
2.1、直接指定字段过滤结果_source
GET my_index/_search{"_source": ["city","country"]}
2.2、指定includes和excludes过滤结果
(1)includes:来指定想要显示的字段。
(2)excludes:来指定不想要显示的字段。
GET my_index/_search{"_source":{"includes": ["city","country"]}}
1. GET my_index/_search2. {3. "_source":{4. "excludes": ["level"]5. }6. }
3、高级查询
3.1、布尔组合(bool)查询
bool把各种其它查询通过must(与)、must_not(非)、should(或)的方式进行组合。
GET my_index/_search{"query": {"bool": {"must": {"match":{"city":"zhengzhou"}},"must_not": {"match":{"city":"shanghai"}},"should": {"match":{"city":"changsha"}}}}}
3.2、范围查询(range)
range 查询找出那些落在指定区间内的数字或者时间;
GET my_index/_search{"query": {"range": {"level": {"gte": 1,"lte": 20}}}}
【注意】
(1)ge操作符:大于;
(2)gte:表示大于等于;
(3)lt:表示小于;
(4)lte:表示小于等于。
3.3、模糊查询(fuzzy)
fuzzy 查询是 term 查询的模糊等价。它允许用户搜索词条与实际词条的拼写出现偏差,但是偏差的编辑距离不得超过2:
1. GET my_index/_search2. {3. "query": {4. "fuzzy": {5. "city": "shangh"6. }7. }8. }
通过fuzziness来指定允许的编辑距离:
1. GET my_index/_search2. {3. "query": {4. "fuzzy": {5. "city": {6. "value": "zhengzh"7. , "fuzziness": 28. }9. }10. }11. }
4、过滤filter
4.1、条件查询中过滤
所有的查询都会影响到文档的评分及排名。如果我们需要在查询结果中进行过滤,并且不希望过滤条件影响评分,那么就不要把过滤条件作为查询条件来用。而是使用filter方式:
GET my_index/_search{"query": {"bool": {"must": [{"match": {"city": "zhegnzhou"}}], "filter": {"range": {"level": {"gte": 1,"lte": 20}}}}}}
4.2、无条件查询直接过滤
如果一次查询只有过滤,没有查询条件,不希望进行评分,我们可以使用constant_score取代只有 filter 语句的 bool 查询。在性能上是完全相同的,但对于提高查询简洁性和清晰度有很大帮助。
GET my_index/_search{"query": {"constant_score": {"filter": {"range": {"level": {"gte": 1,"lte": 8}}}}}}
5、排序
5.1、单字段排序
GET my_index/_search{"query": {"match": {"city": "wuhan3"}}, "sort": [{"level": {"order": "desc"}}]}
5.2、多字段排序
想要结合使用 level和 _score(得分) 进行查询,并且匹配的结果首先按照level等级排序,然后按照相关性得分排序:
GET my_index/_search{"query": {"match": {"city": "wuhan3"}} ,"sort":[{ "level": { "order": "desc" } },{ "_score": { "order": "desc" } }]}
