elasticsearch 基于对象数组的弹性搜索滤波器,elasticsearch,kibana,elasticsearch-7,elasticsearch,Kibana,Elasticsearch 7" /> elasticsearch 基于对象数组的弹性搜索滤波器,elasticsearch,kibana,elasticsearch-7,elasticsearch,Kibana,Elasticsearch 7" />

elasticsearch 基于对象数组的弹性搜索滤波器

elasticsearch 基于对象数组的弹性搜索滤波器,elasticsearch,kibana,elasticsearch-7,elasticsearch,Kibana,Elasticsearch 7,下面是我的地图 { "defaultBoostValue":1.01, "boostDetails": [ { "Type": "Type1", "value": 1.0001 }, { "Type": "Type2", "value": 1.002

下面是我的地图

{
  "defaultBoostValue":1.01,
  "boostDetails": [
    {
      "Type": "Type1",
      "value": 1.0001
    },
    {
      "Type": "Type2",
      "value": 1.002
    },
    {
      "Type": "Type3",
      "value": 1.0005
    }
  ]
}
我想基于类型应用boost类型,所以如果boostType是Type3,那么boostFactor应该是1.0005,如果它没有boostType,那么应该应用“defaultBoostValue”作为boost 下面是我尝试过的问题

{
  "query": {
    "function_score": {
      "boost_mode": "multiply",
      "functions": [
        {
          "filter": {
            "match": {
              "boostDetails.Type": "Type1"
            }
          },
          "field_value_factor": {
            "field": "boostDetails.value",
            "factor": 1,
            "missing": 1
          }
        }
      ]
    }
  }
}
由于boostDetails是一个对象数组,所以它不能按预期工作,我们如何在这种情况下应用过滤器

您可以使用

但是请记住,如果给定文档中不存在此特定的boost类型(
Type1
),则
函数的
过滤器
得分
查询将阻止此脚本激活->默认boostvalue将不会被默认为

因此,我建议改为使用
match\u all
过滤器,当然要保留
preferredBoostType

{
  "query": {
    "function_score": {
      "boost_mode": "multiply",
      "functions": [
        {
          "filter": {
            "match_all": {}                          <---
          },
          "script_score": {
            "script": {
              "source": """

                ...

              """,
              "params": {
                "preferredBoostType": "Type1"
              }
            }
          }
        }
      ]
    }
  }
}
{
“查询”:{
“功能评分”:{
“增压模式”:“倍增”,
“职能”:[
{
“过滤器”:{
“全部匹配”:{}
{
  "query": {
    "function_score": {
      "boost_mode": "multiply",
      "functions": [
        {
          "filter": {
            "match_all": {}                          <---
          },
          "script_score": {
            "script": {
              "source": """

                ...

              """,
              "params": {
                "preferredBoostType": "Type1"
              }
            }
          }
        }
      ]
    }
  }
}