how to add to different field values and store it in another field in elastic search python (elastic search field operation)
how to add to different field values and store it in another field in elastic search python (elastic search field operation)
I have written this elastic search query:
es.search(index=['ind1'],doc_type=['doc'])
I am getting following result:
{'_shards': {'failed': 0, 'skipped': 0, 'successful': 5, 'total': 5},
'hits': {'hits': [{'_id': '1327',
'_index': 'ind1',
'_score': 1.0,
'_source': {'val1': 1,
'val2': None,
'value1': 1327,
'value2': 1531,
'new_values': {'nv1': 1,
'nv2': 0},
{'_id': '1349',
'_index': 'ind1',
'_score': 1.0,
'_source': {'val1': 2,
'val2': 3,
'value1': 1328,
'value2': 1539,
'new_values': {'nv1': 1,
'nv2': 3}},.......
I want all the add val1, value1 and nv1 and store it in another field Let's call total. I want the result will be like:
{'_shards': {'failed': 0, 'skipped': 0, 'successful': 5, 'total': 5},
'hits': {'hits': [{'_id': '1327',
'_index': 'ind1',
'_score': 1.0,
'_source': {'val1': 1,
'val2': None,
'value1': 1327,
'value2': 1531,
'new_values': {'nv1': 1,
'nv2': 0},
'total1': 1329},
{'_id': '1349',
'_index': 'ind1',
'_score': 1.0,
'_source': {'val1': 2,
'val2': 3,
'value1': 1328,
'value2': 1539,
'new_values': {'nv1': 1,
'nv2': 3},
'total': 1331},.......
Elasticsearch from elasticsearch import Elasticsearch es = Elasticsearch()
– Kallol
10 mins ago
So you mean Python :-)
– Val
9 mins ago
Yes. es = Elasticsearch([{'host': 'localhost', 'port': 9200}])
– Kallol
8 mins ago
1 Answer
1
What you can do is use a script field in order to compute that value on the fly:
es.search(index=['ind1'],doc_type=['doc'], body={
'_source': true,
'script_fields': {
'total': {
'script': {
'source': 'doc.val1 + doc.value1 + doc.new_values.nv1'
}
}
}
})
The best way, though, would be to pre-compute that at index time and store the value in a new total
field.
total
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Which client library are you using?
– Val
3 hours ago