对于在Elasticsearch索引的文档,如:
{
"a": [
{
"b": [1,2,3],
"c": "abcd1"
},
{
"b": [4,5,6,7],
"c": "abcd2"
}
]
}
我们是否可以应用源过滤,以便查询仅返回b
中所有对象的a
个节点?
我尝试过这样的事情:
{
"_source": {
"excludes": [
"a[*].c"
]
},
"query": {
"match_all": {}
}
}
但是,它没有用。
答案 0 :(得分:0)
由于"a"
是要完成所需内容的对象数组,因此需要将"a"
定义为Nested
数据类型。请阅读https://www.elastic.co/guide/en/elasticsearch/reference/current/array.html
因此,您必须在映射中将“a”属性定义为nested
类型。我正在按照你的例子中的后续步骤进行操作:
1.-定义映射
curl -XPUT 'localhost:9200/my_index?pretty' -H 'Content-Type: application/json' -d'
{
"mappings": {
"_doc": {
"properties": {
"a": {
"type": "nested"
}
}
}
}
}
'
2.-使用您的样本数据创建文档1
:
curl -XPUT 'localhost:9200/my_index/_doc/1?pretty' -H 'Content-Type: application/json' -d'
{
"a" : [
{
"b" : [1,2,3],
"c" : "abcd1"
},
{
"b" : [4,5,6,7],
"c" : "abcd2"
}
]
}
'
3.-以下是您的查询方式,当您必须指定您真正想要开始查询的路径时,请注意nested.path
,然后是正常查询
curl -XGET 'localhost:9200/my_index/_search?pretty' -H 'Content-Type: application/json' -d'
{
"_source": "a.b",
"query": {
"nested": {
"path": "a",
"query": {
"match_all": {}
}
}
}
}
'
这是每个对象中只有b
字段的结果:
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"a" : [
{
"b" : [1, 2, 3]
},
{
"b" : [4, 5, 6, 7]
}
]
}
}
]
}
这里是嵌套日期类型https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html
的ElasticSearch参考