python - KeyError访问elasticsearch_dsl Python客户端中的inner_hits

标签 python elasticsearch elasticsearch-dsl

使用Python elasticsearch_dsl客户端访问inner_hits数据时遇到问题。尝试使用meta.inner_hits中的嵌入式Response对象会在容器对象的“_type”上产生KeyError。以下代码是完全独立的,因此任何人都应该能够使用Python 2.7和elasticsearch_dsl 5.0.0复制相同的结果:

from elasticsearch import Elasticsearch
from elasticsearch_dsl import Index, Mapping, Nested, Search, Q
from elasticsearch_dsl.connections import connections

index = "test_index"

es_con = connections.create_connection(hosts=["localhost:9200"])
es_index = Index(index)
if es_index.exists():
    es_index.delete()
es_index.create()

para_mapping = Mapping("paragraph")
para_mapping.field("sentences", Nested())
para_mapping.save(index)

test_paras = {}
for a in range(2):
    test_paras[a] = {
        "label": "Test Paragraph p{}".format(a),
        "sentences": []
    }

    for b in range(2):
        test_sent = {
            "text": "Test Sentence p{}s{}".format(a, b),
        }

        test_paras[a]["sentences"].append(test_sent)

for idx, para in test_paras.iteritems():
    para_id = "para_id_p{}".format(idx)
    es_con.create(
        index=index,
        doc_type="paragraph",
        id=para_id,
        body=para
    )

es_index.flush()

q_1 = Search(using=es_con).index(index).doc_type('paragraph')
q_2 = q_1 = q_1.query(
    'nested', path='sentences', query=Q('term', **{"sentences.text": "p0s1"}), inner_hits={}
)
q_1 = q_1.execute()
# We got the expected paragraph
print "PARA_ID:                            ", q_1.hits[0].meta.id
# With all sentences
print "PARA[SENTENCES]:                    ", q_1.hits[0].sentences
# We can see inner_hits is included in para.meta
print "DIR PARA.META:                      ", dir(q_1.hits[0].meta)
# And it contains a "sentences" object
print "DIR PARA.META.INNER_HITS:           ", dir(q_1.hits[0].meta.inner_hits)
# Of type elasticsearch_dsl.result.Response
print "TYPE PARA.META.INNER_HITS.SENTENCES:", type(q_1.hits[0].meta.inner_hits.sentences)
# That contains a "hits" object
print "DIR PARA.META.INNER_HITS.SENTENCES: ", dir(q_1.hits[0].meta.inner_hits.sentences)
# But every attempted action yields a KeyError: '_type' in result.AttrList()
try:
    print q_1.hits[0].meta.inner_hits.sentences
except KeyError as e:
    print "\nException:", type(e)
    print "Message:", e

# Uncomment the following line to see full exception detail
# print dir(q.hits[0].meta.inner_hits.sentences.hits)

# The same query using the base-level API
es = Elasticsearch()

results = es.search(index=index, body=q_2.to_dict())
# This works just fine.
print "\nES RESULT:"
print results["hits"]["hits"][0]["inner_hits"]["sentences"]["hits"]["hits"][0]["_source"]["text"]

这是API中的错误吗?

最佳答案

这是一个错误,其中我们仅在父/子关系中考虑了inner_hits,而不考虑nested。我创建了一个跟踪修订的问题:https://github.com/elastic/elasticsearch-dsl-py/issues/565

感谢您的举报!

关于python - KeyError访问elasticsearch_dsl Python客户端中的inner_hits,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41856129/

相关文章:

elasticsearch - elasticsearch 5:排除搜索

Elasticsearch 过滤聚合桶不存在的文档

构建 selenium webdriver 项目时 Python 构建错误

elasticsearch - 对子文档字段值的Elasticsearch聚合

python - 如何将函数与 pandas 数据框一起使用

elasticsearch - 日期汇总-ElasticSearch

ElasticSearch 嵌套查询 - 排除父文档

elasticsearch - 在嵌套热门点击聚合中包含父 _source 字段

python - 从 DataFrame 行元素生成元组

python - 使用经过训练的 TensorFlow 模型预测单个 PNG 图像