多个匹配项可提高词条查询结果的得分

时间:2018-10-23 11:10:43

标签: elasticsearch

我的弹性搜索索引中存储的服务器文档如下所示:

PUT tests
{
  "mappings": {
    "_doc": {
      "dynamic": false,
      "properties": {
        "objects": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword"
            }
          }
        },
        "text": {
          "type": "text"
        }
      }
    }
  }
}

PUT tests/_doc/1
{
  "text": "lel",
  "objects": ["A"]
}

PUT tests/_doc/2
{
  "text": "lol",
  "objects": ["B"]
}

PUT tests/_doc/3
{
  "text": "lil",
  "objects": ["C"]
}

PUT tests/_doc/4
{
  "text": "lul",
  "objects": ["A", "B", "C"]
}

我想使用以下查询来查询对象:

GET _search
{

  "query": {
    "terms": {
      "objects.keyword": ["A", "B", "C"]
    }
  }
}

结果包括我提供的所有三个示例对象。

我的问题很简单,我是否可以使一个具有完全匹配项(对象数组中的所有关键字)并且不仅仅具有部分匹配项的对象(boost)显得更重要({ "took": 4, "timed_out": false, "_shards": { "total": 11, "successful": 11, "skipped": 0, "failed": 0 }, "hits": { "total": 4, "max_score": 1, "hits": [ { "_index": "tests", "_type": "_doc", "_id": "2", "_score": 1, "_source": { "text": "lol", "objects": [ "B" ] } }, { "_index": "tests", "_type": "_doc", "_id": "4", "_score": 1, "_source": { "text": "lul", "objects": [ "A", "B", "C" ] } }, { "_index": "tests", "_type": "_doc", "_id": "1", "_score": 1, "_source": { "text": "lel", "objects": [ "A" ] } }, { "_index": "tests", "_type": "_doc", "_id": "3", "_score": 1, "_source": { "text": "lil", "objects": [ "C" ] } } ] } } ),因为我在弹性搜索文档中找不到任何信息。

这是我目前收到的结果:

git rev-parse HEAD | xargs git name-rev

1 个答案:

答案 0 :(得分:1)

我认为最好的选择是使用boolshould的{​​{1}}查询。

minimum_should_match: 1

结果:

GET _search
{

  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "objects.keyword": "A" 
          }
        },
        {
          "term": {
            "objects.keyword": "B" 
          }
        },
        {
          "term": {
            "objects.keyword": "C" 
          }
        }
      ],
      "minimum_should_match": 1
    }
  }
}

编辑:以下是原因,如文档(https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html)所述:

  

布尔查询采用的是“更好匹配”的方法,因此,每个“必须”或“应该”子句的得分将加在一起,以提供每个文档的最终_score。