elasticsearch

elasticsearch similarity module 이용해서 score 수정하기

천천히올라가자 2021. 2. 15. 21:51
반응형

 

오늘 내가 필요했던 요구사항은 해당 필드에 해당 키워드가 존재하면 그 키워드가 존재하는 문서의 score 값이

동일하게 나오기를 바랬다

브랜드
무신사 스탠다드
엘무드
커버낫

위의 표처럼 브랜드가 있을 때 "무"라는 키워드로 검색하면 무, 무신사 스탠다드, 엘무드 이 세가지 값이 나오게 되는데 해당 3가지의 score이 tf-idf에 의해서 값이 다 다른데 무, 무신사, 엘무드를 포함하는 문서는 15000이라는 값을 상수로 주고 싶었다

 

 * 사실 요구사항이 더 많았지만 간략하게 기능 소개만 하기 위해 간단하게 예제를 설정

 

그래서 이것 저것 시도해보다 안되서 나와 같은 니즈를 가진 사람들이 있을 것이라고 생각해

검색해 보다가 내가 커스텀해서 score를 계산할 수 있도록 해주는 similarity module 이 있었다

 

https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules-similarity.html

 

Similarity module | Elasticsearch Reference [7.11] | Elastic

While scripted similarities provide a lot of flexibility, there is a set of rules that they need to satisfy. Failing to do so could make Elasticsearch silently return wrong top hits or fail with internal errors at search time:

www.elastic.co

위와 같은 것이 있었고 실제로 적용했더니 내가 원했던 결과가 나올 수 있게 되었다

 

"settings": {
  "similarity": {
      "scripted_boost_score": {
          "type": "scripted",
          "script": {
              "source": "return query.boost;"
          }
      }
  }
}

"mappings": {
    "properties": {
    	"ngram_gaga": {
          "search_analyzer": "search_query_keyword",
          "analyzer": "search_index_ngram",
          "type": "text",
          "similarity": "scripted_boost_score"
        }
    }
}

 

query를 작성하여 요청하였더니

 

위의 script에서 query.boost만 사용하겠다고 했더니 아래 docCount, sumDocFreq 등등을 가지고 연산하지 않고 15000 값만 반환되었다

 
반응형