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參數(shù),用于指定至少需要匹配多少個查詢條件才能返回文檔。
③ must_not查詢:所有的查詢條件都必須不匹配才能返回文檔。如果有任何一個查詢條件匹配,則該文檔將被排除。
這些布爾查詢類型可以組合在一起,以便更精確地過濾文檔。例如,您可以使用bool查詢來組合多個must查詢和should查詢,以便同時滿足多個查詢條件。
03. ElasticSearch bool must 組合多個查詢條件?
must查詢:所有的查詢條件都必須匹配才能返回文檔。如果有任何一個查詢條件不匹配,則該文檔將被排除。
① 索引文檔,構造數(shù)據(jù):
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參數(shù),用于指定至少需要匹配多少個查詢條件才能返回文檔。
① 索引文檔,構造數(shù)據(jù):
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參數(shù)來指定至少有多少個條件必須匹配。
查詢 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查詢:所有的查詢條件都必須不匹配才能返回文檔。如果有任何一個查詢條件匹配,則該文檔將被排除。
① 索引文檔,構造數(shù)據(jù):
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 組合多個查詢條件?
① 索引文檔,構造數(shù)據(jù):
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 過濾器是否支持嵌套查詢?
① 構造數(shù)據(jù):
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
}
② 執(zhí)行 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 查詢中 term
和 bool
查詢是兄弟關系,他們都處于外層的布爾邏輯 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 組合多個查詢條件?
① 構造數(shù)據(jù):
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 查詢文章來源:http://www.zghlxwxcb.cn/news/detail-404355.html
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 查詢文章來源地址http://www.zghlxwxcb.cn/news/detail-404355.html
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實現(xiàn)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);
}
}
到了這里,關于ElasticSearch系列 - SpringBoot整合ES:組合多個查詢條件 bool 查詢的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!