如何查询未分析的字段?

How to query on unanalyzed fields?

我的 elasticsearch_dsl class 中有几项要查询完全匹配:

class Profile(DocType):
    name = String(fields={'raw': String(index='not_analyzed')})

虽然这确实有效,但我总是需要在查询中添加一个 .raw 并且无法准确查询 name

# Matches "foo" and "foo-1"
Profile.search().filter('term', name='foo'})
# Matches nothing
Profile.search().filter('term', name='foo-1'})
# Matches what i want (only "foo-1")
Profile.search().filter('term', **{'name.raw': 'foo-1'})

这感觉有点不对,因为我应该可以只使用 name 而不是 raw,因为它们应该是一样的。

正确的方法是什么?

不,正确的使用方法是 name.raw,因为那是 not_analyzed 的字段。如果你只使用 name 你没有使用 not_analyzed 版本,你使用 standard 分析器分析的版本。

这就是 filter('term', name='foo'}) 匹配 foofoo-1 的原因。