2.X/1. Getting Started

1-07-3. Queries and Filters

drscg 2017. 9. 30. 20:02

The DSL used by Elasticsearch has a single set of components called queries, which can be mixed and matched in endless combinations. This single set of components can be used in two contexts: filtering context and query context.

elasticsearch에서 사용되는 DSL은"DSL (Domain Specific Language)"query라 불리는 component의 단일 집합을 가지는데, 이것은 무한한 조합으로 섞이고 일치될 수 있다. 이 단일 component 집합은 두 개의 context, filtering과 query context에서 사용될 수 있다.

When used in filtering context, the query is said to be a "non-scoring" or "filtering" query. That is, the query simply asks the question: "Does this document match?". The answer is always a simple, binary yes|no.

filtering context 에서 사용될 경우, query는 "non-scoring(score 연산을 하지 않는)" 또는 "filtering" query가 된다. 즉, query는 단순히 "이 document가 일치하는가?" 라는 질문을 던진다. 답변은 항상 yes 또는 no로 간단하다.

  • Is the created date in the range 2013 - 2014?

    created date가 2013 ~ 2014 사이인가?

  • Does the status field contain the term published?

    status field가 published 라는 단어를 포함하고 있는가?

  • Is the lat_lon field within 10km of a specified point?

    lat_lon field가 지정된 지점에서 10km 이내인가?

When used in a querying context, the query becomes a "scoring" query. Similar to its non-scoring sibling, this determines if a document matches and how well the document matches.

querying context 에서 사용될 때, query는 "scoring(score 연산을 하는)" query가 된다. non-scoring과 유사하게 이것은 document가 일치하는지 여부 와 얼마나  일치하는지를 결정한다.

A typical use for a query is to find documents:

query의 일반적인 사용처는 document를 발견하는 것이다.

  • Best matching the words full text search

    full text search 라는 단어에 가장 일치하는

  • Containing the word run, but maybe also matching runsrunningjog, or sprint

    run 이라는 단어를 포함하면서, runsrunningjog 또는 sprint 또한 일치하는

  • Containing the words quickbrown, and fox—the closer together they are, the more relevant the document

    quickbrown 그리고 fox 라는 단어를 포함하는 – 해당 단어들이 서로 근접해 있을수록, 더 관련 있는 document

  • Tagged with lucenesearch, or java—the more tags, the more relevant the document

    lucenesearch 또는 java 로 태그된 - 더 많이 태그될수록, 더 관련 있는 document

A scoring query calculates how relevant each document is to the query, and assigns it a relevance _score, which is later used to sort matching documents by relevance. This concept of relevance is well suited to full-text search, where there is seldom a completely "correct" answer.

scoring query는 각 document가 query에 얼마나 관련(relevant) 있는지를 계산하고, relevance _score를 할당한다. 나중에 relevance를 기준으로, 일치하는 document를 정렬하는데 사용된다. 이 relevance의 개념은 완벽한 "정답(correct answer)" 이 거의 없는, full-text 검색에 적합하다.

Note

Historically, queries and filters were separate components in Elasticsearch. Starting in Elasticsearch 2.0, filters were technically eliminated, and all queries gained the ability to become non-scoring.

원래 elasticsearch에서 query와 filter는 구분된 component였다. elasticsearch 2.0 에서부터, filter는 기술적으로 제거되었다. 그리고, 모든 query는 score 계산을 하지 않는(non-scoring) 기능을 가지게 되었다.

However, for clarity and simplicity, we will use the term "filter" to mean a query which is used in a non-scoring, filtering context. You can think of the terms "filter", "filtering query" and "non-scoring query" as being identical.

그러나, 명확함과 단순함을 위해, score 계산을 하지 않는(non-scoring) filtering context에 사용되는 query를 의미하는 "filter"라는 단어를 사용한다. 단어 "filter"를 "filtering query"와 "non-scoring query"와 동일하게 생각할 수 있다.

Similarly, if the term "query" is used in isolation without a qualifier, we are referring to a "scoring query".

마찬가지로, 단어 "query"가 수식어 없이 홀로 사용된다면, "scoring query"를 의미하는 것이다.

Performance Differencesedit

Filtering queries are simple checks for set inclusion/exclusion, which make them very fast to compute. There are various optimizations that can be leveraged when at least one of your filtering query is "sparse" (few matching documents), and frequently used non-scoring queries can be cached in memory for faster access.

filtering query는 단순하게 포함/배제되는 집합을 확인하는데, 이것은 계산이 매우 빠르다. filtering query 중 적어도 하나가 희박할(일치하는 document가 거의 없는) 경우 활용될 수 있는 다양한 최적화가 있고, 자주 사용되는 non-scoring(score를 계산하지 않는) query는 더 빠른 접근을 위해 메모리에 cache될 수 있다.

In contrast, scoring queries have to not only find matching documents, but also calculate how relevant each document is, which typically makes them heavier than their non-scoring counterparts. Also, query results are not cacheable.

반면에, scoring(score를 계산하는) query는 일치하는 document를 찾을뿐만 아니라, 각 document가 얼마나 관련 있는 지를 계산한다. 이것이 일반적으로 non-scoring(score를 계산하지 않는) query보다 scoring query를 무겁게 만든다. 또한 query의 결과는 cache되지 않는다.

Thanks to the inverted index, a simple scoring query that matches just a few documents may perform as well or better than a filter that spans millions of documents. In general, however, a filter will outperform a scoring query. And it will do so consistently.

inverted index 덕분에, 단순히 몇 개의 document에 일치하는 간단한 query는, 수백만의 document를 cache한 filter보다 더 잘 동작할지도 모른다. 그러나, 일반적으로, cache된 filter는 scoring query를 능가할 것이고, 지속적으로 그렇게 동작할 것이다.

The goal of filtering is to reduce the number of documents that have to be examined by the scoring queries.

filtering의 목표는 scoring query에 의해 검토되어야 하는 document의 수를 줄이는 것이다.

When to Use Whichedit

As a general rule, use query clauses for full-text search or for any condition that should affect therelevance score, and use filters for everything else.

일반적으로, full-text 검색이나 relevance score 에 영향 받는 어떤 조건에 대해서는 query절을 사용하자. 그 외에는 filter절을 사용하자.


'2.X > 1. Getting Started' 카테고리의 다른 글

1-07-1. Empty Search  (0) 2017.09.30
1-07-2. Query DSL  (0) 2017.09.30
1-07-4. Most Important Queries  (0) 2017.09.30
1-07-5. Combining queries together  (0) 2017.09.30
1-07-6. Validating Queries  (0) 2017.09.30