# Elaasticsearch query to compare a filed value of 2 documents in same index

**URL:** https://discuss.elastic.co/t/elaasticsearch-query-to-compare-a-filed-value-of-2-documents-in-same-index/197258
**Category:** Elasticsearch
**Created:** [August 29, 2019, 4:47am UTC](https://discuss.elastic.co/t/elaasticsearch-query-to-compare-a-filed-value-of-2-documents-in-same-index/197258 "2019-08-29T04:47:53Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Harish\_Malu](https://avatars.discourse-cdn.com/v4/letter/h/9dc877/32.png) [@Harish\_Malu](https://discuss.elastic.co/u/Harish_Malu)
#### Post date: [August 29, 2019, 4:47am UTC](https://discuss.elastic.co/t/elaasticsearch-query-to-compare-a-filed-value-of-2-documents-in-same-index/197258/1 "2019-08-29T04:47:53Z")

</div>

I have written below query to fetch latest two document to get few fields like #processor, allocated\_memory for a particular server.  
I want to extend the query to compare these fields i.e. #processor, allocated\_memory for the 2 filtered documents and return the difference value.

"query": {  
"term": {  
"servername.keyword": {  
"value": "prod01"  
}  
}  
},  
"size": 2,  
"sort": [  
{  
"logtime": {  
"order": "desc"  
}  
}  
],  
"\_source":{  
"includes":["logtime","#processors","allocated\_memory\_mb"]  
}

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [September 26, 2019, 4:47am UTC](https://discuss.elastic.co/t/elaasticsearch-query-to-compare-a-filed-value-of-2-documents-in-same-index/197258/2 "2019-09-26T04:47:55Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
