弹性搜索精确匹配

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'}
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进行分析(例如全文)


3
这是否需要更改所有记录?我的索引中有近50,000,000条(很少)记录。还是删除索引并按适当的结构重新导入更合理?当我把数据存储在这样的结构中时,我的ES查询会如何改变?谢谢你的帮助!我想询问关于设计我的索引的最佳方法,但我会将其作为另一个问题提出。 - Josh Harrison
这正是我所需要的,最终我还是重建了数据,因为我想以不同的方式获取它,但我将其作为我的方法的基础。谢谢! - Josh Harrison

0
我认为你的问题在于字段term正在使用标准分析器进行分析(请检查你的映射),并且正在过滤停用词,例如thethat。因此,你会得到DogThe Dog相同的分数。因此,也许你可以通过配置自定义分析器来解决你的问题 => 文档页面

-1

将两个需要搜索的值哈希为哈希键,然后进行搜索。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接