ElasticSearch系列 - SpringBoot整合ES:组合多个查询条件 bool 查询

01. ElasticSearch 布尔查询是什么?

在实际应用中,我们很有可能会查询多个值或字段。 一个 bool 查询由三部分组成:

{
   "bool" : {
      "must" :     [],
      "should" :   [],
      "must_not" : [],
   }
}

must:所有的语句都必须(must) 匹配,与 AND 等价。

must_not:所有的语句都不能(must not)匹配,与 NOT 等价。

should:至少有一个语句要匹配,与 OR 等价。

02. ElasticSearch 布尔查询有哪些类型?

Elasticsearch布尔查询有三种类型:must查询、should查询和must_not查询。

① must查询:所有的查询条件都必须匹配才能返回文档。如果有任何一个查询条件不匹配,则该文档将被排除。

② should查询:至少有一个查询条件匹配时就可以返回文档。如果所有的查询条件都不匹配,则该文档将被排除。should查询可以设置一个minimum_should_match参数,用于指定至少需要匹配多少个查询条件才能返回文档。

③ must_not查询:所有的查询条件都必须不匹配才能返回文档。如果有任何一个查询条件匹配,则该文档将被排除。

这些布尔查询类型可以组合在一起,以便更精确地过滤文档。例如,您可以使用bool查询来组合多个must查询和should查询,以便同时满足多个查询条件。

03. ElasticSearch bool must 组合多个查询条件?

must查询:所有的查询条件都必须匹配才能返回文档。如果有任何一个查询条件不匹配,则该文档将被排除。

① 索引文档,构造数据:

PUT /my_index
{
  "mappings": {
    "properties": {
      "createTime":{
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss"
      },
      "tag":{
        "type": "keyword"
      },
      "price":{
        "type": "integer"
      }
    }
  }
}

PUT /my_index/_doc/1
{
  "createTime": "2023-03-29 10:30:11",
  "tag": "tag1",
  "price": 10
}

PUT /my_index/_doc/2
{
  "createTime": "2023-03-29 10:35:11",
  "tag": "tag2",
  "price": 20
}

PUT /my_index/_doc/3
{
  "createTime": "2023-03-29 10:38:11",
  "tag": "tag3",
  "price": 30
}

② 查询 tag 字段值为 tag1 并且 price 字段的值在10到20之间的文档,两个条件同时满足的文档才会返回:

GET /my_index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "tag": {
              "value": "tag1"
            }
          }
        },
        {
          "range": {
            "price": {
              "gte": 10,
              "lte": 20
            }
          }
        }
      ]
    }
  }
}

04. ElasticSearch bool should 组合多个查询条件?

should查询:至少有一个查询条件匹配时就可以返回文档。如果所有的查询条件都不匹配,则该文档将被排除。should查询可以设置一个minimum_should_match参数,用于指定至少需要匹配多少个查询条件才能返回文档。

① 索引文档,构造数据:

PUT /my_index
{
  "mappings": {
    "properties": {
      "createTime":{
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss"
      },
      "tag":{
        "type": "keyword"
      },
      "price":{
        "type": "integer"
      }
    }
  }
}

PUT /my_index/_doc/1
{
  "createTime": "2023-03-29 10:30:11",
  "tag": "tag1",
  "price": 10
}

PUT /my_index/_doc/2
{
  "createTime": "2023-03-29 10:35:11",
  "tag": "tag2",
  "price": 20
}

PUT /my_index/_doc/3
{
  "createTime": "2023-03-29 10:38:11",
  "tag": "tag3",
  "price": 30
}

② 查询 tag 字段值为 tag1 或者 price 字段的值在10到20之间的文档,两个条件只要有一个满足的文档就会返回:

GET /my_index/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "tag": {
              "value": "tag1"
            }
          }
        },
        {
          "range": {
            "price": {
              "gte": 10,
              "lte": 20
            }
          }
        }
      ]
    }
  }
}
{
  "took" : 13,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.9808292,
    "hits" : [
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.9808292,
        "_source" : {
          "createTime" : "2023-03-29 10:30:11",
          "tag" : "tag1",
          "price" : 10
        }
      },
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "createTime" : "2023-03-29 10:35:11",
          "tag" : "tag2",
          "price" : 20
        }
      }
    ]
  }
}

③ 在ElasticSearch的Bool查询中,可以使用should查询来指定多个查询条件,表示至少有一个条件必须匹配。同时,可以使用minimum_should_match参数来指定至少有多少个条件必须匹配。

查询 tag 字段值为 tag1 或者 price 字段的值在10到20之间的文档,并且至少有两个条件必须匹配:

