简体   繁体   English

Elasticsearch布尔型面返回错误的类型

[英]Elasticsearch boolean facets returned as wrong type

I'm using ES v5.1.2 and having an issue with the facets returning incorrect types for boolean fields. 我正在使用ES v5.1.2,并且构面在返回布尔字段的错误类型时遇到问题。 Here's a minimal setup to reproduce and demonstrate the problem: 这是重现和演示问题的最小设置:

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}

Now create some documents in the index, and execute a faceted search: 现在在索引中创建一些文档,并执行多面搜索:

>>> 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()

The individual response data looks correct: 各个响应数据看起来正确:

>>> 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

That is, we have deserialized Python booleans on the search results. 也就是说,我们在搜索结果中反序列化了Python布尔值。 However, the data in the aggregations is incorrect: 但是,聚合中的数据不正确:

>>> 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)]}

The facets should be 3-tuples of (value, count, selected). 构面应为3个元组(值,计数,已选择)。 But boolean values come back as 1 and 0, they weren't deserialized, so the frontend and my templates are not able to distinguish an integer type from a boolean type. 但是布尔值返回为1和0,它们没有进行反序列化,因此前端和我的模板无法区分整数类型和布尔类型。 To summarise, the expected and actual behaviour are shown below: 总而言之,预期和实际行为如下所示:

Actual behaviour: 实际行为:

>>> response.facets['published']
[(1, 2, False), (0, 1, False)]

Expected behaviour: 预期的行为:

>>> response.facets['published']
[(True, 2, False), (False, 1, False)]

What am I doing wrong here? 我在这里做错了什么? How can we make the facet values for a Boolean field deserialize correctly in the facets, as they do in the actual search results? 我们如何使Boolean字段的构面值在构面中正确反序列化,就像在实际搜索结果中一样?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM