'DSL'에 해당되는 글 2건

  1. 2013.04.18 [Elasticsearch] Query DSL - Filters
  2. 2013.04.17 [Elasticsearch] Query DSL

[Elasticsearch] Query DSL - Filters

Elastic/Elasticsearch 2013. 4. 18. 10:46

본 문서는 개인적인 테스트와 elasticsearch.org 그리고 community 등을 참고해서 작성된 것이며,

정보 교환이 목적입니다.


잘못된 부분에 대해서는 지적 부탁 드립니다.

(예시 코드는 성능 및 보안 검증이 되지 않았습니다.)



[elasticsearch API 리뷰]

원문 링크 : http://www.elasticsearch.org/guide/reference/query-dsl/

- 많이 사용되는 것들로 진행 합니다.



 

Queries

Filters

 


















[Filters]

기본적으로 filtered query 에서 동작 방식을 소개 했기 때문에 이 점을 이해하고 보셔야 합니다.


[and/or]

- 쿼리 결과에 대한 추가 쿼리의 and/or 연산을 수행 합니다.

- 쿼리 결과를 cache 하고 싶을 경우 _cache:true 설정을 하면 됩니다.


[bool]

- boolean 쿼리를 추가 수행 합니다.


[exists]

- 결과에 대해서 항상 cache 합니다.


[ids]

- ids 를 포함한 문서를 필터 합니다.


[limit]

- shard 당 문서 수를 제한 합니다.


[type]

- document/mapping type 에 대한 filter 합니다.


[missing]

- 문서의 특정 필드 값이 no value 인 것을 filter 합니다.

- 지정된 field 는 null_value 를 갖습니다.

- 예제가 직관적이기 떄문에 추가 합니다.

{
    "constant_score" : {
        "filter" : {
            "missing" : { 
                "field" : "user",
                "existence" : true,
                "null_value" : true
            }
        }
    }
}


[not]

- 질의된 결과에 대해서 추가로 주어진 not filter 로 match 된 문서를 제외 합니다.


[numeric range]

- range filter와 유사하며, 어떤 수의 범위를 갖습니다.

- 주어진 parameters 는 아래와 같습니다.

NameDescription
fromThe lower bound. Defaults to start from the first.
toThe upper bound. Defaults to unbounded.
include_lowerShould the first from (if set) be inclusive or not. Defaults to true
include_upperShould the last to (if set) be inclusive or not. Defaults to true.
gtSame as setting from and include_lower to false.
gteSame as setting from and include_lower to true.
ltSame as setting to and include_upper to false.
lteSame as setting to and include_upper to true.

[prefix]

- phrase query 와 유사하며,  prefix query 참고


[query]

- 추가 query 를 생성 할 수 있습니다.


[range]

- range query 참고


[script]

- script 를 이용한 filter 를 적용 할 수 있습니다.


[term]

- term query 참고


[terms]

- terms query 참고

- execution mode 를 지원 합니다.

- 기본 plain 그리고 bool, and, or 지원


[nested]

- nested query 참고


※ filter 의 경우 기본 query 에서 제공 하는 것과 거의 동일 하며,

이 API 의 목적은 한번 질의한 결과에 대해 별도의 filtering 을 하기 위함 입니다.


:

[Elasticsearch] Query DSL

Elastic/Elasticsearch 2013. 4. 17. 17:41

본 문서는 개인적인 테스트와 elasticsearch.org 그리고 community 등을 참고해서 작성된 것이며,

정보 교환이 목적입니다.


잘못된 부분에 대해서는 지적 부탁 드립니다.

(예시 코드는 성능 및 보안 검증이 되지 않았습니다.)



[elasticsearch API 리뷰]

원문 링크 : http://www.elasticsearch.org/guide/reference/query-dsl/

- 많이 사용되는 것들로 진행 합니다.



 

Queries

Filters

 


















[Query]

[match]

- 기본 boolean type 과 or 연산


[multi match]

- 여러개의 field 에 대한 match query

-field 선언 시 boosting 옵션 추가 (^2 라는 의미는 2배 더 중요하다는 의미)


[bool]

- must : 문서에 매칭 되는 것이 무조건 나타나야 함.

