elasticsearch - 根据Elasticsearch中的最佳字段匹配进行搜索

标签 elasticsearch search full-text-search

在下面的文档中搜索“foo bar”将为name*的累积结果计分,但我想获取最匹配字段的分数(即name1分数)。

{
  "name1": "foo bar",
  "name2": "foo",
  "name3": "bar"
}

查询例如:
{
  "query": {
    "filtered": {
      "query": {
        "bool": {
          "should": [{
            "match": {
              "name1": {
                "query": "foo bar"
              }
            }
          }, {
            "match": {
              "name2": {
                "query": "foo bar"
              }
            }
          }, {
            "match": {
              "name3": {
                "query": "foo bar"
              }
            }
          }]
        }
      }
    }
  }
}

使用Elasticsearch 2.4

最佳答案

boolean query合并所有子查询的分数。从文档:

The bool query takes a more-matches-is-better approach, so the score from each matching must or should clause will be added together to provide the final _score for each document.



对于您的情况,您想使用另一个联接查询:The dis_max query

A query that generates the union of documents produced by its subqueries, and that scores each document with the maximum score for that document as produced by any subquery, plus a tie breaking increment for any additional matching subqueries.



例:
{
  "query": {
    "dis_max": {
      "queries": [
        {
          "match": {
            "name1": {
              "query": "foo bar"
            }
          }
        },
        {
          "match": {
            "name2": {
              "query": "foo bar"
            }
          }
        },
        {
          "match": {
            "name3": {
              "query": "foo bar"
            }
          }
        }
      ]
    }
  }
}

*注意,我是在ES6上编写的,它不再具有filtered查询,但我希望您能理解:)

希望这可以帮助!

关于elasticsearch - 根据Elasticsearch中的最佳字段匹配进行搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50138259/

相关文章:

search - 网络搜索点击次数

string - Lua - 得到所有的单词

mysql - 用于 MySQL 全文搜索的转义字符串

python - 在大列表中搜索子字符串

sql - 如何为多语言列创建全文索引?

java - log4j tcp appender 和 logstash 源主机

elasticsearch - 分词器将单词中带有下划线的单词进行拆分,但仍保留完整版本

elasticsearch - 使用给定参数的groupby查询elasticsearch

elasticsearch - Grok解析失败-过滤错误日志时

elasticsearch - 在Elasticsearch中搜索包含某些嵌套对象的文档