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

오늘은 Match Phrase Query란 무엇인가에 대해서 알아보겠습니다.

match_phrase query는 text를 분석하고 분석된 텍스트를 phrase query로 만듭니다.

match query는 띄어쓰기가 포함된 경우 analyze 하면 띄어쓰기로 token을 생성하여 token 중 하나라도 일치하면 document에 포함한다.

반면, match_phrase query는 띄어쓰기를 포함한 정확한 검색을 하고 싶을때 사용하면 된다.

match query 샘플

curl "localhost:9200/sales-records/_search?pretty" -H "Content-Type:application/json" -d '
{
  "query": {
    "match": {
      "message": {
        "query":"canada,snacks/online,c,9/13"      }
    }
  }}'

match query로 할 경우 결과는 10,000개로 나왔네요.

그 이유는 검색어를 ”,”로 analyze하여 token을 생성하고 각 token이 포함된 document를 결과로 받아오기 때문입니다.

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

match_phrase query 샘플

curl "localhost:9200/sales-records/_search?pretty" -H "Content-Type:application/json" -d '
{
  "query": {
    "match_phrase": {
      "message": {
        "query":"canada,snacks/online,c,9/13"      }
    }
  }}'

match_phrase query”,”로 analyze하지 않아 canada,snacks/online,c,9/13 토큰 그대로 검색을 합니다. 결과는 1개만 나왔습니다.

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 15.147113,
    "hits" : [
      ...
    ]
  }
}

검색어에 대해서 유사검색은 match query를 검색어가 정확하게 포함된 document를 원할 경우 match_phrase query를 사용하면 좋을 것 같습니다.




Enjoy Reading This Article?

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

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