ES查询操作
示例
GET /test/_doc/_search?q=label:森林2
match
会使用分词器解析,所以是部分匹配
- 搜索所有带
林
字的精确匹配
GET /test/_doc/_search
{
"query": {
"match": {
"label": "森"
}
}
}
- 过滤结果字段
在示例1的结果中会默认查询出文档类的所有字段,
可以通过添加_source
来过滤结果集. 下面的示例中指定了结果仅显示label
字段
GET /test/_doc/_search
{
"query": {
"match": {
"label": "林"
}
},
"_source": ["label"]
}
- 结果排序
按结果排序会导致_score
字段为空。
这里由于一开始插值时没指定age字段类型,默认为text,且后期无法修改。text类型的字段无法作为排序条件Fielddata is disabled on text fields by default
.
GET /test/_doc/_search
{
"query": {
"match": {
"label": "林"
}
},
"sort": [
{
"age": {
"order": "desc"
}
}
]
}
- 分页
查询中指定from
, size
默认从0
开始
GET /test/_doc/_search
{
"query": {
"match": {
"label": "林"
}
},
"from": 0,
"size": 1
}
- 匹配分数过滤
仅返回匹配分数>0.3的
GET /test/_doc/_search
{
"query": {
"match": {
"label": "林"
}
},
"min_score":0.3
}
- 布尔查询
多条件精确查询
must
同时满足
GET /test/_doc/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"label": "林"
}
},
{
"match": {
"age": 3
}
}
]
}
}
}
should
相当于or
GET /test/_doc/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"label": "林"
}
},
{
"match": {
"age": 1
}
}
]
}
}
}
must_not
相当于not
GET /test/_doc/_search
{
"query": {
"bool": {
"must_not": [
{
"match": {
"label": "林"
}
}
]
}
}
}
一个混合示例, 查询label中有林
但age
不等于1
的
GET /test/_doc/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"label": "林"
}
}
],
"must_not": [
{
"match": {
"age": "1"
}
}
]
}
}
}
另一个复合查询示例,查询包含林
,且时间范围在2017-01-01 ~ 2019-12-31
的数据
GET test/_doc/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"label": "林"
}
},
{
"range" : {
"postdate" : {
"gte" : "2017-01-01",
"lte" : "2019-12-31",
"format": "yyyy-MM-dd"
}
}
}
]
}
}
}
- 区间查询
lt: 小于
lte: 小于等于
gt: 大于
gte: 大于等于
GET test/_doc/_search
{
"query": {
"range" : {
"postdate" : {
"gte" : "2017-01-01",
"lte" : "2019-12-31",
"format": "yyyy-MM-dd"
}
}
}
}
- 过滤器
GET /test/_doc/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"label": "林"
}
}
],
"filter": {
"range": {
"age": {
"gte": 2,
"lte": 5
}
}
}
}
}
}
- 包含某个字段的匹配
查询有tags
字段的文档,或者说tags
字段不为空的文档
GET test/_doc/_search
{
"query": {
"exists": {
"field": "tags"
}
}
}
- 多关键字匹配
直接用空格隔开即可,注意这里是or
关系
GET /test/_doc/_search
{
"query": {
"match": {
"tags": "区 田"
}
}
}
term
与match的区别,
对于输入的查询query,match会相对query进行分词,然后与匹配字段的分词结果进行匹配,也就是查询林田,会被分词为林,和田,然后看匹配字段的分词结果是否包含林或田。
但term中,输入林田,不会对林田进行分词,直接与匹配字段的分词结果进行匹配,注意,这里是和匹配字段的分词结果进行匹配,通过ik分词测试发现,林田的分词结果是林和田,而不包含林田,因此查不到结果。
https://www.cnblogs.com/yjf512/p/4897294.html
测试林田的ik分词结果
GET _analyze
{
"analyzer": "standard",
"text": "林田"
}
term查询
GET /test/_doc/_search
{
"query": {
"term": {
"label": "林田"
}
}
}
高亮查询
添加highlight
字段,会自动标注匹配成功的内容。
默认高亮内容被<em>
标签包裹。
可以添加pre_tags
,post_tags
自定义高亮css。
GET /test/_doc/_search
{
"query": {
"match": {
"label": "林"
}
},
"highlight": {
"pre_tags": "<div class='key' style='color: red;'>",
"post_tags": "</div>",
"fields": {
"label": {}
}
}
}
第二种方式 就是标签的位置换了下,
也可以指定多字段高亮(注意添加了"require_field_match":false
),如下面对tags
也进行了高亮.
GET /test/_doc/_search
{
"query": {
"match": {
"label": "林"
}
},
"highlight": {
"require_field_match":false,
"fields": {
"label": {
"pre_tags": "<div class='key' style='color: red;'>",
"post_tags": "</div>"
},
"tags": {}
}
}
}
显然直接在highlight
里面指定的pre_tags
和post_tags
是高亮默认的,对多字段高亮的情况下,可以单独在指定特定的高亮方式.