ElasticSearch 花式搜索详解(第七更)

it2025-03-26  7

ES复杂查询

先了解搜索后的结构体!

注意 在kibana中每个key 在后面结合springboot后都是对应的对象!

筛选字段 source

"_source": ["field","field",...]

如果设置source就像mysql中指定字段名一样!

排序 sort

"sort": [ { "age": { #这是个集合,代表可以有多个值进行排序 "order": "desc" #asc或desc } } ]

分页 form,size

"from": 0, #从第几个数据开始 "size": 1 #显示多少条

多条件匹配查询 must 相当于mysql中的关键词and

使用must可以将条件进行and匹配

GET /my_index6/_search { "query": { "bool": { "must": [ { "match": { "name": "张" } }, { "match": { "age": 23 } } ] } } }

可以把match换成term 单词进行精确匹配

多条件匹配 should 相当于 mysql中的关键词or

使用should 可以将条件进行or匹配

GET /my_index6/_search { "query": { "bool": { "should": [ { "match": { "name": "张二狗" } }, { "match": { "age": 23 } } ] } } }

多条件查询 must_not 不等于 则匹配

多条件 and关系

GET /my_index6/_search { "query": { "bool": { "must_not": [ { "match": { "age": 23 } } ] } }, "_source": ["age"] }

复杂查询 fitle 过滤

gt #大于 >gte #大于等于 >=lt #小于 <lte #小于等于 <=

查询年龄在22到24之间的数据

"filter": [ { "range": { "age": { "gte": 22, "lte": 24 } } } ]

复杂查询 一个字段多条件匹配 or

只需要空格隔开就行

text和keyword、match和term区别

text 会被分词器解析keyword 不会被分词器解析match 会模糊匹配term 会精确匹配(词),且会使用倒排索引,效率相对来说要高很多term+keyword 最精确查询

高亮查询

"highlight": { "fields": { "name": {} } }

默认高亮是em标签,我们还可以自定义标签和样式

总结!

匹配按条件匹配精确匹配排序区间范围匹配区间过滤匹配分页单个字段 多条件匹配高亮查询 自定义标签
最新回复(0)