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},.......
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.