我试图理解为什么带有单个下划线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/