- should : minimum_number_should_match 값을 통해서 문서에 매칭 되는 것이 나타나야 함.

- must_not : 문서에 매칭 정보가 있으면 안됨. (exclusive)


[boosting]

- query 의 결과를 효과적으로 감소 시킬 수 있다.

- 원문 예제가 이해하는데 직관적이라 추가 합니다.

{ "boosting" : { "positive" : { "term" : { "field1" : "value1" } }, "negative" : { "term" : { "field2" : "value2" } }, "negative_boost" : 0.2 }

}

- negative 조건에 따라 원하지 않는 문서에 대한 스코어를 떨어 트릴수 있습니다.


[constant_score]

- query 된 모든 문서의 score 를 동일하게 한다.


[dis_max]

- multi_field 에 단어 검색 시 유용하다.

- multiple field 에 같은 term 을 포함 하고 있을 경우 tie_breaker 값으로 더 좋은 결과 판정을 합니다.

- explain 을 떠보면서 검색 결과에 따른 score 변화를 확인해 보는게 좋을 것 같내요.


[field]

- query_string 버전의 단순화 query 이며, 대부분의 parameter 를 사용할 수 있습니다.


[filtered]

- query 결과에 대한 filter 를 적용 합니다.

- 예제가 직관적이라 추가 합니다.

{ "filtered" : { "query" : { "term" : { "tag" : "wow" } }, "filter" : { "range" : { "age" : { "from" : 10, "to" : 20 } } } }

}


[flt : fuzzy like this][flt_field]

- 지정한 field 들에 대해서 like 검색을 지원 합니다.

_ _field 가 들어 간 것은 아래 파라미터 중 fields 만 없습니다.

ParameterDescription
fieldsA list of the fields to run the more like this query against. Defaults to the _all field.
like_textThe text to find documents like it, required.
ignore_tfShould term frequency be ignored. Defaults to false.
max_query_termsThe maximum number of query terms that will be included in any generated query. Defaults to 25.
min_similarityThe minimum similarity of the term variants. Defaults to 0.5.
prefix_lengthLength of required common prefix on variant terms. Defaults to 0.
boostSets the boost value of the query. Defaults to 1.0.
analyzerThe analyzer that will be used to analyze the text. Defaults to the analyzer associated with the field.


[fuzzy]

- Levenshtein algorithm 을 기반으로 하는 유사도 검색을 지원 한다.


[match_all]

- 모든 문서를 출력한다.


[mlt : more like this][mlt_field]

- 지정한 field 들에 대해서 like 검색을 지원 합니다.

- term 단위 like 검색이라는 것을 유의 해야함.

- 결국 지정된 field 에는 term_vector 설정이 되어 있어야 함

ParameterDescription
fieldsA list of the fields to run the more like this query against. Defaults to the _all field.
like_textThe text to find documents like it, required.
percent_terms_to_matchThe percentage of terms to match on (float value). Defaults to 0.3 (30 percent).
min_term_freqThe frequency below which terms will be ignored in the source doc. The default frequency is 2.
max_query_termsThe maximum number of query terms that will be included in any generated query. Defaults to 25.
stop_wordsAn array of stop words. Any word in this set is considered “uninteresting” and ignored. Even if your Analyzer allows stopwords, you might want to tell the MoreLikeThis code to ignore them, as for the purposes of document similarity it seems reasonable to assume that “a stop word is never interesting”.
min_doc_freqThe frequency at which words will be ignored which do not occur in at least this many docs. Defaults to 5.
max_doc_freqThe maximum frequency in which words may still appear. Words that appear in more than this many docs will be ignored. Defaults to unbounded.
min_word_lenThe minimum word length below which words will be ignored. Defaults to 0.
max_word_lenThe maximum word length above which words will be ignored. Defaults to unbounded (0).
boost_termsSets the boost factor to use when boosting terms. Defaults to 1.
boostSets the boost value of the query. Defaults to 1.0.
analyzerThe analyzer that will be used to analyze the text. Defaults to the analyzer associated with the field.

[prefix]

- 매치된 문서는 prefix를 포함한 term 을 갖는다. (not_analyzed)


[query_string]

- query parser 를 이용해서 검색 함.

