【问题标题】:Provide a negative score when not matched in Elasticsearch Query在 Elasticsearch 查询中不匹配时提供负分
【发布时间】:2019-10-17 16:26:22
【问题描述】:

我正在处理一个疯狂的需求,疯狂是因为我从未这样做过,而且从外观上看,我认为它不能直接完成。如果有人可以帮助我,请在此平台上寻求帮助。

如果值匹配,则要求在 Elasticsearch 中,然后我必须提供分数,但如果该字段存在但不匹配,则提供负分数,当后者相加时会降低分数。 示例假设我正在搜索两个字段名称和主题。如果名称匹配,则提供的分数为 50,如果主题匹配,则分数为 10。因此,如果两者都匹配,则得分为 60。如果主题不匹配,则得分为 -10,添加时得分为 40。

这在某些编程语言方面很容易,但在 Elasticsearch 查询中无法做到这一点。我正在使用函数评分,下面是示例查询。 感谢您在这方面的任何帮助。

GET dummy_rps_index_2/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "dis_max": {
            "queries": [
              {
                "function_score": {
                  "boost_mode": "replace",
                  "score_mode": "max",
                  "max_boost": 100,
                  "query": {
                    "bool": {
                      "disable_coord": true,
                      "minimum_should_match": 1,
                      "should": [
                        {
                          "function_score": {
                            "score_mode": "max",
                            "boost_mode": "replace",
                            "_name": "name_match",
                            "query": {
                              "multi_match": {
                                "query": "TIFFIN",
                                "fields": [
                                "name"
                                ],
                                "operator": "or",
                                "fuzziness": "AUTO"
                              }
                            },
                            "functions": [
                              {
                                "filter": {
                                  "multi_match": {
                                    "query": "TIFFIN",
                                    "fields": [
                                      "name"
                                    ],
                                    "operator": "or",
                                    "fuzziness": 0
                                  }
                                },
                                "weight": 50
                              }
                            ]
                          }
                        },
                        {
                          "function_score": {
                            "score_mode": "max",
                            "boost_mode": "replace",
                            "_name": "multipleline_code",
                            "query": {
                              "bool": {
                                "disable_coord": true,
                                "minimum_should_match": 1,
                                "should": [
                                  {
                                    "function_score": {
                                      "boost_mode": "replace",
                                      "query": {
                                        "multi_match": {
                                          "query": "maths",
                                          "fields": [
                                            "subject
                                          ]
                                        }
                                      },
                                      "functions": [
                                        {
                                          "weight": 10
                                        }
                                      ]
                                    }
                                  }
                                ]
                              }
                            }
                          }
                        }
                      ]                      
                    }
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

【问题讨论】:

    标签: elasticsearch elastic-stack


    【解决方案1】:

    函数权重中不允许有负权重。 在函数过滤器中使用 must 和 must_not 可以分配不同的分数。

    数据:

    [
          {
            "_index" : "index28",
            "_type" : "_doc",
            "_id" : "bJLH2G0Bti9Q-XOmRxgV",
            "_score" : 1.0,
            "_source" : {
              "name" : "John",
              "subject" : "Math"
            }
          },
          {
            "_index" : "index28",
            "_type" : "_doc",
            "_id" : "bZLH2G0Bti9Q-XOmcxia",
            "_score" : 1.0,
            "_source" : {
              "name" : "John",
              "subject" : "English"
            }
          }
        ]
    

    查询:

    {
      "query": {
        "function_score": {
          "query": {
            "match_all": {}
          },
          "functions": [
            {
              "filter": {
                "bool": {
                  "must": [
                    {
                      "match": {
                        "name": "John"
                      }
                    },
                    {
                      "match": {
                        "subject": "English"
                      }
                    }
                  ]
                }
              },
              "weight": 60
            },
            {
              "filter": {
                "bool": {
                  "must": [
                    {
                      "match": {
                        "name": "John"
                      }
                    }
                  ],
                  "must_not": [
                    {
                      "match": {
                        "subject": "English"
                      }
                    }
                  ]
                }
              },
              "weight": 40
            },
            {
              "filter": {
                "bool": {
                  "must_not": [
                    {
                      "match": {
                        "name": "John"
                      }
                    }
                  ],
                  "must": [
                    {
                      "match": {
                        "subject": "English"
                      }
                    }
                  ]
                }
              },
              "weight": 10
            }
          ],
          "score_mode": "sum"
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-12
      • 2018-04-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多