점점 심화되는 elasticsearch 과정..ㅠ_ㅠ 이번에는 muti_match 쿼리를 사용해볼 일이 있어서 살펴보다가 정리도 해야겠다 싶어서 작성하게 되었다.
다중 필드에 쿼리를 한번에 날려서 질의를 하기 위한 match 쿼리이다.
GET /_search
{
"query": {
"multi_match" : {
"query": "this is a test",
"fields": [ "subject", "message" ]
}
}
}
각 필드에 wildcard 사용도 가능하고 boost도 주어 가중치도 줄 수 있다.
GET /_search
{
"query": {
"multi_match" : {
"query": "this is a test",
"fields": [ "*_name", "message^3" ]
// "frist_name", "last_name" 등의 필드에 검색
// "message" 필드에 3배 가중치
}
}
}
best_fields | (default) 모든 Field에 대해 Match되는 Field를 찾는다. 가장 잘 Match되는 1개의 Field를 기준으로 Score를 계산한다. |
---|---|
most_fields | 모든 Field에 대해 Match되는 각각의 field에 대해 점수를 계산하고 모두 합한다. 합한 점수를 field 개수로 나눠서 최종 스코어를 계산한다. |
cross_fields | analyzer필드를 하나의 큰 필드인 것처럼 동일하게 처리한다 . 모든 필드 에서 각 단어를 찾는다 . |
phrase | match_phrase각 필드에 대해 쿼리를 실행 하고 _score 최상의 필드에서 사용한다. |
phrase_prefix | match_phrase_prefix각 필드에 대해 쿼리를 실행 하고 _score최상의 필드에서 사용한다. |
bool_prefix | match_bool_prefix각 필드에 대한 쿼리를 생성 하고 _score각 필드의 쿼리를 결합한다. |
→ 여기서 내가 눈여겨보는 best_fields과 most_fields를 자세히 알아보도록 하겠다.
best_fields
기본 설정값이며 모든 Field에 대해 match되는 field를 찾는다. best_fields 는 각 필드에 대한 match 쿼리를 생성하고 dis_max 쿼리로 래핑하여 가장 적합한 단일 필드를 찾는다.
GET /_search
{
"query": {
"multi_match" : {
"query": "brown fox",
"type": "best_fields",
"fields": [ "subject", "message" ],
"tie_breaker": 0.3
}
}
}
// 위와 같은 쿼리
GET /_search
{
"query": {
"dis_max": {
"queries": [
{ "match": { "subject": "brown fox" }},
{ "match": { "message": "brown fox" }}
],
"tie_breaker": 0.3
}
}
}
계산방법 : 일반적으로 가장 일치하는 단일필드 의 점수를 사용 하지만 tie_breaker
가 있는 경우 다음과 같이 점수를 계산하게 된다.
→ 가장 일치하는 필드의 점수 + 나머지 필드에 대한 tie_breaker * _score
*analyzer
, boost
, operator
, minimum_should_match
, fuzziness
, lenient
, prefix_length
, max_expansions
, fuzzy_rewrite
, zero_terms_query
, auto_generate_synonyms_phrase_query
fuzzy_transpositions
사용도 허용된다.
most_fields
모든 Field에 대해 Match되는 각 각의 field에 대해 점수를 계산하고 모두 합한다.
GET /_search
{
"query": {
"multi_match" : {
"query": "quick brown fox",
"type": "most_fields",
"fields": [ "title", "title.original", "title.shingles" ]
}
}
}
// 위와 같은 쿼리
GET /_search
{
"query": {
"bool": {
"should": [
{ "match": { "title": "quick brown fox" }},
{ "match": { "title.original": "quick brown fox" }},
{ "match": { "title.shingles": "quick brown fox" }}
]
}
}
}
계산방법 : 각 각의 Match되는 field에 대해 점수를 계산 (bool 쿼리와 유사)
*analyzer
, boost
, operator
, minimum_should_match
, fuzziness
, lenient
, prefix_length
, max_expansions
, fuzzy_rewrite
, zero_terms_query
사용도 허용된다.
참고: https://jangseongwoo.github.io/elasticsearch/elasticsearch_multi_match/ , https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html