GET /my_index/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "tag": {
              "value": "tag1"
            }
          }
        },
        {
          "range": {
            "price": {
              "gte": 10,
              "lte": 20
            }
          }
        }
      ],
      "minimum_should_match": 2
    }
  }
}
{
  "took" : 6,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.9808292,
    "hits" : [
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.9808292,
        "_source" : {
          "createTime" : "2023-03-29 10:30:11",
          "tag" : "tag1",
          "price" : 10
        }
      }
    ]
  }
}

05. ElasticSearch bool must_not 组合多个查询条件?

must_not查询:所有的查询条件都必须不匹配才能返回文档。如果有任何一个查询条件匹配,则该文档将被排除。

① 索引文档,构造数据:

PUT /my_index
{
  "mappings": {
    "properties": {
      "createTime":{
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss"
      },
      "tag":{
        "type": "keyword"
      },
      "price":{
        "type": "integer"
      }
    }
  }
}

PUT /my_index/_doc/1
{
  "createTime": "2023-03-29 10:30:11",
  "tag": "tag1",
  "price": 10
}

PUT /my_index/_doc/2
{
  "createTime": "2023-03-29 10:35:11",
  "tag": "tag2",
  "price": 20
}

PUT /my_index/_doc/3
{
  "createTime": "2023-03-29 10:38:11",
  "tag": "tag3",
  "price": 30
}

② 查询 tag 字段不等于tag1 并且 price 字段的值不在10到20之间的文档:

GET /my_index/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "term": {
            "tag": {
              "value": "tag1"
            }
          }
        },
        {
          "range": {
            "price": {
              "gte": 10,
              "lte": 20
            }
          }
        }
      ]
    }
  }
}
{
  "took" : 14,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 0.0,
        "_source" : {
          "createTime" : "2023-03-29 10:38:11",
          "tag" : "tag3",
          "price" : 30
        }
      }
    ]
  }
}

06. ElasticSearch bool 组合多个查询条件?

① 索引文档,构造数据:

PUT /my_index
{
  "mappings": {
    "properties": {
      "createTime":{
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss"
      },
      "tag":{
        "type": "keyword"
      },
      "price":{
        "type": "integer"
      }
    }
  }
}

PUT /my_index/_doc/1
{
  "createTime": "2023-03-29 10:30:11",
  "tag": "tag1",
  "price": 10
}

PUT /my_index/_doc/2
{
  "createTime": "2023-03-29 10:35:11",
  "tag": "tag2",
  "price": 20
}

PUT /my_index/_doc/3
{
  "createTime": "2023-03-29 10:38:11",
  "tag": "tag3",
  "price": 30
}

② 查询 tag 字段的值不为tag1 ,且createTime的值为"2023-03-29 10:38:11",或者 price 字段的值在10到20之间的文档:

GET /my_index/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "term": {
            "tag": {
              "value": "tag1"
            }
          }
        }
      ],
      "should": [
        {
          "range": {
            "price": {
              "gte": 10,
              "lte": 20
            }
          }
        }
      ],
      "must": [
        {
          "term": {
            "createTime": {
              "value": "2023-03-29 10:38:11"
            }
          }
        }
      ]
    }
  }
}
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "createTime" : "2023-03-29 10:38:11",
          "tag" : "tag3",
          "price" : 30
        }
      }
    ]
  }
}

07. ElasticSearch bool 过滤器是否支持嵌套查询?

① 构造数据:

PUT /my_index
{
  "mappings": {
    "properties": {
      "createTime":{
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss"
      },
      "tag":{
        "type": "keyword"
      },
      "price":{
        "type": "integer"
      }
    }
  }
}

PUT /my_index/_doc/1
{
  "createTime": "2023-03-29 10:30:11",
  "tag": "tag1",
  "price": 10
}

PUT /my_index/_doc/2
{
  "createTime": "2023-03-29 10:35:11",
  "tag": "tag2",
  "price": 20
}

PUT /my_index/_doc/3
{
  "createTime": "2023-03-29 10:38:11",
  "tag": "tag3",
  "price": 30
}

② 执行 bool 嵌套查询:

条件1:tag字段的值为tag2

条件2:price 字段的值为10 并且 createTime 字段的值为 “2023-03-29 10:30:11”

条件1 和 条件2 为或的关系

GET /my_index/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "tag": "tag2"
          }
        },
        {
          "bool": {
            "must": [
              {
                "term": {
                  "price": 10
                }
              },
              {
                "term": {
                  "createTime": "2023-03-29 10:30:11" 
                }
              }
            ]
          }
        }
      ]
    }
  }
}

外部的 bool 查询中 termbool 查询是兄弟关系,他们都处于外层的布尔逻辑 should 的内部,返回的命中文档至少须匹配其中一个过查询的条件。内部的 bool 查询中的两个 term 语句作为兄弟关系,同时处于 must 语句之中,所以返回的命中文档要必须都能同时匹配这两个条件。

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 2.0,
    "hits" : [
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 2.0,
        "_source" : {
          "createTime" : "2023-03-29 10:30:11",
          "tag" : "tag1",
          "price" : 10
        }
      },
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.9808292,
        "_source" : {
          "createTime" : "2023-03-29 10:35:11",
          "tag" : "tag2",
          "price" : 20
        }
      }
    ]
  }
}