ParameterDescription
queryThe actual query to be parsed.
default_fieldThe default field for query terms if no prefix field is specified. Defaults to the index.query.default_field index settings, which in turn defaults to _all.
default_operatorThe default operator used if no explicit operator is specified. For example, with a default operator of OR, the query capital of Hungary is translated to capital OR of OR Hungary, and with default operator of AND, the same query is translated to capital AND of AND Hungary. The default value is OR.
analyzerThe analyzer name used to analyze the query string.
allow_leading_wildcardWhen set, * or ? are allowed as the first character. Defaults to true.
lowercase_expanded_termsWhether terms of wildcard, prefix, fuzzy, and range queries are to be automatically lower-cased or not (since they are not analyzed). Default it true.
enable_position_incrementsSet to true to enable position increments in result queries. Defaults to true.
fuzzy_max_expansionsControls the number of terms fuzzy queries will expand to. Defaults to 50
fuzzy_min_simSet the minimum similarity for fuzzy queries. Defaults to 0.5
fuzzy_prefix_lengthSet the prefix length for fuzzy queries. Default is 0.
phrase_slopSets the default slop for phrases. If zero, then exact phrase matches are required. Default value is 0.
boostSets the boost value of the query. Defaults to 1.0.
analyze_wildcardBy default, wildcards terms in a query string are not analyzed. By setting this value to true, a best effort will be made to analyze those as well.
auto_generate_phrase_queriesDefault to false.
minimum_should_matchA percent value (for example 20%) controlling how many “should” clauses in the resulting boolean query should match.
lenientIf set to true will cause format based failures (like providing text to a numeric field) to be ignored. (since 0.19.4).

ParameterDescription
use_dis_maxShould the queries be combined using dis_max (set it to true), or a bool query (set it to false). Defaults to true.
tie_breakerWhen using dis_max, the disjunction max tie breaker. Defaults to 0.

[range]

- string 은 TermRangeQuery

- numeric/date 는 NumericRangeQuery

NameDescription
fromThe lower bound. Defaults to start from the first.
toThe upper bound. Defaults to unbounded.
include_lowerShould the first from (if set) be inclusive or not. Defaults to true
include_upperShould the last to (if set) be inclusive or not. Defaults to true.
gtSame as setting from to the value, and include_lower to false.
gteSame as setting from to the value,and include_lower to true.
ltSame as setting to to the value, and include_upper to false.
lteSame as setting to to the value, and include_upper to true.
boostSets the boost value of the query. Defaults to 1.0.

[term]

- 어떤 term 을 포함한 결과를 갖는다. (term 은 not_analyzed)

- 결국 하나의 word 라고 보면 이해하는데 도움이 됨.


[terms]

- term query 의 IN 절 기능을 제공 한다.


[wildcard]

- * 과 ? 로 매칭을 할 수 있다.

- * 은 SQL 에서 % 와 비슷하며, ? 는 정규표현식의 single character 와 비슷하다.

- 사용 시 주의 할 점은 *, ? 를 첫 시작으로 사용하지 말라는 것이다.


[nested]

- nested mapping 된 parent 문서에 대한 검색을 지원 한다.

- 예제가 직관적이라 추가 합니다.

{
    "type1" : {
        "properties" : {
            "obj1" : {
                "type" : "nested"
            }
        }
    }
} 
{
    "nested" : {
        "path" : "obj1",
        "score_mode" : "avg",
        "query" : {
            "bool" : {
                "must" : [
                    {
                        "match" : {"obj1.name" : "blue"}
                    },
                    {
                        "range" : {"obj1.count" : {"gt" : 5}}
                    }
                ]
            }
        }
    }
}


[indices]

- multiple indices 로 검색을 수행 할 수 있다.

- 역시 예제가 직관적이라 추가 합니다.

{
    "indices" : {
        "indices" : ["index1", "index2"],
        "query" : {
            "term" : { "tag" : "wow" }
        },
        "no_match_query" : {
            "term" : { "tag" : "kow" }
        }
    }
}

- no_match_query 에 none 을 set 하면 no document, all 을 set 하면 match_all 이 된다.



※ Queries 가 너무 많아서 Filters 는 별도로 분리 합니다.


: