Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.7k views
in Technique[技术] by (71.8m points)

elasticsearch - Elastic search filter based on array of object

Below is mapping which i have

{
  "defaultBoostValue":1.01,
  "boostDetails": [
    {
      "Type": "Type1",
      "value": 1.0001
    },
    {
      "Type": "Type2",
      "value": 1.002
    },
    {
      "Type": "Type3",
      "value": 1.0005
    }
  ]
}

I want to apply boost type based on type , so if boostType is Type3 then boostFactor should be 1.0005, and if it does not have that boostType, it should apply "defaultBoostValue" as boost below is the query which i have tried

{
  "query": {
    "function_score": {
      "boost_mode": "multiply",
      "functions": [
        {
          "filter": {
            "match": {
              "boostDetails.Type": "Type1"
            }
          },
          "field_value_factor": {
            "field": "boostDetails.value",
            "factor": 1,
            "missing": 1
          }
        }
      ]
    }
  }
}

it is not working as expected as boostDetails is array of object, how can we apply filter on such case


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could use a Script Score function_score query

{
  "query": {
    "function_score": {
      "boost_mode": "multiply",
      "functions": [
        {
          "filter": {
            "match": {
              "boostDetails.Type": "Type1"
            }
          },
          "script_score": {
            "script": {
              "source": """
                double findBoost(Map params_copy) {
                    for (def group : params_copy._source.boostDetails) {
                        if (group['Type'] == params_copy.preferredBoostType ) {
                            return group['value'];
                        }
                    }
                    return params_copy._source['defaultBoostValue'];
                }
                
                return findBoost(params)
              """,
              "params": {
                "preferredBoostType": "Type1"
              }
            }
          }
        }
      ]
    }
  }
}

but keep in mind that if this particular boost type (Type1) does not exist in a given document, the filter of your function_score query will prevent this script from activation -> the defaultBoostValue won't be defaulted to at all.

So what I'd recommend instead is using a match_all filter instead and of course keeping the preferredBoostType:

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

                ...

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

BTW, if your boostDetails array is not of type nested, you'll probably encounter unexpected and seemingly results as explained here and here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...