[Elasticsearch] Accessing document field on scripts

Elastic/Elasticsearch 2022. 11. 23. 17:08

아는 것도 시간이 지나면 다 까먹는 나이!!

 

[레퍼런스]

https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting-fields.html#_search_and_aggregation_scripts
https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting-expression.html

 

접근 유형에 따른 성능)

doc value 가 가장 빠르고 _source 와 _fields 는 비슷 합니다.

 

QueryDSL 예제)

GET /kibana_sample_data_ecommerce/_search
{
  "query": {
    "script_score": {
      "query": {
        "bool": {
          "filter": {
            "term": {
              "products.product_name": "shirt"
            }
          }
        }
      },
      "script": {
          "lang": "expression",
          "source": "_score * doc['taxless_total_price']"
        }
    }
  }
}

GET /kibana_sample_data_ecommerce/_search
{
  "query": {
    "script_score": {
      "query": {
        "bool": {
          "filter": {
            "term": {
              "products.product_name": "shirt"
            }
          }
        }
      },
      "script": {
          "lang": "painless",
          "source": "_score * doc['taxless_total_price'].value"
        }
    }
  }
}

GET /kibana_sample_data_ecommerce/_search
{
  "query": {
    "script_score": {
      "query": {
        "bool": {
          "filter": {
            "term": {
              "products.product_name": "shirt"
            }
          }
        }
      },
      "script": {
          "lang": "painless",
          "source": "_score * params._source.taxless_total_price"
        }
    }
  }
}

expression 과 painless 에 따라 다르기 때문에 사용에 주의 하세요.

: