Elasticsearch - 在没有特定字段的情况下对文档进行负面推动

ram*_*lev 7 elasticsearch

我正在进行查询,基本过滤的多重匹配查询按计划工作,它返回我想要的文档.

问题是想要提升具有某个字符串字段的结果.0.5,或者在这个例子中给出没有该字段'traded_as'的结果为负增益1.0.

无法获得过滤器 - 提升 - 必须 - 存在/缺失按我的意愿工作.

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

使用elasticsearch 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
}
}
Run Code Online (Sandbox Code Playgroud)

Jul*_* C. 2

你不可能得到想要的结果。正如提升查询的文档中所述:

与布尔查询中的“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
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

因此,您仍然会有一些不相关的文档,但匹配的文档会有更好的分数。这样一来,你不会对 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"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)