簡體   English   中英

如何在ElasticSearch中比較同一文檔中的兩個字段?

[英]How to compare two fields in the same document in ElasticSearch?

我正在使用ElasticSearch 7.2.0,
我在具有三個字段的索引中有文檔id, field1, field2
我想查詢並返回其field1 > 20 AND field1 > field2那些文檔

以下是索引具有的數據- 在此處輸入圖片說明

以下是我正在嘗試的查詢-

GET /test/_search
 {
    "query": {
        "function_score": {
            "query": {
                "constant_score": {
                    "filter": {
                        "bool": {
                            "must": [
                              {
                              "range" : {
                      "field1" : {
                          "gte" : 20
                      }
                  }
                            },
                              {
                                "script": {
                  "source": "doc['field1'].value > doc['field2'].value",
                "params": {
                }
              }}
                            ]
                        }
                    }
                }
            }
        }
    }
}

以下是錯誤-

{
  "error": {
    "root_cause": [
      {
        "type": "parsing_exception",
        "reason": "[script] query does not support [source]",
        "line": 18,
        "col": 29
      }
    ],
    "type": "parsing_exception",
    "reason": "[script] query does not support [source]",
    "line": 18,
    "col": 29
  },
  "status": 400
}

您的查詢是正確的! 您只需要將script包裝在另一個script 第一個是表示script查詢,第二個是實際定義腳本:

{
  "query": {
    "function_score": {
      "query": {
        "constant_score": {
          "filter": {
            "bool": {
              "must": [
                {
                  "range": {
                    "field1": {
                      "gte": 20
                    }
                  }
                },
                {
                  "script": {
                    "script": {
                      "source": "doc['field1'].value > doc['field2'].value",
                      "params": {}
                    }
                  }
                }
              ]
            }
          }
        }
      }
    }
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM