乐闻世界logo
搜索文章和话题

What is a bool query in Elasticsearch?

1个答案

1

The bool query in Elasticsearch is a compound query that enables you to combine multiple query clauses using boolean logic to enhance the relevance and accuracy of search results. It consists of four clause types: must, must_not, should, and filter.

  1. must: Conditions specified here must be met. This corresponds to the AND operation in SQL. For example, to find documents where the title contains 'apple' and the content contains 'iphone', include both conditions in the must clause.

  2. must_not: Conditions specified here must not be met, negating the condition. This corresponds to the NOT operation in SQL. For example, to exclude documents where the content contains 'android', place this condition in the must_not clause.

  3. should: Conditions specified here are not mandatory, but if satisfied, they can boost the document's relevance score. This is analogous to the OR operation in SQL. For example, if a document's title contains 'review' or 'description', it may increase the document's relevance.

  4. filter: This clause is used for filtering query results, but unlike must, it does not affect scoring. Using filter improves query efficiency because Elasticsearch caches the filtered results. It is suitable for cases where you only need to filter documents meeting the criteria without considering their match score. For example, filtering documents within a specific time range.

A practical example is when operating an electronics store website and wanting to find products where reviews mention 'durable' and ratings are above 4 stars, but exclude those mentioning 'expensive'. The query can be constructed as follows:

json
{ "query": { "bool": { "must": [ { "match": { "comment": "durable" } }, { "range": { "rating": { "gte": 4 } } } ], "must_not": [ { "match": { "comment": "expensive" } } ], "should": [ { "match": { "category": "electronics" } } ] } } }

This query combines multiple conditions using the bool query to ensure that the results are both precise and relevant.

2024年8月13日 21:38 回复

你的答案