08. ElasticSearch bool must 组合多个查询条件?

① 构造数据:

PUT /my_index
{
  "mappings": {
    "properties": {
      "price":{
        "type": "integer"
      },
      "title":{
        "type": "text"
      },
      "content":{
        "type": "text"
      }
    }
  }
}

PUT /my_index/_doc/1
{
  "title": "金都时尚情侣浪漫主题酒店",
  "content": "山东省青岛市",
  "price": 300
}

PUT /my_index/_doc/2
{
  "title": "金都嘉怡假日酒店",
  "content": "北京市",
  "price": 400
}

PUT /my_index/_doc/3
{
  "title": "金都欣欣24小时酒店",
  "content": "安徽省淮北市",
  "price": 200
}

② match 查询

GET  /my_index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "title": "嘉怡"
          }
        },
        {
          "match": {
            "content": "北京"
          }
        }
      ]
    }
  }
}
{
  "took" : 18,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 3.845211,
    "hits" : [
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 3.845211,
        "_source" : {
          "title" : "金都嘉怡假日酒店",
          "content" : "北京市",
          "price" : 400
        }
      }
    ]
  }
}

③ match 查询和 term 查询

GET  /my_index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "title": "嘉怡"
          }
        },
        {
          "term": {
            "price": 400
          }
        }
      ]
    }
  }
}
{
  "took" : 7,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 3.1105196,
    "hits" : [
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 3.1105196,
        "_source" : {
          "title" : "金都嘉怡假日酒店",
          "content" : "北京市",
          "price" : 400
        }
      }
    ]
  }
}

09. SpringBoot整合ES实现bool查询

GET /my_index/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "term": {
            "tag": {
              "value": "tag1"
            }
          }
        }
      ],
      "should": [
        {
          "range": {
            "price": {
              "gte": 10,
              "lte": 20
            }
          }
        }
      ],
      "must": [
        {
          "term": {
            "createTime": {
              "value": "2023-03-29 10:38:11"
            }
          }
        }
      ]
    }
  }
}
@Slf4j
@Service
public class ElasticSearchImpl {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    public void searchUser() throws IOException {
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        // bool 查询
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();

        // must_not
        TermQueryBuilder termQueryBuilder = new TermQueryBuilder("tag","tag1");
        boolQueryBuilder.mustNot(termQueryBuilder);

        // should
        RangeQueryBuilder rangeQueryBuilder = new RangeQueryBuilder("price");
        rangeQueryBuilder.gte(10);
        rangeQueryBuilder.lte(20);
        boolQueryBuilder.should(rangeQueryBuilder);

        // must
        termQueryBuilder = new TermQueryBuilder("createTime","2023-03-29 10:38:11");
        boolQueryBuilder.must(termQueryBuilder);

        searchSourceBuilder.query(boolQueryBuilder);

        SearchRequest searchRequest = new SearchRequest(new String[]{"my_index"},searchSourceBuilder);
        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        System.out.println(searchResponse);
    }
}
  • 5
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
冒泡排序算法是一种基本的排序算法,在C语言中可以使用循环语句和条件语句完成。其基本思想是通过逐一比较相邻的两个元素,每次将大小不符合要求的元素交换位置,从而实现排序的目的。 具体实现方式为,我们从数列的第一个元素开始,逐一比较相邻的两个元素。如果前一个元素大于后一个元素,则交换两个元素的位置。这样一次比较下来,最后一个元素一定是数组中的最大值。接下来,我们对未排序的元素重复相同的操作,直到所有元素排序完成。 以下是用C语言实现冒泡排序的程序代码: ```c #include <stdio.h> int main() { int array[] = {15, 8, 20, 33, 44, 10, 5}; int n = sizeof(array) / sizeof(int); // 数组长度 int temp; // 冒泡排序 for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - 1 - i; j++) { if (array[j] > array[j + 1]) { temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } } } // 打印排序后的数组 for (int i = 0; i < n; i++) { printf("%d ", array[i]); } return 0; } ``` 在上面的代码中,我们首先定义了一个整型数组`array`来存储需要排序的元素,然后使用`sizeof`操作符获取该数组的长度,赋值给`n`变量。接下来,我们使用两个嵌套的`for`循环实现了冒泡排序算法。内层循环用于逐一比较相邻的元素并交换位置,外层循环控制排序的次数。最后,我们在循环结束后再次遍历数组,打印出排序后的结果。 需要注意的是,在实现冒泡排序时,如果某一次循环中没有发生任何元素交换,则说明整个数组已经有序,可以提前结束排序过程,以提高程序的效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我一直在流浪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值