brunch

You can make anything
by writing

C.S.Lewis

by 무명 Aug 24. 2020

[es] bool, filter, must, score

쿼리 위계와 context

가끔 복잡한 기능과 조건을 수행하는 쿼리를 만들 때, 

동작순위, 원하는 기능, 문법에 맞게 구현하는 것이 헷갈리기도 한다. 

그래서 몇개 좀 찾아보고 정리한다. 




Constant score query

Wraps a filter query and returns every matching document with a relevance score equal to the 

boost parameter value.

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-constant-score-query.html

constant_score는 이렇다. 

내가 짜야 하는 쿼리는 script_score를 사용하고 있어서 보기만 했다. 




Script score query

Uses a script to provide a custom score for returned documents.

The script_score query is useful if, for example, a scoring function is expensive and you only need to calculate the score of a filtered set of documents

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-script-score-query.html


명시된 것 처럼, "params"로 "source"에 쓰이는 변수를 매핑해서 기술할 수 있다. 

params.name1 같은 형식으로.



Boolean query

A query that matches documents matching boolean combinations of other queries.

https://www.elastic.co/guide/en/elasticsearch/reference/6.8/query-filter-context.html

bool속에 must, filter, should, must_not들을 복수개 사용 가능하다. 


filter와 must_not의 차이는 결과가 보여지느냐 아니냐의 차이다. 

must_not을 쓰면 matched documents 에 나오지 않는다. 

그리고 둘 다 스코어링도 되지 않는다. 

https://stackoverflow.com/questions/47226479/difference-between-must-not-and-filter-in-elasticsearch

아래 표에 너무 잘 정리되어있다. 

이걸 잘 고려해야 쿼리시에 불필요한 연산과 속도를 잡을 수 있다. 



매거진의 이전글 [Elasticsearch] ES 필드타입 변경
작품 선택
키워드 선택 0 / 3 0
댓글여부
afliean
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari