【问题标题】:Elasticsearch - give negative boost to documents without a certain fieldElasticsearch - 对没有特定字段的文档进行负面提升
【发布时间】:2015-05-07 09:47:50
【问题描述】:

我正在处理一个查询,基本的过滤多重匹配查询正在按计划工作,它返回我想要的文档。

问题是想要提升具有特定字符串字段的结果。 0.5,或者在这个例子中,给没有这个字段'traded_as'的结果一个1.0的负提升。

无法让过滤器 - 提升 - 必须 - 存在/缺少按我的意愿工作。

这是解决这个问题的正确方法吗?

使用弹性搜索 1.5.2

{
"query": {
    "filtered": {
        "query": {
           "multi_match": {
               "query": "something",
               "fields": ["title", "url", "description"]
           }
        },
        "filter": {
           "bool": {
                "must": {
                    "missing": {
                        "field": "marked_for_deletion"
                    }
                }
            }
        }
    }
},
"boosting": {
    "positive": {
        "filter": {
            "bool": {
                "must": {
                    "exists": {
                        "field": "traded_as"                            
                    }
                }
            }
        }
    },
    "negative": {
        "filter": {
           "bool": {
                "must": {
                    "missing": {
                        "field": "traded_as"
                    }
                }
            }
        }
    },
    "negative_boost": 1.0
}
}

【问题讨论】:

  • 您确定可以在同一级别进行查询和提升吗? { "查询": { }, "提升": { } }
  • 我尝试了很多不同的方法,都没有成功。
  • negative_boost 应该小于 1,ES 会按该因子缩小分数。

标签: elasticsearch


【解决方案1】:

您无法获得想要的结果。正如提升查询的文档中所述:

与 bool 查询中的“NOT”子句不同,这仍然会选择包含不需要的词的文档,但会降低它们的总分。

{
  "query": {
    "boosting": {
      "positive": [{
        "filtered": {
          "query": {
            "multi_match": {
              "query": "something",
              "fields": ["title", "url", "description"]
            }
          },
          "filter": {
            "bool": {
              "must": [{
                "missing": {
                  "field": "marked_for_deletion"
                }
              }]
            }
          }
        }
      }],
      "negative": [{
        "filtered": {
          "filter": {
            "missing": {
              "field": "traded_as"
            }
          }
        }
      }],
      "negative_boost": 1.0
    }
  }
}

所以你仍然会有一些不相关的文档,但匹配的文档会有更好的分数。这样你不会对 traded_as 的存在有任何提升。为此,您应该查看功能得分http://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#_using_function_score

你会有类似的东西

{
  "query": {
    "function_score": {
      "query": {
        "filtered": {
          "query": {
            "multi_match": {
              "query": "something",
              "fields": ["title", "url", "description"]
            }
          },
          "filter": {
            "bool": {
              "must": {
                "missing": {
                  "field": "marked_for_deletion"
                }
              }
            }
          }
        }
      },
      "functions": [{
        "filter": {
          "exists": {
            "field": "traded_as"
          }
        },
        "boost_factor": 2
      }, {
        "filter": {
          "missing": {
            "field": "traded_as"
          }
        },
        "boost_factor": 0.5
      }],
      "score_mode": "first",
      "boost_mode": "multiply"
    }
  }
}

【讨论】:

  • 我明白了,但收到“没有为 [missing]] 注册查询”
  • 抱歉有一个错误,我已经更新了查询;)
  • 我在那里但很快 :) 似乎我只得到了交易为字段的文件。我需要用 traded_as 作为增强文档来取回所有东西
  • 好的,我明白了,我已经更新了我的答案。您可以尝试更新提升因子
  • 太棒了 - 必须查看查询才能理解它,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-16
  • 1970-01-01
  • 2013-07-15
  • 1970-01-01
  • 1970-01-01
  • 2012-02-22
  • 1970-01-01
相关资源
最近更新 更多