boosting, score 조작

jade·2026년 1월 28일
post-thumbnail

elastic search에서 검색결과 통계에 사용되는 score 점수를 조정할 수 있는 것들 모음

특정 필드에 가중치 두기

GET blogs/_search
{
	"query": {
		"multi_match": {
			"query": "shay banon",
			"fields": [
				"title^3", // 이 필드에 3배 가중치
				"content"
			]
		}
	}
}

인덱스에 가중치 두기

다중 인덱스 검색시, 특정 인덱스에 포함된 문서들에 가중치 두기

GET blogs*/_search
{
	"indices_boost": [
		{ "blogs-2022": 2.0 }, // 이 인덱스의 점수는 2배
		{ "blogs-2021": 1.5 } // 이 인덱스의 점수는 1.5배
	]
}

점수 고정시키기

특정 조건에 맞는 모든 결과의 점수를 동일하게 고정시킨다.

  • 장점: 조건에 맞는 문서들에 대해 score를 계산해야 하는 시간이 단축됨

일반적인 쿼리는 검색어가 얼마나 자주 등장하는지(TF-IDF나 BM25 알고리즘)에 따라 결과마다 점수가 다 다fmek다. 하지만 constant_score"조건에 맞기만 하면 점수는 무조건 X점!"이라고 못 박는 것이다!

아래 예시는 monica가 작성한 모든 블로그 게시물은 점수를 1.5로 고정한다.

GET blogs/_search
{
	"query": {
		"constant_score": {
			"filter": {
				"term": { "authors.first_name": "monica" }
			},
			"boost": 1.5
} } }

score에 필드값 반영하기

painelss scriping을 사용해서 기존의 필드값으로 score를 계산한다.

ex. 키가 큰 사람에게 가중치 주기

GET my_web_logs/_search
{
	"query": {
		"script_score": {
			"query": {
				"match": { "message": "elasticsearch" }
			},
			"script": {
			//검색결과 문서들의 score를 ‘resp_ms’필드의 값으로 나누어라.
				"source": "_score / doc['resp_ms'].value"
			}
} } }

cf) 쿼리 결과에 대한 설명 출력하기

explain=true 옵션을 통해 검색결과 계산에 대한 자세한 설명을 볼 수 잇다.

GET blogs/_search?explain=true
{
	"query": {
		"multi_match" : {
			"query" : "shay banon",
			"fields" : [ "title^3", "content" ]
		}
	}
}
// 이런식의 explanation 필드가 결과에 추가됨
"_explanation" : {
          "value" : 13.830835,
          "description" : "max of:",
          "details" : [
            {
              "value" : 11.835554,
              "description" : "sum of:",
              "details" : [
                {
                  "value" : 6.22925,
                  "description" : "weight(content:shay in 483) [PerFieldSimilarity], result of:",
                  "details" : [
                    {
                      "value" : 6.22925,
                      "description" : "score(freq=5.0), computed as boost * idf * tf from:",
                      "details" : [
                        {
profile
keep on pushing

0개의 댓글