b

elasticsearch 에서 must_not random document 검색하기 본문

카테고리 없음

elasticsearch 에서 must_not random document 검색하기

dev.bistro 2019. 7. 5. 10:30

엘라스틱 커뮤니케이서 본 글 ( https://www.facebook.com/groups/elasticsearch.kr/permalink/2277648678987626/ ) 
random-score로 특정 문서 몇개를 추출 하는 방법에 대한 고민이다.

{
  "size": 1,
  "_source" : "type",
  "query": {
    "function_score": {
      "random_score": {}, 
      "query": {
        "bool": {
          "must_not": {
            "term" :{
              "type": "APPROVAL_REQUESTED"
            }
          }
        }
      }
    }
  }
}

이렇게 하여 랜덤으로 문서 1개를 뽑을 수 있을 것 같았다. 하지만 must_not 은 score 를 항상 0 점으로 반환하므로  거기에 random score를 곱해 봤자 모든 문서는 0점으로 으로  원하는 대로 동작하지 않았다. (  링크1)  must_not  score of 0 for all documents is returned )

링크2)와 링크3)을 를 참고하여서 score_mode 에 'sum' 을 활용하면 되지 않을까 생각하였고,

{
  "size": 1,
  "_source" : "type",
  "query": {
    "function_score": {
      "boost": "1",
      "random_score": {}, 
      "boost_mode":"sum",
      "query": {
        "bool": {
          "must_not": {
            "term" :{
              "type": "APPROVAL_REQUESTED"
            }
          }
        }
      }
    }
  }
}

 

위와 같은 쿼리로  must_not 이면서 random document 1개를 뽑을 수 있었다.

 

링크1)  https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html 

 

Bool Query | Elasticsearch Reference [7.2] | Elastic

A query that matches documents matching boolean combinations of other queries. The bool query maps to Lucene BooleanQuery. It is built using one or more boolean clauses, each clause with a typed occurrence. The occurrence types are: The bool query takes a

www.elastic.co

 

링크2) https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html

 

Function Score Query | Elasticsearch Reference [7.2] | Elastic

Keep in mind that taking the log() of 0, or the square root of a negative number is an illegal operation, and an exception will be thrown. Be sure to limit the values of the field with a range filter to avoid this, or use log1p and ln1p.

www.elastic.co

링크3) https://medium.com/horrible-hacks/customizing-scores-in-elasticsearch-for-product-recommendations-9e0d02ce1dbd 

 

Customizing scores in Elasticsearch for product recommendations

Elasticsearch has a really nifty feature called function_score that allows you to modify the scores of documents. It took me a while to…

medium.com

 

Comments