我已經在elasticsearch中存儲了三個json對象,每個對象都有一個title
和projects
數組。如何在elasticsearch的特定索引處獲取元素?
{"name": "haris","projects": [{"title": "Splunk"},{"title": "QRadar"},{"title": "LogAnalysis"}]}
{"name": "khalid","projects": [{"title": "MS"},{"title": "Google"},{"title": "Apple"}]}
{"name": "Hamid","projects": [{"title": "Toyota"},{"title": "Honda"},{"title": "Kia"}]}
我寫了一個查詢通過_id
提取特定的對象和它的具體特點projects
curl -XGET 'localhost:9200/jsontest/_search?pretty' -d '{"query" : { "match" : {"_id":"AV1kzzZqAzHWQ2S7B8f1"} }, "_source": ["projects"]}'
正如預期的那樣,它返回項目對象
{ "took" : 3, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "failed" : 0 }, "hits" : { "total" : 1, "max_score" : 1.0, "hits" : [ { "_index" : "jsontest", "_type" : "json", "_id" : "AV1kzzZqAzHWQ2S7B8f1", "_score" : 1.0, "_source" : { "projects" : [{"title" : "Splunk"},{"title" : "QRadar"},{"title" : "LogAnalysis"} ] } } ] } }
問題:有沒有辦法在特定的i上檢索價值projects
?這是虛擬數據,在我的真實場景projects
可以有大量的元素,每個元素本身是一個具有很多屬性的JSON對象。我只需要檢索projects
的某個索引值。
只是要清楚你想從項目數組的其中一個元素返回某個字段,但是你不想返回整個項目數組元素?如果是這種情況,只需通過'inner_hits' [Elasticsearch Inner Hits]擴展查詢來實現這一點(https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-inner-hits .html#nested-inner-hits) –
@UlugToprak不,我不想從某個元素獲得某個字段,我想要數組的完整元素 –