2.X/1. Getting Started

1-07-4. Most Important Queries

drscg 2017. 9. 30. 19:58

While Elasticsearch comes with many queries, you will use just a few frequently. We discuss them in much greater detail in Search in Depth but next we give you a quick introduction to the most important queries.

Elasticsearch에는 많은 query가 있지만, 자주 사용하는 것은 소수일 것이다. Search in Depth에서, 훨씬 더 자세히 이야기할 것이다. 아래에서 가장 중요한 query들에 대해 빠르게 소개하겠다.

match_all Queryedit

The match_all query simply matches all documents. It is the default query that is used if no query has been specified:

match_all query는 단순하게 모든 document와 일치한다. 어떤 query도 지정하지 않았을 경우에 사용되는 기본 query이다.

{ "match_all": {}}

This query is frequently used in combination with a filter—for instance, to retrieve all emails in the inbox folder. All documents are considered to be equally relevant, so they all receive a neutral _score of 1.

이 query는 filter의 조합에 흔히 사용된다. 예를 들면, inbox folder에 있는 모든 mail을 가져오는 경우가 있다. 모든 document는 똑같이 관련이 있다고 간주된다. 그래서 모두 기본인 1 이라는 _score 를 받는다.

match Queryedit

The match query should be the standard query that you reach for whenever you want to query for a full-text or exact value in almost any field.

match query는 거의 모든 field에서 full-text 또는 exact value를 query할 때마다 사용하는, 표준 query이다.

If you run a match query against a full-text field, it will analyze the query string by using the correct analyzer for that field before executing the search:

full-text field에 match query를 실행하면, 검색을 실행하기 전에, 해당 field에 적합한 analyzer를 사용하여, query string을 분석한다.

{ "match": { "tweet": "About Search" }}

If you use it on a field containing an exact value, such as a number, a date, a Boolean, or a not_analyzed string field, then it will search for that exact value:

number, date, Boolean 또는 not_analyzed string field 같은 exact value를 가지고 있는 field에 사용하면, 해당 exact value를 검색할 것이다.

{ "match": { "age":    26           }}
{ "match": { "date":   "2014-09-01" }}
{ "match": { "public": true         }}
{ "match": { "tag":    "full_text"  }}
Tip

For exact-value searches, you probably want to use a filter clause instead of a query, as a filter will be cached. We’ll see some filtering examples soon.

exact-value 검색에서, filter는 cache되기 때문에, query 보다 filter를 사용하는 것이 좋다. 곧, 몇 가지 filtering 예제를 볼 수 있을 것이다.

Unlike the query-string search that we showed in Search Lite, the match query does not use a query syntax like +user_id:2 +tweet:search. It just looks for the words that are specified. This means that it is safe to expose to your users via a search field; you control what fields they can query, and it is not prone to throwing syntax errors.

Search Lite에서 본 query string 검색과 달리, match query는 +user_id:2 +tweet:search 같은 query 문법을 사용하지 않는다. 단지, 지정한 단어를 찾는다. 즉, 검색 field를 통해 사용자에게 노출하는 것이 안전하다. query할 수 있는 field를 조정하고, 문법 오류를 던지는 경향도 없다.

multi_match Queryedit

The multi_match query allows to run the same match query on multiple fields:

multi_match query는 다중 field에 대해 match query와 동일하게 실행된다.

{
    "multi_match": {
        "query":    "full text search",
        "fields":   [ "title", "body" ]
    }
}

range Queryedit

The range query allows you to find numbers or dates that fall into a specified range:

range query는 지정된 범위에 해당하는, number나 date를 찾는다.

{
    "range": {
        "age": {
            "gte":  20,
            "lt":   30
        }
    }
}

The operators that it accepts are as follows:

사용되는 operator는 아래와 같다.

gt
Greater than, ~ 보다 큰
gte
Greater than or equal to, ~ 보다 크거나 같은
lt
Less than, ~ 보다 작은
lte
Less than or equal to, ~ 보다 작거나 같은

term Queryedit

The term query is used to search by exact values, be they numbers, dates, Booleans, or not_analyzed exact-value string fields:

term query는 exact value로 검색할 때 사용된다. number, date, Boolean 또는 not_analyzed exact value string field에 사용된다.

{ "term": { "age":    26           }}
{ "term": { "date":   "2014-09-01" }}
{ "term": { "public": true         }}
{ "term": { "tag":    "full_text"  }}

The term query performs no analysis on the input text, so it will look for exactly the value that is supplied.

term query는 입력된 문자열을 분석(analysis) 하지 않는다. 따라서 정확히 제공된 값을 찾는다.

terms Queryedit

The terms query is the same as the term query, but allows you to specify multiple values to match. If the field contains any of the specified values, the document matches:

terms query는 term query와 동일하다. 다만, 일치하는 다수의 값을 지정할 수 있다. field가 지정한 값들 중 어느 것이라도 포함하고 있으면, document는 일치한다.

{ "terms": { "tag": [ "search", "full_text", "nosql" ] }}

Like the term query, no analysis is performed on the input text. It is looking for exact matches (including differences in case, accents, spaces, etc).

term query와 마찬가지로, 입력된 문자열에 대한 분석(analysis)을 하지 않는다. 정확히 일치하는 것(대소문자, 액센트, 공백 등을 포함하여)을 찾는다.

exists and missing Queriesedit

The exists and missing queries are used to find documents in which the specified field either has one or more values (exists) or doesn’t have any values (missing). It is similar in nature to IS_NULL(missing) and NOT IS_NULL (exists)in SQL:

exists 와 missing query는 지정된 field가 하나 이상의 값을 가지거나(exists) 어떤 값도 가지지 않는(missing) document를 찾는다. 본질적으로 SQL에서 IS_NULL (missing) 과 NOT IS_NULL (exists)과 유사하다.

{
    "exists":   {
        "field":    "title"
    }
}

These queries are frequently used to apply a condition only if a field is present, and to apply a different condition if it is missing.

이들 query는 field가 존재하는 경우에만 어떤 조건을 적용하고, 없는 경우에는 다른 조건을 적용하기 위해 흔히 사용된다.


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

1-07-2. Query DSL  (0) 2017.09.30
1-07-3. Queries and Filters  (0) 2017.09.30
1-07-5. Combining queries together  (0) 2017.09.30
1-07-6. Validating Queries  (0) 2017.09.30
1-08. Sorting and Relevance  (0) 2017.09.30