bool query
组合一个或者多个布尔查询,支持以下布尔查询,子句查询都是一个布尔查询,查询模式为 是否匹配,是否存在,是否满足条件等
must
子句查询内容必须出现在文档中,参与计分
filter
子句查询内容必须出现在文档中,不参与计分(子句在Filter上下文中执行)
should
子句查询至少满足一条,参与计分
minimum_should_match 参数可以指定满足的子句条数,支持常量数字和百分比
must_not
子句查询内容必须不能出现在文档中,不参与计分(子句在Filter上下文中执行)
示例
# 创建索引
PUT /index_bool
# 构建数据
PUT /index_bool/_doc/1
{
"name": "张三",
"age": 24
}
PUT /index_bool/_doc/2
{
"name": "张三通",
"age": 28
}
PUT /index_bool/_doc/3
{
"name": "李三通",
"age": 28
}
must
# must的条件必须全部满足
POST /index_bool/_search
{
"query": {
"bool": {
"must": {
"match": {
"name": {
"query": "张 三",
"operator": "AND"
}
}
}
}
}
}
must_not
# must_not中条件必须全部不满足
POST /index_bool/_search
{
"query": {
"bool": {
"must_not": [
{
"term": {
"name": "张"
}
},
{
"range": {
"age": {
"gt": 24
}
}
}
]
}
}
}
should
# should 满足其一即可
POST /index_bool/_search
{
"query": {
"bool": {
"should": [
{
"range": {
"age": {
"gt": 24
}
}
},
{
"prefix": {
"name": {
"value": "张"
}
}
}
]
}
}
}
filter
# filter 过滤满足的条件
POST /index_bool/_search
{
"query": {
"bool": {
"filter": {
"range": {
"age": {
"gt": 24
}
}
}
}
}
}