Elasticsearch Multi Match Query에 대해서 알아보기

쿼리를 시도할때 검색어를 하나의 필드가 아닌 여러개의 필드를 통해 검색을 하고 싶다면 Multi Match Query를 사용하세요.

단, 한번에 검색할 수 있는 필드 수에는 제한이 있으며 기본값은 1024개이며 indices.query.bool.max_clause_count 검색설정에 정의되어있습니다.

만약 검색하려는 필드가 비어있다면 모든 필드를 검색합니다. 단, 매핑타입에 맞는 필드로 한정하는 것 같습니다. 하지만 검색하려는 필드가 비어있을 일은 거의 없겠죠?

example1

curl "localhost:9200/sales-records/_search?pretty" -H "Content-Type:application/json" -d '
{
  "query": {
    "multi_match": {
      "query": "south",
      "fields": ["country", "message"]
    }
  }
}'

“south”라는 검색어를 country, message필드에서 검색합니다.

{
  "took" : 23,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10000,
      "relation" : "gte"
    },
    "max_score" : 4.295763,
    "hits" : [
      ...
    ]
  }
}

위 예제는 기본적인 Multi Match Query입니다.

이외에도 유형을 정의하여 검색을 할 수 있습니다.

best_fields

best_fields는 동일한 필드에서 여러 단어를 검색할때 유용합니다. 그리고 정확도가 높은 필드의 score를 사용하지만 tie_breaker가 지정되 있다면 정확도가 높은 필드의 _score + 다른 모든 필드의 (tie_breaker * _score)로 score를 계산합니다.

best_fields의 동작방식은 각 필드에 대해 match query를 생성하고 dis_max 쿼리에서 래핑하여 가장 일치하는 단일 필드를 찾습니다.

curl "localhost:9200/sales-records/_search?pretty" -H "Content-Type:application/json" -d '
{
  "query": {
    "multi_match": {
      "query": "south",
      "fields": ["country", "*ssage"],
      "type": "best_fields",
      "tie_breaker":0.3
    }
  }
}'

위와 같은 쿼리는 다음과 같습니다.

curl "localhost:9200/sales-records/_search?pretty" -H "Content-Type:application/json" -d '
{
  "query": {
    "dis_max": {
      "queries": [
        { "match": { "country": "south" }},
        { "match": { "message": "south" }}
      ],
      "tie_breaker": 0.3
    }
  }
}'

most_fields

most_fields는 다른 방식으로 같은 텍스트를 analyze 해서 여러 필드를 쿼리할때 유용합니다.

예를들어 필드가 다른 형태소 분석기를 쓰는 경우입니다.

cross_fields

cross_fields는 모든 필드를 하나의 필드 처럼 보고 검색을 진행합니다.

$ curl "localhost:9200/sales-records/_search?pretty" -H "Content-Type:application/json" -d '
{
  "query": {
    "multi_match": {
      "query": "asia korea",
      "type": "cross_fields",
      "operator": "and",
      "fields": ["region", "country"]
    }
  }
}'

phrase

각 필드에서 match_phrase 쿼리를 실행하고 최상의 필드에서 _score를 사용합니다.

phrase_prefix

각 필드에서 match_phrase_prefix 쿼리를 실행하고 각 필드의 _score를 사용합니다.

참고사이트

  1. https://gist.github.com/einsub/86638954d3d6168bdfbe772c3f3d3d90
  2. https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html#type-best-fields



Enjoy Reading This Article?

Here are some more articles you might like to read next:

  • 백엔드 개발자가 리액트 네이티브로 달력 앱을 배포해보았습니다.
  • MySQL 실시간 쿼리 확인하기
  • MySQL 트랜잭션 격리수준 Isolation level 알아보기
  • 레디스에 대한 간단한 설명과 성능향상시키기
  • Apple login 사용해보기