复杂查询条件
IS NULL的查询
tags:[null]、tags:null
均属于空值的范畴
{
"query": {
"constant_score": {
"filter": {
"bool": {
"must_not": {
"exists": {
"field": "age"
}
}
}
}
}
}
}
match查询必须匹配两个字段,默认下match是取或的关系,比如"match:{"name":"love you"}"
,查询的时候会先利用解析器分别通过term
查询love
和you
,
{
"query": {
"constant_score": {
"filter": {
"match": {
"about": {
"query": "love to i",
"operator": "and"
}
}
}
}
}
}
term查询的例子
{
"query": {
"constant_score": {
"filter": {
"term": {
"about": "love"
}
}
}
}
}
精度的控制
{
"query": {
"constant_score": {
"filter": {
"match": {
"about": {
"query": "love i you",
"minimum_should_match": "75%"
}
}
}
}
}
}
minimum_should_match用于设置必须匹配的词数,可以是匹配的数量或者是输入的词的百分比,不管这个值设置为什么,至少要匹配一个词的文档才会返回
组合查询
通过 bool
关键字来组合相关的查询,子查询可以是must
,must_not
,should
之一,should中的子句可以都不需要满足,除非没有must
子句的时候的至少需要匹配一个should
中的条件。
{
"query": {
"bool": {
"should": [{
"match": {
"about": "love"
}
}, {
"match": {
"about": "you"
}
}],
"minimum_should_match": 2
}
}
}
其中的match
也可以改为term
当做全词匹配。
词的权重设置
{
"query": {
"bool": {
"must": {
"match": {
"content": {
"query": "full text search",
"operator": "and"
}
}
},
"should": [{
"match": {
"content": {
"query": "Elasticsearch",
"boost": 3
}
}
},
{
"match": {
"content": {
"query": "Lucene",
"boost": 2
}
}
}
]
}
}
}
boost
来设置某个词项的权重,默认是1,如果需要加强权重,可以设置一个大于1的值,值越大表现越重要。
网友评论