嵌套数组在"字段"中表示时会变平。我希望合并来自同一路径的值,但不会修改内部数据结构。
有人可以解释我是否做错了什么,或者这是否属于Elasticsearch问题?
重现的步骤:
创建2D数据
curl -XPOST localhost:9200/test/5 -d '{ "data": [ [100],[2,3],[6,7] ] }'
查询数据,指定字段
curl -XGET localhost:9200/test/5/_search -d '{"query":{"query_string":{"query":"*"} }, "fields":["data"] } }'
结果:
{"took":1,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":1.0,"hits":[{"_index":"test","_type":"5","_id":"AVdsHJrepOClGTFyoGqo","_score":1.0,"fields":{"data":[100,2,3,6,7]}}]}}
在不使用"字段":
的情况下重复curl -XGET localhost:9200/test/5/_search -d '{"query":{"query_string":{"query":"*"} } } }'
结果:
{"took":1,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":1.0,"hits":[{"_index":"test","_type":"5","_id":"AVdsHJrepOClGTFyoGqo","_score":1.0,"_source":{ "data": [ [100],[2,3],[6,7] ] }}]}}
请注意_source和字段不同,在"字段"将2D阵列分解为一维阵列。
答案 0 :(得分:3)
当您在请求中没有指定任何其他内容时,您获取的foreach命中的内容是“_source”对象,即完全您在索引期间发送给ES的Json(甚至包括空格!) 。
当您使用源过滤时,正如安德烈建议的那样,除了您可以包含或排除某些字段外,它是相同的。
在查询中使用“fields”指令时,返回值不是从_source获取的,而是直接从Lucene索引中读取。 (请参阅docs)现在,搜索响应中的键将从“_source”切换为“字段”以反映此更改。
正如alkis所说: https://www.elastic.co/guide/en/elasticsearch/reference/current/array.html 这些文档预先说明,是的,Elasticsearch确实压缩了数组。
答案 1 :(得分:2)
我通常会source filtering
而不是指定“字段”您的查询将更改为:
curl -XGET <IPADDRESS>:9200/test/5/_search -d '{"_source":{"include": ["data"]}, "query":{"query_string":{"query":"*"} }}'
答案 2 :(得分:2)
从这里https://www.elastic.co/guide/en/elasticsearch/reference/current/array.html 似乎elasticsearch认为它们是相同的。
In Elasticsearch, there is no dedicated array type. Any field can contain zero or more values by default, however, all values in the array must be of the same datatype. For instance:
an array of strings: [ "one", "two" ]
an array of integers: [ 1, 2 ]
an array of arrays: [ 1, [ 2, 3 ]] which is the equivalent of [ 1, 2, 3 ]
an array of objects: [ { "name": "Mary", "age": 12 }, { "name": "John", "age": 10 }]
您可以使用json对象数组,并使用嵌套数据类型和嵌套查询。
也许嵌套数据类型可能会有所帮助
PUT /my_index
PUT /my_index/_mapping/my_type
{
"properties" : {
"data" : {
"type" : "nested",
"properties": {
"value" : {"type": "long" }
}
}
}
}
POST /my_index/my_type
{
"data": [
{ "value": [1, 2] },
{ "value": [3, 4] }
]
}
POST /my_index/my_type
{
"data": [
{ "value": [1, 5] }
]
}
GET /my_index/my_type/_search
{
"query": {
"nested": {
"path": "data",
"query": {
"bool": {
"must": [
{
"match": {
"data.value": 1
}
},
{
"match": {
"data.value": 2
}
}
]
}
}
}
}
}