[ElasticSearch] rank_feature field 타입, 쿼리

Woong·2023년 6월 27일
0

ElasticSearch

목록 보기
19/21

rank_feature field

  • 숫자 값을 인덱싱하여, rank_feature query 에서 boost 하는데 활용할 수 있는 타입
    • 양수 (positive number) 값 1개만 지정 가능
    • 여러 값 지정 불가, 음수 불가
      • 0 도 지정 불가능. (아래와 같은 오류 발생)
        • featureValue must be a positive normal float, got: 0.0 for feature features.purchase_rank.rank_feature on field _feature which is less than the minimum positive normal float: 1.17549435E-38'
  • rank_feature 쿼리에서만 사용 가능
    • query/sort/aggregate 불가
      • long 또는 integer 타입 필드의 하위 필드로 rank_feature 타입 필드를 지정하여 우회 가능
  • positive_score_impact: true 일 경우 높을 수록 boost (기본값)
    • false 일 경우 값이 작을 수록 boost
PUT my-index-000001
{
  "mappings": {
    "properties": {
      "pagerank": {
        "type": "rank_feature" 
      },
      "url_length": {
        "type": "rank_feature",
        "positive_score_impact": false 
      }
    }
  }
}

PUT my-index-000001/_doc/1
{
  "pagerank": 8,
  "url_length": 22
}

GET my-index-000001/_search
{
  "query": {
    "rank_feature": {
      "field": "pagerank"
    }
  }
}

rank_features field type

  • rank_feature 타입과 유사하지만, 개별 필드를 지정하기엔 애매한 경우에 사용
  • rank_feature query 와 term query 에 사용 가능
    • term query 에선 boost 에 rank_features feature value 가 곱해진 값이 스코어링
PUT my-index-000001
{
  "mappings": {
    "properties": {
      "topics": {
        "type": "rank_features" 
      },
      "negative_reviews" : {
        "type": "rank_features",
        "positive_score_impact": false 
      }
    }
  }
}

PUT my-index-000001/_doc/1
{
  "topics": { 
    "politics": 20,
    "economics": 50.8
  },
  "negative_reviews": {
    "1star": 10,
    "2star": 100
  }
}

PUT my-index-000001/_doc/2
{
  "topics": {
    "politics": 5.2,
    "sports": 80.1
  },
  "negative_reviews": {
    "1star": 1,
    "2star": 10
  }
}

GET my-index-000001/_search
{
  "query": { 
    "rank_feature": {
      "field": "topics.politics"
    }
  }
}

GET my-index-000001/_search
{
  "query": { 
    "rank_feature": {
      "field": "negative_reviews.1star"
    }
  }
}

rank_feature query

  • rank_feature, rank_features 필드의 숫자(numeric) 값을 통해 boost 하는

  • should 구문의 bool 쿼리에 들어가 relevance (관련성) score 에 사용하는 편

  • positive_score_impact 를 false 로 지정한 경우 해당 필드가 없는 document보다 필드값이 존재하는 document가 boost되기 때문에, 모든 document 에 해당 필드값을 지정하는 것이 좋음

  • track_total_hits 를 true 로 설정하였을 때, function_score 등 다른 relevance score 에 영향을 주는 쿼리보다 효율이 높다.

    • track_total_hits 는 쿼리에 매칭되는 hits 수를 추적하는 방법에 대한 설정
      • true 이면 정확하게 추적
        • 성능 저하 이슈 있음
      • N개로 값을 지정할 경우 N개까지만 정확히 추적 (기본값 10,000)
      • 검색 결과 상위 N개만 중요하고 전체 hits 수에 관심없다면 false 로 설정
  • rank_feature 계산용 함수로 Saturation, Logarithm, Sigmoid, Linear 지원

PUT /test
{
  "mappings": {
    "properties": {
      "pagerank": {
        "type": "rank_feature"
      },
      "url_length": {
        "type": "rank_feature",
        "positive_score_impact": false
      },
      "topics": {
        "type": "rank_features"
      }
    }
  }
}
GET /test/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "content": "2016"
          }
        }
      ],
      "should": [
        {
          "rank_feature": {
            "field": "pagerank"
          }
        },
        {
          "rank_feature": {
            "field": "url_length",
            "boost": 0.1
          }
        },
        {
          "rank_feature": {
            "field": "topics.sports",
            "boost": 0.4
          }
        }
      ]
    }
  }
}

Saturation

  • S / (S + pivot)
    • pivot 값을 주지 않을 경우, index 에서 기하 평균(geometric mean) 근사치를 계산하여 사용
    • 항상 0~1 사이의 값이고, S > pivot 이면 0.5 이상, S < pivot 이면 0.5 이하의 값이 됨
GET /test/_search
{
  "query": {
    "rank_feature": {
      "field": "pagerank",
      "saturation": {
        "pivot": 8
      }
    }
  }
}

Logarithm

  • log(scaling_factor + S)
    • scaling factor 를 더한 후 자연로그(lnln)를 취한 값
GET /test/_search
{
  "query": {
    "rank_feature": {
      "field": "pagerank",
      "log": {
        "scaling_factor": 4
      }
    }
  }
}

Sigmoid

  • S^exp^ / (S^exp^ + pivot^exp^)
    • S 는 pivot
    • exponent는 0.5~1 사이의 양수
GET /test/_search
{
  "query": {
    "rank_feature": {
      "field": "pagerank",
      "sigmoid": {
        "pivot": 7,
        "exponent": 0.6
      }
    }
  }
}

Linear

  • S
    • rank_feature 필드값 그대로 계산
    • "positive_score_impact": true 일 경우 S,
    • "positive_score_impact": false 일 경우 1/S
GET /test/_search
{
  "query": {
    "rank_feature": {
      "field": "pagerank",
      "linear": {}
    }
  }
}

reference

0개의 댓글