国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

ElasticSearch系列 - SpringBoot整合ES:組合多個查詢條件 bool 查詢

這篇具有很好參考價值的文章主要介紹了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參數(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 查詢中 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 組合多個查詢條件?

① 構造數(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 查詢

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)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉載,請注明出處: 如若內容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • ElasticSearch系列 - SpringBoot整合ES:查詢字段不為空的文檔 exists

    1. ElasticSearch exists 查詢是什么 在某些場景下,我們希望找到某個字段不為空的文檔,則可以用exists搜索。字段不為空的條件有: 值存在且不是 null; 值不是空數(shù)組; 值是數(shù)組,但不是 [null] 例如,查詢在字段中至少有一個非空值的文檔: 這些文檔都將匹配上面的查詢: ①

    2024年02月06日
    瀏覽(23)
  • ElasticSearch系列 - SpringBoot整合ES:查詢條件 query 和過濾條件 filter 的區(qū)別

    01. Elasticsearch 查詢條件和過濾條件的區(qū)別? Elasticsearch中的查詢條件和過濾條件都是用于搜索和過濾文檔的條件,但它們之間有一些區(qū)別。 查詢條件是用于計算文檔相關度得分的條件,它會將所有符合條件的文檔按照相關度得分從高到低排序,并返回前N個文檔。查詢條件可以

    2024年02月14日
    瀏覽(26)
  • springboot整合elasticsearch8組合條件查詢

    整合過程見上一篇文章 springboot整合elasticsearch8 1.es8多條件組合查詢 2.使用scroll進行大數(shù)據(jù)量查詢

    2024年02月16日
    瀏覽(19)
  • ElasticSearch序列 - SpringBoot整合ES:范圍查詢 range

    01. ElasticSearch range查詢是什么? Elasticsearch 中的 range 查詢可以用于查詢某個字段在一定范圍內的文檔。 range 查詢可同時提供包含和不包含這兩種范圍表達式,可供組合的選項如下: gt : 大于(greater than) lt : 小于(less than) gte : = 大于或等于(greater than or equal to) lte : = 小于

    2024年02月09日
    瀏覽(28)
  • ElasticSearch序列 - SpringBoot整合ES:根據(jù)指定的 ids 查詢

    1. ElasticSearch 根據(jù) ids 查詢文檔 ① 索引文檔,構造數(shù)據(jù) ② 查詢文檔 id 為 1 或者 2 的文檔: 我們索引文檔時,文檔的id為整型,為什么查詢出來的文檔 id為字符串類型呢?如果我們使用字符串類型的文檔id查詢呢? 可以看到仍然可以查詢到匹配的文檔。 在Elasticsearch中,文檔

    2024年02月11日
    瀏覽(23)
  • ElasticSearch系列 - SpringBoot整合ES:ElasticSearch分析器

    1. ElasticSearch match 文本搜索的過程? Elasticsearch 的 match 查詢是一種基于文本匹配的查詢方式,它的搜索過程如下: ① 將查詢字符串分詞:Elasticsearch 會將查詢字符串分成一個個詞項(term),并去除停用詞(如“的”、“是”等常用詞匯)和標點符號等無意義的字符。 ② 構建

    2023年04月18日
    瀏覽(25)
  • ElasticSearch系列 - SpringBoot整合ES:分析器

    1. ElasticSearch match 文本搜索的過程? Elasticsearch 的 match 查詢是一種基于文本匹配的查詢方式,它的搜索過程如下: ① 將查詢字符串分詞:Elasticsearch 會將查詢字符串分成一個個詞項(term),并去除停用詞(如“的”、“是”等常用詞匯)和標點符號等無意義的字符。 ② 構建

    2024年02月06日
    瀏覽(24)
  • ElasticSearch系列 - SpringBoot整合ES:實現(xiàn)搜索結果排序 sort

    00. 數(shù)據(jù)準備 01. Elasticsearch 默認的排序方式是什么? ElasticSearch 默認的排序方式是相關性排序。相關性排序是根據(jù)查詢條件與文檔的匹配程度來計算每個文檔的相關性得分,然后按照得分從高到低進行排序。相關性排序是 ElasticSearch 中最常用的排序方式,因為它可以根據(jù)查詢

    2024年02月02日
    瀏覽(22)
  • ElasticSearch系列 - SpringBoot整合ES:映射中定義字段的數(shù)據(jù)類型及屬性

    ElasticSearch - SpringBoot整合ES:映射定義字段的數(shù)據(jù)類型及屬性 01. ElasticSearch 搜索結果的準確性和召回率是什么? 在Elasticsearch中,搜索結果的準確性和召回率是非常重要的指標,它們反映了搜索引擎的性能和效果。以下是這兩個指標的定義和解釋: 準確性:搜索結果的準確性

    2024年02月08日
    瀏覽(25)
  • ElasticSearch系列 - SpringBoot整合ES:restHighLevelClient.count(countRequest, RequestOptions.DEFAULT)

    restHighLevelClient.count(countRequest, RequestOptions.DEFAULT) 是 Elasticsearch Java High Level REST Client 中用于執(zhí)行計數(shù)請求的方法。 具體來說,它接受兩個參數(shù): countRequest:一個 CountRequest 對象,表示計數(shù)請求的參數(shù),包括要計數(shù)的索引、查詢條件等。 RequestOptions.DEFAULT:一個 RequestOptions 對象

    2024年02月08日
    瀏覽(23)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包