Elastic Search嵌套对象查询 [英] Elastic Search nested object query
问题描述
我有一个如下所示的弹性搜索索引集合,
I have a elastic search index collection like below,
"_index":"test",
"_type":"abc",
"_source":{
"file_name":"xyz.ex"
"metadata":{
"format":".ex"
"profile":[
{"date_value" : "2018-05-30T00:00:00",
"key_id" : "1",
"type" : "date",
"value" : [ "30-05-2018" ]
},
{
"key_id" : "2",
"type" : "freetext",
"value" : [ "New york" ]
}
}
现在我需要通过将 key_id
与其值匹配来搜索文档。 ( key_id
是一些字段,其值存储在 value
中)
例如。对于 key_id ='1'
字段,如果它的 value = 30-05-2018
则应与上面的匹配文档。
Now I need to search for document by matching key_id
to its value. (key_id
is some field whose value is stored in "value"
)
Ex. For key_id='1'
field, if it's value = "30-05-2018"
it should match the above document.
我尝试将其映射为嵌套对象,但是我无法编写查询来使用2个或多个 key_id
匹配其各自的值。
I tried mapping this as a nested object, But I am not able to write query to search with 2 or more key_id
matching its respective value.
推荐答案
这就是我要这样做的方式。您需要通过 bool / filter
(或 bool / must
)对每个条件对进行两个嵌套查询,因为您要匹配同一父文档中的两个不同的嵌套元素。
This is how I would do it. You need to AND together via bool/filter
(or bool/must
) two nested queries for each of the condition pair, since you want to match two different nested elements from the same parent document.
{
"query": {
"bool": {
"filter": [
{
"nested": {
"path": "metadata.profile",
"query": {
"bool": {
"filter": [
{
"term": {
"metadata.profile.f1": "a"
}
},
{
"term": {
"metadata.profile.f2": true
}
}
]
}
}
}
},
{
"nested": {
"path": "metadata.profile",
"query": {
"bool": {
"filter": [
{
"term": {
"metadata.profile.f1": "b"
}
},
{
"term": {
"metadata.profile.f2": false
}
}
]
}
}
}
}
]
}
}
}
这篇关于Elastic Search嵌套对象查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!