Elasticsearch search

By | 2020년 6월 22일
Table of Contents

Elasticsearch search

match_all, match, match_phrase 에 대해 정리합니다.

match_all

모든 문서와 매칭됩니다. 검색이외에 아무 계산도 하지 않으므로 속도가 빠릅니다.

curl -XPOST 'localhost:9200/items/_search?pretty' -H 'Content-Type: application/json' -d'{
  "query": {
      "match_all": {}
  }
}'

_source 의 일부만 반환하게 지정할 수 있습니다.

curl -XPOST 'localhost:9200/items/_search?pretty' -H 'Content-Type: application/json' -d'{
  "query": {
      "match_all": {}
  },
  "_source": ["itemname", "viewkeywords", "buykeywords"]
}'

페이징을 지원합니다.

curl -XPOST 'localhost:9200/items/_search?pretty' -H 'Content-Type: application/json' -d'{
  "query": {
      "match_all": {}
  },
  "from": 2,
  "size": 2
}'

정렬순서를 변경할 수 있습니다.

curl -XPOST 'localhost:9200/items/_search?pretty' -H 'Content-Type: application/json' -d'{
  "query": {
      "match_all": {}
  },
  "sort": [
    {"lastupdate": "desc"}
  ]
}'

match

match 는 검색어를 형태소분석 후 나온 단어중 하나라도 일치하는 문서를 반환합니다.

curl -XPOST 'localhost:9200/items/_search?pretty' -H 'Content-Type: application/json' -d'
{
  "query": {
    "match": {
      "itemname": "아이폰케이스"
    }
  }
}'

아래의 쿼리로 형태소분석된 모든 단어가 등장하는 문서를 검색하도록 할 수 있습니다.

curl -XPOST 'localhost:9200/items/_search?pretty' -H 'Content-Type: application/json' -d'
{
  "query": {
    "match": {
      "itemname": {
        "query": "아이폰케이스",
        "operator": "and"
      }
    }
  }
}'

하위 필드를 검색하기 위해서는 아래와 같이 itemname.ko 를 입력하면 됩니다.

curl -XPOST 'localhost:9200/items/_search?pretty' -H 'Content-Type: application/json' -d'
{
  "query": {
    "match": {
      "itemname.ko": "아이폰케이스"
    }
  }
}'

multi_match

여러 필드를 한번에 검색할 수 있습니다.

curl "localhost:9200/items/_search?pretty" -H "Content-Type:application/json" -d '{
  "query": {
    "multi_match": { 
      "query": "쉬폰블라우스", 
      "fields": ["itemname.ko", "itemname.en"]
    }
  }
}'

match_phrase

match_phrase 는 검색어를 형태소분석 후 나온 단어가 모두 등장하고 순서도 일치하는 문서를 반환합니다.

curl -XPOST 'localhost:9200/items/_analyze?pretty' -H 'Content-Type: application/json' -d'
{
  "analyzer": "korean",
  "text": "아이폰케이스"
}'

curl -XPOST 'localhost:9200/items/_search?pretty' -H 'Content-Type: application/json' -d '{
  "query": {
    "match_phrase": {
      "itemname": "아이폰케이스"
    }
  }
}'

_termvectors

_termvectors 를 이용해 필드에서 가지고 있는 term 을 확인할 수 있습니다.

curl -XGET 'localhost:9200/items/_doc/1/_termvectors?fields=itemname&pretty'

bool 쿼리

must, must_not, should, filter 를 이용해 복잡한 쿼리를 생성할 수 있습니다.

curl -XGET 'localhost:9200/items/_search?pretty' -H 'Content-Type: application/json' -d '{
  "query": {
    "bool": {
      "must": [
        { <쿼리> }, …
      ],
      "must_not": [
        { <쿼리> }, …
      ],
      "should": [
        { <쿼리> }, …
      ],
      "filter": [
        { <쿼리> }, …
      ]
    }
  }
}'

must

매칭하는 문서를 반환합니다.

curl -XGET 'localhost:9200/items/_search?pretty' -H 'Content-Type: application/json' -d '{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "itemname": "아이폰"
          }
        },
        {
          "match_phrase": {
            "itemname": "아이폰케이스"
          }
        }
      ]
    }
  }
}'

must_not

매칭하지 않는 문서를 반환합니다.

curl -XGET 'localhost:9200/items/_search?pretty' -H 'Content-Type: application/json' -d '{
  "query": {
    "bool": {
      "must_not": [
        {
          "match": {
            "itemname": "아이폰"
          }
        },
        {
          "match_phrase": {
            "itemname": "아이폰케이스"
          }
        }
      ]
    }
  }
}'

should

매칭하는 문서의 score 를 올려줍니다.

curl -XGET 'localhost:9200/items/_search?pretty' -H 'Content-Type: application/json' -d '{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "itemname": "아이폰"
          }
        }
      ],
      "should": [
        {
          "match_phrase": {
            "itemname": "핑크"
          }
        }
      ]
    }
  }
}'

filter

매칭하는 문서를 반환하지만 score 를 계산하지 않습니다.

curl -XGET 'localhost:9200/items/_search?pretty' -H 'Content-Type: application/json' -d '{
  "query": {
    "bool": {
      "must": { "match_all": {} },
      "filter": [
        {
          "match": {
            "itemname": "아이폰"
          }
        }
      ],
      "must": [
        {
          "match": {
            "itemname": "아이폰"
          }
        }
      ],
      "should": [
        {
          "match_phrase": {
            "itemname": "핑크"
          }
        }
      ]
    }
  }
}'

캐시가 되므로, 다른 검색기능에 앞서 사용하면 속도개선에 도움이 됩니다.

curl -XGET 'localhost:9200/items/_search?pretty' -H 'Content-Type: application/json' -d '{
  "query": {
    "bool": {
      "must": { "match_all": {} },
      "filter": [
        {
          "match": {
            "itemname": "wpc장우산"
          }
        }
      ],
      "must": [
        {
          "match": {
            "itemname": "검정장우산"
          }
        }
      ],
      "should": [
        {
          "match_phrase": {
            "itemname": {
              "query": "wpc",
              "slop":  2
            }
          }
        }
      ]
    }
  }
}'

범위 쿼리

curl -XGET 'localhost:9200/items/_search?pretty' -H 'Content-Type: application/json' -d '{
  "query": {
    "range": {
      "price": {
        "gte": 700,
        "lt": 900
      }
    }
  }
}'
curl -XGET 'localhost:9200/items/_search?pretty' -H 'Content-Type: application/json' -d '{
  "query": {
    "range": {
      "date": {
        "gt": "2016-01-01"
      }
    }
  }
}'

아래와 같이 타임존을 지정할 수 있습니다.

curl -XGET 'localhost:9200/items/_search?pretty' -H 'Content-Type: application/json' -d '{
  "query": {
    "range": {
      "date": {
        "gt": "2020-06-30T15:00:00+09:00"
      }
    }
  }
}'

답글 남기기