小编典典

Elasticsearch完全匹配

elasticsearch

我正在使用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'}

我当然知道“狗”,“那只狗”和“狗”的得分相同,但是我需要弄清楚如何提高得分“狗”的精确匹配度。

我也试过

{
  "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'}

阅读 454

收藏
2020-06-22

共1个答案

小编典典

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

"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字段以查找完全匹配,并查询dog._tokenized进行分析查询(例如全文本)

2020-06-22