# Using term queries with two different script based custom filter

**URL:** <https://discuss.elastic.co/t/using-term-queries-with-two-different-script-based-custom-filter/13520>\
**Category:** Elasticsearch\
**Created:** [September 9, 2013, 3:06pm UTC](https://discuss.elastic.co/t/using-term-queries-with-two-different-script-based-custom-filter/13520 "2013-09-09T15:06:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mathursharp](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@mathursharp](https://discuss.elastic.co/u/mathursharp)\
**Post date:** [September 9, 2013, 3:06pm UTC](https://discuss.elastic.co/t/using-term-queries-with-two-different-script-based-custom-filter/13520/1 "2013-09-09T15:06:40Z")

</div>

Hi,

I need to to club a set of term queries with two different script based  
custom filter score query.  
I have done this clubbing of queries and filters by using the bool filter  
but when i run this, i get results with score =1.

I am not sure whether this approach is correct or not.  
Also i am not able to get why the score is 1 even after the use of script.

Please help.

Thanks in advance.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![javanna](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/javanna/32/4698_2.png) [@javanna](https://discuss.elastic.co/u/javanna)\
**Post date:** [September 9, 2013, 4:33pm UTC](https://discuss.elastic.co/t/using-term-queries-with-two-different-script-based-custom-filter/13520/2 "2013-09-09T16:33:55Z")

</div>

Can you please post the query you are executing and elaborate a bit more on  
what you want to achieve with it?

On Monday, September 9, 2013 5:06:40 PM UTC+2, Harshit Mathur wrote:

> Hi,
> 
> I need to to club a set of term queries with two different script based  
> custom filter score query.  
> I have done this clubbing of queries and filters by using the bool filter  
> but when i run this, i get results with score =1.
> 
> I am not sure whether this approach is correct or not.  
> Also i am not able to get why the score is 1 even after the use of script.
> 
> Please help.
> 
> Thanks in advance.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![mathursharp](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@mathursharp](https://discuss.elastic.co/u/mathursharp)\
**Post date:** [September 10, 2013, 5:01am UTC](https://discuss.elastic.co/t/using-term-queries-with-two-different-script-based-custom-filter/13520/3 "2013-09-10T05:01:34Z")

</div>

hi

Thanks for answering,

Actually, I want to run a combine query where first i will be selecting  
docs based on FACTOR field (using bool query), then apply custom score  
filter query based on the two scripts.  
Below is the query,

{  
"filtered" : {  
"query" : {  
"match\_all" : { }  
},  
"filter" : {  
"bool" : {  
"must" : [ {  
"query" : {  
"custom\_score" : {  
"query" : {  
"match\_all" : { }  
},  
"script" : "(doc['SPIRIT'].value / 10)"  
}  
}  
}, {  
"query" : {  
"custom\_score" : {  
"query" : {  
"match\_all" : { }  
},  
"script" : "max (((2013 - doc['YEAR'].value) / 20), 0)"  
}  
}  
} ],  
"should" : {  
"query" : {  
"bool" : {  
"should" : [ {  
"term" : {  
"FACTOR" : {  
"value" : "31",  
"boost" : 2.0  
}  
}  
}, {  
"term" : {  
"FACTOR" : {  
"value" : "252",  
"boost" : 1.0  
}  
}  
}, {  
"term" : {  
"FACTOR" : {  
"value" : "276",  
"boost" : 2.0  
}  
}  
}, {  
"term" : {  
"FACTOR" : {  
"value" : "23",  
"boost" : 1.0  
}  
}  
}, {  
"term" : {  
"FACTOR" : {  
"value" : "9",  
"boost" : 2.0  
}  
}  
} ]  
}  
}  
}  
}  
}  
}  
}

On Monday, September 9, 2013 10:03:55 PM UTC+5:30, Luca Cavanna wrote:

> Can you please post the query you are executing and elaborate a bit more  
> on what you want to achieve with it?
> 
> On Monday, September 9, 2013 5:06:40 PM UTC+2, Harshit Mathur wrote:
> 
> > Hi,
> > 
> > I need to to club a set of term queries with two different script based  
> > custom filter score query.  
> > I have done this clubbing of queries and filters by using the bool filter  
> > but when i run this, i get results with score =1.
> > 
> > I am not sure whether this approach is correct or not.  
> > Also i am not able to get why the score is 1 even after the use of script.
> > 
> > Please help.
> > 
> > Thanks in advance.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![javanna](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/javanna/32/4698_2.png) [@javanna](https://discuss.elastic.co/u/javanna)\
**Post date:** [September 12, 2013, 11:46am UTC](https://discuss.elastic.co/t/using-term-queries-with-two-different-script-based-custom-filter/13520/4 "2013-09-12T11:46:50Z")

</div>

Hi,  
thanks for posting the query.

The problem is that you put the scoring logic in the filter block, which is  
wrapped into a ConstantScoreQuery that always returns 1. In fact filters  
are not scored, but meant to be used to exclude documents the query need to  
be executed on. I would just move the whole scoring block to the query part  
of the json object. You don't seem to have any filter in your query, thus I  
don't think you need the filtered query either.

Cheers  
Luca

On Tuesday, September 10, 2013 7:01:34 AM UTC+2, Harshit Mathur wrote:

> hi
> 
> Thanks for answering,
> 
> Actually, I want to run a combine query where first i will be selecting  
> docs based on FACTOR field (using bool query), then apply custom score  
> filter query based on the two scripts.  
> Below is the query,
> 
> {  
> "filtered" : {  
> "query" : {  
> "match\_all" : { }  
> },  
> "filter" : {  
> "bool" : {  
> "must" : [ {  
> "query" : {  
> "custom\_score" : {  
> "query" : {  
> "match\_all" : { }  
> },  
> "script" : "(doc['SPIRIT'].value / 10)"  
> }  
> }  
> }, {  
> "query" : {  
> "custom\_score" : {  
> "query" : {  
> "match\_all" : { }  
> },  
> "script" : "max (((2013 - doc['YEAR'].value) / 20), 0)"  
> }  
> }  
> } ],  
> "should" : {  
> "query" : {  
> "bool" : {  
> "should" : [ {  
> "term" : {  
> "FACTOR" : {  
> "value" : "31",  
> "boost" : 2.0  
> }  
> }  
> }, {  
> "term" : {  
> "FACTOR" : {  
> "value" : "252",  
> "boost" : 1.0  
> }  
> }  
> }, {  
> "term" : {  
> "FACTOR" : {  
> "value" : "276",  
> "boost" : 2.0  
> }  
> }  
> }, {  
> "term" : {  
> "FACTOR" : {  
> "value" : "23",  
> "boost" : 1.0  
> }  
> }  
> }, {  
> "term" : {  
> "FACTOR" : {  
> "value" : "9",  
> "boost" : 2.0  
> }  
> }  
> } ]  
> }  
> }  
> }  
> }  
> }  
> }  
> }
> 
> On Monday, September 9, 2013 10:03:55 PM UTC+5:30, Luca Cavanna wrote:
> 
> > Can you please post the query you are executing and elaborate a bit more  
> > on what you want to achieve with it?
> > 
> > On Monday, September 9, 2013 5:06:40 PM UTC+2, Harshit Mathur wrote:
> > 
> > > Hi,
> > > 
> > > I need to to club a set of term queries with two different script based  
> > > custom filter score query.  
> > > I have done this clubbing of queries and filters by using the bool filter  
> > > but when i run this, i get results with score =1.
> > > 
> > > I am not sure whether this approach is correct or not.  
> > > Also i am not able to get why the score is 1 even after the use of  
> > > script.
> > > 
> > > Please help.
> > > 
> > > Thanks in advance.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![mathursharp](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@mathursharp](https://discuss.elastic.co/u/mathursharp)\
**Post date:** [September 13, 2013, 4:34am UTC](https://discuss.elastic.co/t/using-term-queries-with-two-different-script-based-custom-filter/13520/5 "2013-09-13T04:34:31Z")

</div>

Hi,

Thanks for the reply.  
I got a bit confused, can you please correct the query for me.  
You said about constant score query, but i have used custom score query.

Please help.

Thanks,

Harshit  
On 12 Sep 2013 17:16, "Luca Cavanna" [cavannaluca@gmail.com](mailto:cavannaluca@gmail.com) wrote:

> Hi,  
> thanks for posting the query.
> 
> The problem is that you put the scoring logic in the filter block, which  
> is wrapped into a ConstantScoreQuery that always returns 1. In fact filters  
> are not scored, but meant to be used to exclude documents the query need to  
> be executed on. I would just move the whole scoring block to the query part  
> of the json object. You don't seem to have any filter in your query, thus I  
> don't think you need the filtered query either.
> 
> Cheers  
> Luca
> 
> On Tuesday, September 10, 2013 7:01:34 AM UTC+2, Harshit Mathur wrote:
> 
> > hi
> > 
> > Thanks for answering,
> > 
> > Actually, I want to run a combine query where first i will be selecting  
> > docs based on FACTOR field (using bool query), then apply custom score  
> > filter query based on the two scripts.  
> > Below is the query,
> > 
> > {  
> > "filtered" : {  
> > "query" : {  
> > "match\_all" : { }  
> > },  
> > "filter" : {  
> > "bool" : {  
> > "must" : [ {  
> > "query" : {  
> > "custom\_score" : {  
> > "query" : {  
> > "match\_all" : { }  
> > },  
> > "script" : "(doc['SPIRIT'].value / 10)"  
> > }  
> > }  
> > }, {  
> > "query" : {  
> > "custom\_score" : {  
> > "query" : {  
> > "match\_all" : { }  
> > },  
> > "script" : "max (((2013 - doc['YEAR'].value) / 20), 0)"  
> > }  
> > }  
> > } ],  
> > "should" : {  
> > "query" : {  
> > "bool" : {  
> > "should" : [ {  
> > "term" : {  
> > "FACTOR" : {  
> > "value" : "31",  
> > "boost" : 2.0  
> > }  
> > }  
> > }, {  
> > "term" : {  
> > "FACTOR" : {  
> > "value" : "252",  
> > "boost" : 1.0  
> > }  
> > }  
> > }, {  
> > "term" : {  
> > "FACTOR" : {  
> > "value" : "276",  
> > "boost" : 2.0  
> > }  
> > }  
> > }, {  
> > "term" : {  
> > "FACTOR" : {  
> > "value" : "23",  
> > "boost" : 1.0  
> > }  
> > }  
> > }, {  
> > "term" : {  
> > "FACTOR" : {  
> > "value" : "9",  
> > "boost" : 2.0  
> > }  
> > }  
> > } ]  
> > }  
> > }  
> > }  
> > }  
> > }  
> > }  
> > }
> > 
> > On Monday, September 9, 2013 10:03:55 PM UTC+5:30, Luca Cavanna wrote:
> > 
> > > Can you please post the query you are executing and elaborate a bit more  
> > > on what you want to achieve with it?
> > > 
> > > On Monday, September 9, 2013 5:06:40 PM UTC+2, Harshit Mathur wrote:
> > > 
> > > > Hi,
> > > > 
> > > > I need to to club a set of term queries with two different script based  
> > > > custom filter score query.  
> > > > I have done this clubbing of queries and filters by using the bool  
> > > > filter  
> > > > but when i run this, i get results with score =1.
> > > > 
> > > > I am not sure whether this approach is correct or not.  
> > > > Also i am not able to get why the score is 1 even after the use of  
> > > > script.
> > > > 
> > > > Please help.
> > > > 
> > > > Thanks in advance.
> > > > 
> > > > --  
> > > > You received this message because you are subscribed to a topic in the  
> > > > Google Groups "elasticsearch" group.  
> > > > To unsubscribe from this topic, visit  
> > > > [https://groups.google.com/d/topic/elasticsearch/CptEL1NlyfA/unsubscribe](https://groups.google.com/d/topic/elasticsearch/CptEL1NlyfA/unsubscribe).  
> > > > To unsubscribe from this group and all its topics, send an email to  
> > > > [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![javanna](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/javanna/32/4698_2.png) [@javanna](https://discuss.elastic.co/u/javanna)\
**Post date:** [September 13, 2013, 8:44am UTC](https://discuss.elastic.co/t/using-term-queries-with-two-different-script-based-custom-filter/13520/6 "2013-09-13T08:44:09Z")

</div>

No worries, the important bit is that filters don't have any score, but are  
meant to be used to excude results from the resultset. The part of your  
query that needs to influence the score needs to be submitted as a query,  
and as far as I can see you don't have any filter in your query. Therefore  
you can remove the filtered query and move two levels up your bool query,  
like this:

{  
"bool" : {  
"must" : [  
here go your must clauses  
],  
"should" :[  
here go your should clauses  
]  
}  
}

On Fri, Sep 13, 2013 at 6:34 AM, Harshit Mathur [mathursharp@gmail.com](mailto:mathursharp@gmail.com)wrote:

> Hi,
> 
> Thanks for the reply.  
> I got a bit confused, can you please correct the query for me.  
> You said about constant score query, but i have used custom score query.
> 
> Please help.
> 
> Thanks,
> 
> Harshit  
> On 12 Sep 2013 17:16, "Luca Cavanna" [cavannaluca@gmail.com](mailto:cavannaluca@gmail.com) wrote:
> 
> > Hi,  
> > thanks for posting the query.
> > 
> > The problem is that you put the scoring logic in the filter block, which  
> > is wrapped into a ConstantScoreQuery that always returns 1. In fact filters  
> > are not scored, but meant to be used to exclude documents the query need to  
> > be executed on. I would just move the whole scoring block to the query part  
> > of the json object. You don't seem to have any filter in your query, thus I  
> > don't think you need the filtered query either.
> > 
> > Cheers  
> > Luca
> > 
> > On Tuesday, September 10, 2013 7:01:34 AM UTC+2, Harshit Mathur wrote:
> > 
> > > hi
> > > 
> > > Thanks for answering,
> > > 
> > > Actually, I want to run a combine query where first i will be selecting  
> > > docs based on FACTOR field (using bool query), then apply custom score  
> > > filter query based on the two scripts.  
> > > Below is the query,
> > > 
> > > {  
> > > "filtered" : {  
> > > "query" : {  
> > > "match\_all" : { }  
> > > },  
> > > "filter" : {  
> > > "bool" : {  
> > > "must" : [ {  
> > > "query" : {  
> > > "custom\_score" : {  
> > > "query" : {  
> > > "match\_all" : { }  
> > > },  
> > > "script" : "(doc['SPIRIT'].value / 10)"  
> > > }  
> > > }  
> > > }, {  
> > > "query" : {  
> > > "custom\_score" : {  
> > > "query" : {  
> > > "match\_all" : { }  
> > > },  
> > > "script" : "max (((2013 - doc['YEAR'].value) / 20), 0)"  
> > > }  
> > > }  
> > > } ],  
> > > "should" : {  
> > > "query" : {  
> > > "bool" : {  
> > > "should" : [ {  
> > > "term" : {  
> > > "FACTOR" : {  
> > > "value" : "31",  
> > > "boost" : 2.0  
> > > }  
> > > }  
> > > }, {  
> > > "term" : {  
> > > "FACTOR" : {  
> > > "value" : "252",  
> > > "boost" : 1.0  
> > > }  
> > > }  
> > > }, {  
> > > "term" : {  
> > > "FACTOR" : {  
> > > "value" : "276",  
> > > "boost" : 2.0  
> > > }  
> > > }  
> > > }, {  
> > > "term" : {  
> > > "FACTOR" : {  
> > > "value" : "23",  
> > > "boost" : 1.0  
> > > }  
> > > }  
> > > }, {  
> > > "term" : {  
> > > "FACTOR" : {  
> > > "value" : "9",  
> > > "boost" : 2.0  
> > > }  
> > > }  
> > > } ]  
> > > }  
> > > }  
> > > }  
> > > }  
> > > }  
> > > }  
> > > }
> > > 
> > > On Monday, September 9, 2013 10:03:55 PM UTC+5:30, Luca Cavanna wrote:
> > > 
> > > > Can you please post the query you are executing and elaborate a bit  
> > > > more on what you want to achieve with it?
> > > > 
> > > > On Monday, September 9, 2013 5:06:40 PM UTC+2, Harshit Mathur wrote:
> > > > 
> > > > > Hi,
> > > > > 
> > > > > I need to to club a set of term queries with two different script based  
> > > > > custom filter score query.  
> > > > > I have done this clubbing of queries and filters by using the bool  
> > > > > filter  
> > > > > but when i run this, i get results with score =1.
> > > > > 
> > > > > I am not sure whether this approach is correct or not.  
> > > > > Also i am not able to get why the score is 1 even after the use of  
> > > > > script.
> > > > > 
> > > > > Please help.
> > > > > 
> > > > > Thanks in advance.
> > > > > 
> > > > > --  
> > > > > You received this message because you are subscribed to a topic in the  
> > > > > Google Groups "elasticsearch" group.  
> > > > > To unsubscribe from this topic, visit  
> > > > > [https://groups.google.com/d/topic/elasticsearch/CptEL1NlyfA/unsubscribe](https://groups.google.com/d/topic/elasticsearch/CptEL1NlyfA/unsubscribe).  
> > > > > To unsubscribe from this group and all its topics, send an email to  
> > > > > [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > > > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> You received this message because you are subscribed to a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/CptEL1NlyfA/unsubscribe](https://groups.google.com/d/topic/elasticsearch/CptEL1NlyfA/unsubscribe).  
> To unsubscribe from this group and all its topics, send an email to  
> [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:16am UTC](https://discuss.elastic.co/t/using-term-queries-with-two-different-script-based-custom-filter/13520/7 "2017-07-06T02:16:41Z")

</div>


