Elasticsearch

[Elasticsearch] Elastic-builder - Must와 Should의 차이

Edyo 2023. 2. 27. 11:40

Elastic-builder 공식 문서에서의 Must와 Should 설명

  • Must
💡 must(queries)

Adds must query to boolean container. The clause (query) must appear in matching documents and will contribute to the score.
must(queries: (Array<Query> | Query)): BoolQuery​

Parameters
queries ((Array<Query> | Query)) List of valid Query objects or Query object

Returns
BoolQuery: returns this so that calls can be chained.

Throws
- TypeError: If Array item or query is not an instance of Query

 

  • should
💡 should(queries)

Adds should query to boolean container. The clause (query) should appear in the matching document. In a boolean query with no must or filter clauses, one or more should clauses must match a document. The minimum number of should clauses to match can be set using the minimum_should_match parameter.
should(queries: (Array<Query> | Query)): BoolQuery​

Parameters

queries ((Array<Query> | Query)) List of valid Query objects or Query object

Returns

BoolQuery: returns this so that calls can be chained.

Throws

  • TypeError: If Array item or query is not an instance of Query

 

 

 

 

계속 등장하는 BoolQuery에 대해 👇🏻

Elastic-builder 공식 문서에서의 BoolQuery에 대한 설명

💡 Boolquery()

A query that matches documents matching boolean combinations of other queries. The bool query maps to Lucene BooleanQuery. It is built using one or more boolean clauses, each clause with a typed occurrence.
new BoolQuery()​
Extends Query

Example
const qry = esb.boolQuery()
    .must(esb.termQuery('user', 'kimchy'))
    .filter(esb.termQuery('tag', 'tech'))
    .mustNot(esb.rangeQuery('age').gte(10).lte(20))
    .should([
        esb.termQuery('tag', 'wow'),
        esb.termQuery('tag', 'elasticsearch')
    ])
    .minimumShouldMatch(1)
    .boost(1.0);​

 

 

 

공식 문서 내용을 보고도 명확하게 이해가 되지 않아서 더 찾아보았다.

  • must means: The clause (query) must appear in matching documents. These clauses must match, like logical AND.
  • should means: At least one of these clauses must match, like logical OR.

Basically they are used like logical operators AND and OR. See this.

Now in a bool query:

  • must means: Clauses that must match for the document to be included.
  • should means: If these clauses match, they increase the _score; otherwise, they have no effect. They are simply used to refine the relevance score for each document.

출처는 글 맨 아래에 기재.

 

must는 AND , should는 OR 같은 개념.

 

 

 

References