我使用的是ES v5.1.2,并且facet的问题是返回布尔字段的错误类型。这是重现和演示问题的最小设置:
from elasticsearch_dsl import DocType, FacetedSearch, TermsFacet
from elasticsearch_dsl.field import Keyword, Integer, Boolean
class Post(DocType):
comment = Keyword()
likes = Integer()
published = Boolean()
class Meta:
index = 'blog'
class PostSearch(FacetedSearch):
index = 'blog'
doc_types = [Post]
fields = 'comment', 'likes', 'published'
facets = {k: TermsFacet(field=k) for k in fields}
现在在索引中创建一些文档,并执行分面搜索:
>>> Post.init()
>>> Post(comment='potato', likes=42, published=True).save()
True
>>> Post(comment='spud', likes=12, published=False).save()
True
>>> Post(comment='foo', likes=7, published=True).save()
True
>>> search = PostSearch()
>>> response = search.execute()
个别回复数据看起来是正确的:
>>> response.hits.total
3
>>> vars(response[0])
{'_d_': {u'comment': u'spud', u'likes': 12, u'published': False},
'meta': {u'index': u'blog', u'score': 1.0, u'id': u'AVofDCdDpUlHAgmQ...}}
>>> response[0].published
False
也就是说,我们在搜索结果中反序列化了Python布尔值。但是,聚合中的数据不正确:
>>> response.facets.to_dict()
{'comment': [(u'foo', 1, False), (u'potato', 1, False), (u'spud', 1, False)],
'likes': [(7, 1, False), (12, 1, False), (42, 1, False)],
'published': [(1, 2, False), (0, 1, False)]}
facet应该是3元组(值,计数,选中)。但是布尔值返回为1和0,它们没有被反序列化,因此前端和我的模板无法区分整数类型和布尔类型。总而言之,预期和实际行为如下所示:
实际行为:
>>> response.facets['published']
[(1, 2, False), (0, 1, False)]
预期行为:
>>> response.facets['published']
[(True, 2, False), (False, 1, False)]
我在这里做错了什么?我们如何使Boolean
字段的构面值在构面中正确反序列化,就像在实际搜索结果中一样?