我在使用Python 中的elasticsearch_dsl和elasticsearch库搜索嵌套文档时遇到问题。
我可以成功地对文档的顶级(即非嵌套)部分执行搜索,但是我搜索嵌套部分的所有尝试都因某种原因而失败。
我已经在 StackOverflow 和网络上搜索了使用 Python 搜索嵌套文档的权威指南,但一直在简短说明。
这是我正在使用的示例文档:
{"username": "nancy",
"codeData": [
{"code": "B1", "order": "2"},
{"code": "L4", "order": "1"}
]
}
Run Code Online (Sandbox Code Playgroud)
我在索引中有 7 个文档,我已将其映射如下:
request_body = {
"settings" : {
"number_of_shards": 5,
"number_of_replicas": 1
},
'mappings': {
'testNesting': {
'properties': {
'username': {'type': 'text'},
'codeData': {'type': 'nested',
'properties' :{
"code" : {"type":"text"},
"order" :{"type":"text"}
}
}
}
}
}
}
es.indices.create(index = "nest-test6", body = request_body)
Run Code Online (Sandbox Code Playgroud)
执行以下搜索工作正常:
s = Search(using = es).query("match", username = …Run Code Online (Sandbox Code Playgroud)