# Search query based on the document's different field value's comparison

**URL:** <https://discuss.elastic.co/t/search-query-based-on-the-documents-different-field-values-comparison/8371>\
**Category:** Elasticsearch\
**Created:** [July 10, 2012, 7:27am UTC](https://discuss.elastic.co/t/search-query-based-on-the-documents-different-field-values-comparison/8371 "2012-07-10T07:27:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![narinder\_izap](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/narinder_izap/32/803_2.png) [@narinder\_izap](https://discuss.elastic.co/u/narinder_izap)\
**Post date:** [July 10, 2012, 7:27am UTC](https://discuss.elastic.co/t/search-query-based-on-the-documents-different-field-values-comparison/8371/1 "2012-07-10T07:27:11Z")

</div>

Hi Shay,  
Can you please guide me to the following problem, suppose i am  
having the following document structure,

{  
"name":"narinder",  
"guid" : "345435",  
"parent\_guid": "544333",  
"country\_guid": "456778",  
"current\_location\_guid":"765432"  
}

Here I want to search all the document, where country\_guid ==  
current\_location\_guid, and same kind of search on string fields too. Is it  
implemented in elasticsearch??

---

<div class="post-metadata">

**Author:** ![narinder\_izap](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/narinder_izap/32/803_2.png) [@narinder\_izap](https://discuss.elastic.co/u/narinder_izap)\
**Post date:** [July 10, 2012, 7:28am UTC](https://discuss.elastic.co/t/search-query-based-on-the-documents-different-field-values-comparison/8371/2 "2012-07-10T07:28:56Z")

</div>

and more over I need to add the higher priority on such case too using  
custom\_filter\_score query, if its possible in elasticsearch, please help me  
in the syntax too,

Thank you  
Narinder Kaur

On Tuesday, 10 July 2012 12:57:11 UTC+5:30, Narinder Kaur wrote:

> Hi Shay,  
> Can you please guide me to the following problem, suppose i am  
> having the following document structure,
> 
> {  
> "name":"narinder",  
> "guid" : "345435",  
> "parent\_guid": "544333",  
> "country\_guid": "456778",  
> "current\_location\_guid":"765432"  
> }
> 
> Here I want to search all the document, where country\_guid ==  
> current\_location\_guid, and same kind of search on string fields too. Is it  
> implemented in elasticsearch??

---

<div class="post-metadata">

**Author:** ![Shaun\_Etherton](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shaun_etherton/32/2540_2.png) [@Shaun\_Etherton](https://discuss.elastic.co/u/Shaun_Etherton)\
**Post date:** [July 10, 2012, 12:51pm UTC](https://discuss.elastic.co/t/search-query-based-on-the-documents-different-field-values-comparison/8371/3 "2012-07-10T12:51:34Z")

</div>

Hello

Perhaps you could do something like the following?

"filtered" : {  
"query" : {  
...  
},  
"filter" : {  
"script" : {  
"script" : "doc['country\_guid'].value == doc['current\_location\_guid'].value"  
}  
}  
}

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

HTH.

- shaun

--  
shaun etherton  
Sent with Sparrow ([http://www.sparrowmailapp.com/?sig](http://www.sparrowmailapp.com/?sig))

On Tuesday, 10 July 2012 at 4:57 PM, Narinder Kaur wrote:

> Hi Shay,  
> Can you please guide me to the following problem, suppose i am having the following document structure,
> 
> {  
> "name":"narinder",  
> "guid" : "345435",  
> "parent\_guid": "544333",  
> "country\_guid": "456778",  
> "current\_location\_guid":"765432"  
> }
> 
> Here I want to search all the document, where country\_guid == current\_location\_guid, and same kind of search on string fields too. Is it implemented in elasticsearch??

---

<div class="post-metadata">

**Author:** ![narinder\_izap](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/narinder_izap/32/803_2.png) [@narinder\_izap](https://discuss.elastic.co/u/narinder_izap)\
**Post date:** [July 14, 2012, 4:57am UTC](https://discuss.elastic.co/t/search-query-based-on-the-documents-different-field-values-comparison/8371/4 "2012-07-14T04:57:54Z")

</div>

thank you, it will be really helpful.

On Tuesday, 10 July 2012 18:21:34 UTC+5:30, Shaun Etherton wrote:

> Hello
> 
> Perhaps you could do something like the following?
> 
> "filtered" : {  
> "query" : {  
> ...  
> },  
> "filter" : {  
> "script" : {  
> "script" : "doc['country\_guid'].value ==  
> doc['current\_location\_guid'].value"  
> }  
> }  
> }
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/script-filter.html)
> 
> HTH.
> 
> - shaun
> 
> --  
> shaun etherton  
> Sent with Sparrow [http://www.sparrowmailapp.com/?sig](http://www.sparrowmailapp.com/?sig)
> 
> On Tuesday, 10 July 2012 at 4:57 PM, Narinder Kaur wrote:
> 
> Hi Shay,  
> Can you please guide me to the following problem, suppose i am  
> having the following document structure,
> 
> {  
> "name":"narinder",  
> "guid" : "345435",  
> "parent\_guid": "544333",  
> "country\_guid": "456778",  
> "current\_location\_guid":"765432"  
> }
> 
> Here I want to search all the document, where country\_guid ==  
> current\_location\_guid, and same kind of search on string fields too. Is it  
> implemented in elasticsearch??

---

<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:** [July 6, 2017, 3:20am UTC](https://discuss.elastic.co/t/search-query-based-on-the-documents-different-field-values-comparison/8371/5 "2017-07-06T03:20:20Z")

</div>


