Elastic Search-속성에 대한 가중치 기반

Aug 21 2020

Elastic Search에서 검색 쿼리에 사용 된 속성이 아닌 다른 속성을 기반으로 결과에 가중치를 부여하는 방법이 있습니까? 예를 들어, '이름'필드를 검색하지만 'with_pictures'가있는 모든 문서는 실제 가중치가 더 높습니다.

답변

2 ESCoder Aug 21 2020 at 09:08

개별 필드에서 boost 매개 변수를 사용하여 쿼리 시간에 자동으로 부스트 (관련성 점수에 더 많이 계산)되는 부스트 를 사용할 수 있습니다.

인덱스 데이터, 매핑 및 검색 쿼리로 작업 예제 추가

인덱스 매핑 :

{
  "mappings": {
    "properties": {
      "with_pictures": {
        "type": "boolean",
        "boost": 2 
      },
      "name": {
        "type": "keyword"
      }
    }
  }
}

인덱스 데이터 :

{
    "name": "A",
    "with_pictures": false
}

{
    "name": "A",
    "with_pictures": true
}
{
    "name": "B",
    "with_pictures": true
}

검색어:

{
  "query": {
    "bool": {
      "minimum_should_match": 1,
      "should": [
        {
          "bool": {
            "should": [

              {
                "term": {
                  "name": "A"
                }
              },
              {
                "term": {
                  "with_pictures": true
                }
              }
            ]
          }
        }
      ]
    }
  }
}

검색 결과:

 "hits": [
      {
        "_index": "fd_cb1",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.4100108,
        "_source": {
          "name": "A",
          "with_pictures": true
        }
      },
      {
        "_index": "fd_cb1",
        "_type": "_doc",
        "_id": "3",
        "_score": 0.9400072,
        "_source": {
          "name": "B",
          "with_pictures": true
        }
      },
      {
        "_index": "fd_cb1",
        "_type": "_doc",
        "_id": "2",
        "_score": 0.4700036,
        "_source": {
          "name": "A",
          "with_pictures": false
        }
      }
    ]

두 조건을 만족하는 문서의 점수 namewith_properties가장 높은 점수를 가지고있다. 그러나 문서가 필요 name: B하고 with_pictures: true보다 높은 점수를 name: A하고 with_pictures: false있기 때문에에 적용되는 부스트 (with_pictures

쿼리 로 검색되는 문서의 점수를 수정할 수있는 함수 점수 쿼리 를 참조 할 수도 있습니다.