0

My question a bit similar to Elasticsearch - get nested fields

but my data is more complex, so nested field in my case is array of dicts keywords:

keywords = [{'key': key1, 'value': value1}, {'key': key2, 'value': value2}, ...]

Is there any way to get only some value specifying it somehow in _source, for example I need value where key = key2?

Or the only way is to get all keywords and parse it manually in my script?

Sergius
  • 908
  • 8
  • 20

1 Answers1

0

This can be achieved by using Elasticsearch Term Query.

Assuming your index has documents similar to:

....
"key_list" : [
    {"key" : "key1", "value" : "value1"},
    {"key" : "key2", "value" : "value2"}, 
    ....
]

Then you can query the same as follows:

GET index/_search
{
    "query": {
        "term" : { "key_list.key" : "key2" } 
    }
}

And, for getting only specific values in result instead of entire response JSON, you can try using filter_path clause.

GET index/_search?pretty&filter_path=hits.hits._source.*.value
{
    "query": {
        "term" : { "key_list.key" : "key2" } 
    }
}
Sameer Mirji
  • 2,135
  • 16
  • 28
  • Interesting solution, but it doesn't work for me. Maybe because some subdicts has additional parameters? – Sergius Nov 28 '17 at 10:39
  • Yeah, would help for clarity purposes if you could post the part of your index document. – Sameer Mirji Nov 28 '17 at 10:41
  • oh, sorry, it's ok, it was just complex value, also may have subarray and subdict, so the original question still valid – Sergius Nov 28 '17 at 10:42