【问题标题】:multiple underscores in elasticsearch field name with python elasticsearch-dsl使用python elasticsearch-dsl的elasticsearch字段名称中的多个下划线
【发布时间】:2018-07-03 00:34:29
【问题描述】:

我试图理解为什么带有单个下划线 mods_genre 的字段的行为与带有 1+ 下划线的字段的行为不同,例如mods__genre 使用python时elasticsearch-dsl client

使用 ElasticSearch 版本 5.5.1 和 python 3.5

以下是我正在使用的代码,用于选择字段与值匹配的所有文档。

此示例正在搜索索引foo,其字段名称只有一个下划线,并按预期返回结果(我已确认此字段填充了此值):

# query against index with single underscores in field name
query = Search(using=es_handle, index='foo')
query = query.filter(Q('term', **{'%s.keyword' % 'mods_genre' : 'biography'}))
query_results = query.execute()

In [16]: query_results.hits.total
Out[16]: 6

但是,使用非常相似的代码,但查询具有连续多个下划线的字段名称的索引bar,我得到的结果为零:

# query against index with multiple underscores in field name
query = Search(using=es_handle, index='bar')
query = query.filter(Q('term', **{'%s.keyword' % 'mods__genre' : 'biography'}))
query_results = query.execute()

In [16]: query_results.hits.total
Out[16]: 0

对为什么会出现这种情况有任何见解吗?我知道以下划线开头的字段名称是保留的,但没有偶然发现任何指示字段内下划线的文档 - 特别是连续多个下划线 - 会有问题。

【问题讨论】:

    标签: elasticsearch elasticsearch-dsl-py


    【解决方案1】:

    这仅仅是因为elasticsearch-dsl-py 将字段名称中的双下划线__ 替换为点.。这可以在utils.py 的第 222-223 行看到。所以基本上,第二个查询实际上是在mods.genre.keyword 上进行的,这可能不是您所期望的。

    有关上下文的更多信息可以在issue #28 中看到,但基本上他们想采用类似于 Django CRM 中所做的概念。

    【讨论】:

    • 啊哈!没想到考虑elasticsearch-dsl-py,很有意义。谢谢!
    • 很高兴它有帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-05
    • 1970-01-01
    • 2015-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-14
    相关资源
    最近更新 更多