2.X/1. Getting Started

1-07-2. Query DSL

drscg 2017. 9. 30. 20:07

The query DSL is a flexible, expressive search language that Elasticsearch uses to expose most of the power of Lucene through a simple JSON interface. It is what you should be using to write your queries in production. It makes your queries more flexible, more precise, easier to read, and easier to debug.

query DSL은 간단한 JSON interface를 이용해, Lucene을 100% 활용하기 위해, Elasticsearch가 사용하는 유연하고, 표현력 있는 검색 언어이다. 제품에 query를 작성하는데 사용되어야 하는 것이다. query를 보다 유연하고, 보다 정확하고, 보다 쉽게 읽을 수 있고, 더 쉽게 debug할 수 있게 해 준다.

To use the Query DSL, pass a query in the query parameter:

query DSL을 사용하기 위해서는, query 매개변수에 query를 전달해야 한다.

GET /_search
{
    "query": YOUR_QUERY_HERE
}

The empty search{}—is functionally equivalent to using the match_all query clause, which, as the name suggests, matches all documents:

empty search({})는 match_all query 절을 사용하는 것과 기능적으로 동일하다. 이는 이름에서도 알 수 있듯이, 모든 document와 일치한다.

GET /_search
{
    "query": {
        "match_all": {}
    }
}

Structure of a Query Clauseedit

A query clause typically has this structure:

query 절은 일반적으로 다음과 같은 구조를 갖는다.

{
    QUERY_NAME: {
        ARGUMENT: VALUE,
        ARGUMENT: VALUE,...
    }
}

If it references one particular field, it has this structure:

하나의 특정 field를 참조한다면, 아래 구조를 갖는다.

{
    QUERY_NAME: {
        FIELD_NAME: {
            ARGUMENT: VALUE,
            ARGUMENT: VALUE,...
        }
    }
}

For instance, you can use a match query clause to find tweets that mention elasticsearch in the tweet field:

예를 들자면, tweet field에서, elasticsearch 를 언급하고 있는 tweet을 찾기 위해, match query 절을 사용할 수 있다.

{
    "match": {
        "tweet": "elasticsearch"
    }
}

The full search request would look like this:

전체 search request는 아래와 같다.

GET /_search
{
    "query": {
        "match": {
            "tweet": "elasticsearch"
        }
    }
}

Combining Multiple Clausesedit

Query clauses are simple building blocks that can be combined with each other to create complex queries. Clauses can be as follows:

query 절 은 복잡한 query를 생성하기 위해, 서로가 조합될 수 있는, 단순한 구성 요소이다. 절은 아래 중의 하나가 될 수 있다.

  • Leaf clauses (like the match clause) that are used to compare a field (or fields) to a query string.

    query string과 field(s)를 비교하는데 사용되는 match 절 같은 하위 절(leaf clauses)

  • Compound clauses that are used to combine other query clauses. For instance, a bool clauseallows you to combine other clauses that either must match, must_not match, or shouldmatch if possible. They can also include non-scoring, filters for structured search:

    다른 query 절과 조합하는데 사용되는 복합(compound) 절. 예를 들면, bool 절은 must match, must_not match 또는 should match 등의 다른 절을 조합할 수 있다. 또한, structured search에 대해 score 연산을 하지 않는 filter를 포함할 수 있다.

{
    "bool": {
        "must":     { "match": { "tweet": "elasticsearch" }},
        "must_not": { "match": { "name":  "mary" }},
        "should":   { "match": { "tweet": "full text" }},
        "filter":   { "range": { "age" : { "gt" : 30 }} }
    }
}

It is important to note that a compound clause can combine any other query clauses, including other compound clauses. This means that compound clauses can be nested within each other, allowing the expression of very complex logic.

복합(compound)절이, 다른 복합 절을 포함한, 다른 어떤 query절이라도 조합할 수 있다는 사실은 중요하다. 즉, 복합 절들이 서로 중첩될 수 있고, 매우 복잡한 logic을 표현할 수 있다.

As an example, the following query looks for emails that contain business opportunity and should either be starred, or be both in the Inbox and not marked as spam:

예를 들자면, 다음 query는 business opportunity 를 포함하고, 중요하다고 표시되거나, inbox에 있는데, spam이 아닌 email을 검색한다.

{
    "bool": {
        "must": { "match":   { "email": "business opportunity" }},
        "should": [
            { "match":       { "starred": true }},
            { "bool": {
                "must":      { "match": { "folder": "inbox" }},
                "must_not":  { "match": { "spam": true }}
            }}
        ],
        "minimum_should_match": 1
    }
}

Don’t worry about the details of this example yet; we will explain in full later. The important thing to take away is that a compound query clause can combine multiple clauses—both leaf clauses and other compound clauses—into a single query.

이 예제의 자세한 사항에 대해서는 아직 걱정하지 말자. 나중에 모두 설명할 것이다. 중요한 것은 복합 query 절은 다중 절(하위 절, 다른 복합 절 모두)을 단일 query로 조합할 수 있다는 점이다.


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

1-07. Full-Body Search  (0) 2017.09.30
1-07-1. Empty Search  (0) 2017.09.30
1-07-3. Queries and Filters  (0) 2017.09.30
1-07-4. Most Important Queries  (0) 2017.09.30
1-07-5. Combining queries together  (0) 2017.09.30