14

我正在使用elasticsearch,并且有一段时间要进行精确匹配。我尝试了 match、query_string 等的各种组合,但要么一无所获,要么结果不好。查询如下所示:

{
  "filter": {
    "term": {
      "term": "dog",
      "type": "main"
    }
  },
  "query": {
    "match_phrase": {
      "term": "Dog"
    }
  },
  "sort": [
    "_score"
  ]
}

排序结果

10.102211 {u'term': u'The Dog', u'type': u'main', u'conceptid': 7730506}
10.102211 {u'term': u'That Dog', u'type': u'main', u'conceptid': 4345664}
10.102211 {u'term': u'Dog', u'type': u'main', u'conceptid': 144}
7.147442 {u'term': u'Dog Eat Dog (song)', u'type': u'main', u'conceptid': u'5288184'}

我当然看到“The Dog”、“That Dog”和“Dog”都有相同的分数,但我需要弄清楚如何提高精确匹配“Dog”的分数。

我也试过

{
  "sort": [
    "_score"
  ],
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "term": "Dog"
          }
        },
        {
          "match_phrase": {
            "term": {
              "query": "Dog",
              "boost": 5
            }
          }
        }
      ]
    }
  },
  "filter": {
    "term": {
      "term": "dog",
      "type": "main"
    }
  }
}

但这仍然给了我

11.887239 {u'term': u'The Dog', u'type': u'main', u'conceptid': 7730506}
11.887239 {u'term': u'That Dog', u'type': u'main', u'conceptid': 4345664}
11.887239 {u'term': u'Dog', u'type': u'main', u'conceptid': 144}
8.410372 {u'term': u'Dog Eat Dog (song)', u'type': u'main', u'conceptid': u'5288184'}
4

3 回答 3

14

默认情况下使用标准分析器分析字段。如果您想检查完全匹配,您可以存储未分析的字段,例如:

"dog":{
            "type":"multi_field",
            "fields":{
                "dog":{
                    "include_in_all":false,
                    "type":"string",
                    "index":"not_analyzed",
                    "store":"no"
                },
                "_tokenized":{
                    "include_in_all":false,
                    "type":"string",
                    "index":"analyzed",
                    "store":"no"
                }
            }
        }

然后您可以查询 dog-field 以获得精确匹配,并查询 dog._tokenized 用于分析查询(如全文)

于 2013-09-04T09:54:18.530 回答
0

我认为您的问题是该字段term正在使用标准分析器进行分析(检查您的映射),并且正在过滤诸如theor之类的停用词that。因此,您在Dog和上得到相同的分数The Dog因此,也许您可​​以通过配置自定义分析器 =>文档页面来解决您的问题

于 2013-09-03T19:34:34.320 回答
0

将需要搜索的两个值散列到散列键中,然后进行搜索。

于 2017-05-09T02:03:17.130 回答