在公司的日常操作中,仅限于用bool去拼一个filter,其它的东西一无所知,之前的学习也仅仅是了解了三种查询方式【通过id查询、通过term分词查询、通过queryString查询】【ElasticSearch从入门到放弃系列 四】ElasticSearch的基本概念和使用,其实对于ES的查询的理解还停留在了Lucene阶段,对于高级的查询方式并不熟悉,包括在后续的Java API,甚至Spring Data ES的使用中也仅仅是对ES查询做了简单了解,依然是【通过id查询、通过term分词查询、通过queryString查询】老三样,当然可能加了分页和高亮的查询,现在,就在本篇blog做一个全局的了解和实践。
ES查询方式
按照查询条件的多少,从网上找了一些文章,看起来真的是不知所云,分类为所欲为,于是想对整个的ES查询方式做个整理,分为如下几个维度去讨论,防止大家读起来混乱。
| 查询大类 | 查询小类 | 细分关键词 |
| 普通查询 | ID查询【通过ID精确匹配】 | id |
| 语法查询【QueryString查询】 | query_string | |
| DSL查询【 ES领域专用】 | term、terms、much、much_all、match_phrase、muti_match、range、exists、prefix、wildcard、regexp、fuzzy | |
| 复合查询 | Boolean复合查询 | must、should、must_not、filter【四种子句关系类型】 |
| 聚合查询 | 桶聚合 | terms、range |
| 聚合查询 | 指标聚合 | min、max、avg、sum、cardinalit、stats、percentile、 percentile_rank、top hits |
以上所有的查询方式,都支持查询结果的:排序、分页【fromsize+scroll】和高亮查询 这些基本特性,所以拜托各位大佬的文章不要把这些当做分类带进去,很容易误导人。
数据准备
一以贯之,还是拿人员信息来举例吧,不过这次正式玩儿之前,不用之前的5.7版本了,其实已经过去两个大版本了,之前用5.7也是为了和公司的版本保持一致,这回使用最新版的7.9.2版本来操作下,先从官网上下载一个,同时帮助文档也可以看最新Doc-7.9.2版本的:
复制三个实例成为一个集群,启动时需要注意,需要在配置文件里指定master是哪个节点,在5.7版本没有遇到这个问题,新版本的配置可能是必须的了cluster.initial_master_nodes: ["node-1"],集群调整好后我们录入一些数据,因为之后要用聚合查询,包括要使用一些数字查询,这里我们用三个字段标识人员信息:姓名、性别、年龄
然后我提前准备了一组数据,用来做各种各样的查询:
普通查询
普通查询有三种模式,第一种就是ID查询,这个比较好理解,因为ID是唯一标识文档的,所以通过id一定能精确查询;第二种是语法查询,语法查询说白了就是queryString查询,这种查询方式相当于把查询语句先分词,然后再依据各个关键词去检索;第三种可就比较多了,DSL查询,字面意思就是特殊语言查询,也就是只适用于ElasticSearch的查询方式,由于是ES自带专属的,那么种类和方式可就多了。
ID查询
ID查询顾名思义就是通过ID进行查询,例如这里我们想查询ID为9的数据,通过如下的Get查询请求即可:
GET:localhost:9200/tml-userinfo/_doc/9
得到的返回结果为:
{ "_index": "tml-userinfo", "_type": "_doc", "_id": "9", "_version": 1, "_seq_no": 6, "_primary_term": 3, "found": true, "_source": { "age": 18, "sex": "女", "name": "森小玲" } }
语法查询
语法查询相当于我的查询语句会进行分词,那么我们想查询name里带玲和林的数据,通过以下URL查询即可
POST:localhost:9200/tml-userinfo/_doc/_search
查询的body为:
{ "query": { "query_string": { "default_field": "name", "query": "林玲" } } }
请求的返回结果为:
{ "took": 28, "timed_out": false, "_shards": { "total": 3, "successful": 3, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 5, "relation": "eq" }, "max_score": 1.7917595, "hits": [ { "_index": "tml-userinfo", "_type": "_doc", "_id": "6", "_score": 1.7917595, "_source": { "age": 28, "sex": "男", "name": "李小林" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "9", "_score": 1.7917595, "_source": { "age": 18, "sex": "女", "name": "森小玲" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "2", "_score": 1.3862942, "_source": { "age": 28, "sex": "男", "name": "森小林" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "10", "_score": 1.3862942, "_source": { "age": 18, "sex": "女", "name": "李小玲" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "5", "_score": 0.9808291, "_source": { "age": 28, "sex": "男", "name": "李小林" } } ] } }
DSL查询
DSL查询属于ES的专属查询语句,所以查询关键字较多:term、terms、much、much_all、match_phrase、muti_match、range、exists、fuzzy、prefix、wildcard、regexp,功能也比较全,别急我们一个个来看,同时如果后续还有常用的也可以补充进来
term查询
Term查询用来查询某个关键字在文档里是否存在,所以Term需要是文档切分的一个关键字,这里我想要查性别为男的文档,此时如果输入男女去term搜索一定是一无所获的,因为查询参数不会自己切分
POST:localhost:9200/tml-userinfo/_doc/_search
请求体body
{ "query": { "term": { "sex": "男女" } } }
查询结果
{ "took": 3, "timed_out": false, "_shards": { "total": 3, "successful": 3, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 0, "relation": "eq" }, "max_score": null, "hits": [] } }
如果只查询性别为男的,那么男这个字是一个关键词,所以会被检索到:
{ "query": { "term": { "sex": "男" } } }
检索结果:
{ "took": 7, "timed_out": false, "_shards": { "total": 3, "successful": 3, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 7, "relation": "eq" }, "max_score": 0.6931471, "hits": [ { "_index": "tml-userinfo", "_type": "_doc", "_id": "5", "_score": 0.6931471, "_source": { "age": 28, "sex": "男", "name": "李小林" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "1", "_score": 0.6931471, "_source": { "age": 18, "sex": "男", "name": "森小辰" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "6", "_score": 0.6931471, "_source": { "age": 28, "sex": "男", "name": "李小林" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "2", "_score": 0.2876821, "_source": { "age": 28, "sex": "男", "name": "森小林" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "3", "_score": 0.2876821, "_source": { "age": 8, "sex": "男", "name": "森小贤" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "4", "_score": 0.2876821, "_source": { "age": 8, "sex": "男", "name": "李小贤" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "12", "_score": 0.2876821, "_source": { "age": 18, "sex": "男", "name": "李小辰" } } ] } }
terms查询
Terms查询用来查询某几个个关键字在文档里是否存在,Terms可以同时对一个字段检索多个关键字
POST:localhost:9200/tml-userinfo/_doc/_search
请求体body
{ "query": { "terms": { "age": [18,28] } } }
查询结果
{ "took": 60, "timed_out": false, "_shards": { "total": 3, "successful": 3, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 11, "relation": "eq" }, "max_score": 1.0, "hits": [ { "_index": "tml-userinfo", "_type": "_doc", "_id": "5", "_score": 1.0, "_source": { "age": 28, "sex": "男", "name": "李小林" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "13", "_score": 1.0, "_source": { "age": 18, "sex": "女", "name": "李小男" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "2", "_score": 1.0, "_source": { "age": 28, "sex": "男", "name": "森小林" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "10", "_score": 1.0, "_source": { "age": 18, "sex": "女", "name": "李小玲" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "12", "_score": 1.0, "_source": { "age": 18, "sex": "男", "name": "李小辰" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "14", "_score": 1.0, "_source": { "age": 18, "sex": "", "name": "李小男" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "15", "_score": 1.0, "_source": { "age": 18, "sex": "woman", "name": "赵小男" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "1", "_score": 1.0, "_source": { "age": 18, "sex": "男", "name": "森小辰" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "6", "_score": 1.0, "_source": { "age": 28, "sex": "男", "name": "李小林" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "9", "_score": 1.0, "_source": { "age": 18, "sex": "女", "name": "森小玲" } } ] } }
match查询
如果说Term查询是关键词全词匹配查询,那么Match查询和queryString有点类似,就是先对查询内容做分词,然后再去进行匹配,还拿上边的例子,我们查性别为男女的文档,Term查询是查不到的,Match查询却会检索到所有的数据
Post:localhost:9200/tml-userinfo/_doc/_search
请求body
{ "query": { "match": { "sex": "男女" } } }
返回值
{ "took": 19, "timed_out": false, "_shards": { "total": 3, "successful": 3, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 12, "relation": "eq" }, "max_score": 1.540445, "hits": [ { "_index": "tml-userinfo", "_type": "_doc", "_id": "10", "_score": 1.540445, "_source": { "age": 18, "sex": "女", "name": "李小玲" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "8", "_score": 0.7985077, "_source": { "age": 8, "sex": "女", "name": "森小美" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "9", "_score": 0.7985077, "_source": { "age": 18, "sex": "女", "name": "森小玲" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "11", "_score": 0.7985077, "_source": { "age": 28, "sex": "女", "name": "森小捷" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "5", "_score": 0.6931471, "_source": { "age": 28, "sex": "男", "name": "李小林" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "7", "_score": 0.6931471, "_source": { "age": 8, "sex": "女", "name": "李小美" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "1", "_score": 0.5978369, "_source": { "age": 18, "sex": "男", "name": "森小辰" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "6", "_score": 0.5978369, "_source": { "age": 28, "sex": "男", "name": "李小林" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "2", "_score": 0.24116206, "_source": { "age": 28, "sex": "男", "name": "森小林" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "3", "_score": 0.24116206, "_source": { "age": 8, "sex": "男", "name": "森小贤" } } ] } }



