elasticsearch - 使用python elasticsearch-dsl的Elasticsearch字段名称中的多个下划线

标签 elasticsearch elasticsearch-dsl-py

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

使用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-dsl-py用点__替换了字段名中的双下划线.。可以在 utils.py 中的第222-223行看到。因此,基本上,第二个查询实际上是在mods.genre.keyword上进行的,这可能不是您期望的。

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

关于elasticsearch - 使用python elasticsearch-dsl的Elasticsearch字段名称中的多个下划线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51050043/

相关文章:

elasticsearch - 以编程方式设置索引的read_only_allow_delete属性

elasticsearch - 使用python中的 Elasticsearch DSL访问单个字段

python - 无法通过 Python 访问 ElasticSearch AWS

elasticsearch - 如何使用Elasticsearch过渡API(别名已更新以指向最新创建的索引)?

elasticsearch - 聚集的扫描结果

date - 过滤范围日期elasticsearch

ruby-on-rails-4 - 使用多模型在 Rails 中通过极性提高 Elasticsearch 结果

python - elasticsearch dsl python解压缩q查询

scala - 在 elastic4s 中创建后如何更改索引的设置?

spring-mvc - ES Spring数据将大小添加到spring数据格式中