# Is it possible to use filtered query with custom filters score?

**URL:** <https://discuss.elastic.co/t/is-it-possible-to-use-filtered-query-with-custom-filters-score/5479>\
**Category:** Elasticsearch\
**Created:** [September 29, 2011, 2:41pm UTC](https://discuss.elastic.co/t/is-it-possible-to-use-filtered-query-with-custom-filters-score/5479 "2011-09-29T14:41:29Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![BlueZero\_2](https://avatars.discourse-cdn.com/v4/letter/b/e68b1a/32.png) [@BlueZero\_2](https://discuss.elastic.co/u/BlueZero_2)\
**Post date:** [September 29, 2011, 2:41pm UTC](https://discuss.elastic.co/t/is-it-possible-to-use-filtered-query-with-custom-filters-score/5479/1 "2011-09-29T14:41:29Z")

</div>

I need to search documents that are matching some filters. But i want  
to sort them by custom score script.  
FI:  
filter by gender = 1

and calculate score:  
if ( age \> 10 )  
\_score = age \* 2  
else  
\_score = age \* 1

Thats just an example do not try to understand the score function 😃

Is it possible?

Thanx

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [October 2, 2011, 8:30am UTC](https://discuss.elastic.co/t/is-it-possible-to-use-filtered-query-with-custom-filters-score/5479/2 "2011-10-02T08:30:35Z")

</div>

Yes, its possible. The query you specify within a custom\_score query can be  
any query you want to use.

On Thu, Sep 29, 2011 at 5:41 PM, BlueZero [bluezero@bluezero.sk](mailto:bluezero@bluezero.sk) wrote:

> I need to search documents that are matching some filters. But i want  
> to sort them by custom score script.  
> FI:  
> filter by gender = 1
> 
> and calculate score:  
> if ( age \> 10 )  
> \_score = age \* 2  
> else  
> \_score = age \* 1
> 
> Thats just an example do not try to understand the score function 😃
> 
> Is it possible?
> 
> Thanx

---

<div class="post-metadata">

**Author:** ![BlueZero\_2](https://avatars.discourse-cdn.com/v4/letter/b/e68b1a/32.png) [@BlueZero\_2](https://discuss.elastic.co/u/BlueZero_2)\
**Post date:** [October 3, 2011, 8:20am UTC](https://discuss.elastic.co/t/is-it-possible-to-use-filtered-query-with-custom-filters-score/5479/3 "2011-10-03T08:20:34Z")

</div>

Hm, ok, this is my request:

{  
"from":0,  
"size":"20",  
"fields":  
[  
"idUser",  
"online",  
"idChat",  
"lastUpdate"  
],  
"custom\_filters\_score":  
{  
"query":  
{  
"filtered":  
{  
"query":  
{  
"match\_all":{}  
},  
"filter":  
{  
"and":  
[  
{  
"term":  
{  
"userType":"0"  
}  
},  
{  
"term":  
{  
"NeDajVek":"0"  
}  
},  
{  
"numeric\_range":  
{  
"Vek":  
{  
"from":14,  
"to":50  
}  
}  
},  
{  
"term":  
{  
"Pohlavie":"2"  
}  
},  
{  
"term":  
{  
"idKraj":"1"  
}  
},  
{  
"term":  
{  
"Fotka":"1"  
}  
},  
{  
"numeric\_range":  
{  
"Level":  
{  
"from":1,  
"to":10  
}  
}  
}  
],  
"not":  
}  
},  
"filters":  
[  
{  
"filter":  
{  
"term":  
{  
"Vek":25  
}  
},  
"boost":3  
},  
{  
"filter":  
{  
"term":  
{  
"Vek":22  
}  
},  
"boost":1  
}  
]  
}  
}  
}

and i am getting this parse error:  
[No parser for element [custom\_filters\_score]]

On Oct 2, 10:30 am, Shay Banon [kim...@gmail.com](mailto:kim...@gmail.com) wrote:

> Yes, its possible. The query you specify within a custom\_score query can be  
> any query you want to use.
> 
> On Thu, Sep 29, 2011 at 5:41 PM, BlueZero [bluez...@bluezero.sk](mailto:bluez...@bluezero.sk) wrote:
> 
> > I need to search documents that are matching some filters. But i want  
> > to sort them by custom score script.  
> > FI:  
> > filter by gender = 1
> 
> > and calculate score:  
> > if ( age \> 10 )  
> > \_score = age \* 2  
> > else  
> > \_score = age \* 1
> 
> > Thats just an example do not try to understand the score function 😃
> 
> > Is it possible?
> 
> > Thanx

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [October 3, 2011, 9:36am UTC](https://discuss.elastic.co/t/is-it-possible-to-use-filtered-query-with-custom-filters-score/5479/4 "2011-10-03T09:36:05Z")

</div>

You need to put the "custom\_filters\_score" element in a "query" element that  
represents the "query" part of the search request (otherwise, the search  
request can't tell where the query part is).

On Mon, Oct 3, 2011 at 10:20 AM, BlueZero [bluezero@bluezero.sk](mailto:bluezero@bluezero.sk) wrote:

> Hm, ok, this is my request:
> 
> {  
> "from":0,  
> "size":"20",  
> "fields":  
> [  
> "idUser",  
> "online",  
> "idChat",  
> "lastUpdate"  
> ],  
> "custom\_filters\_score":  
> {  
> "query":  
> {  
> "filtered":  
> {  
> "query":  
> {  
> "match\_all":{}  
> },  
> "filter":  
> {  
> "and":  
> [  
> {  
> "term":  
> {
> 
> "userType":"0"  
> }  
> },  
> {  
> "term":  
> {
> 
> "NeDajVek":"0"  
> }  
> },  
> {  
> "numeric\_range":  
> {  
> "Vek":  
> {
> 
> "from":14,
> 
> "to":50  
> }  
> }  
> },  
> {  
> "term":  
> {
> 
> "Pohlavie":"2"  
> }  
> },  
> {  
> "term":  
> {  
> "idKraj":"1"  
> }  
> },  
> {  
> "term":  
> {  
> "Fotka":"1"  
> }  
> },  
> {  
> "numeric\_range":  
> {  
> "Level":  
> {
> 
> "from":1,
> 
> "to":10  
> }  
> }  
> }  
> ],  
> "not":  
> }  
> },  
> "filters":  
> [  
> {  
> "filter":  
> {  
> "term":  
> {  
> "Vek":25  
> }  
> },  
> "boost":3  
> },  
> {  
> "filter":  
> {  
> "term":  
> {  
> "Vek":22  
> }  
> },  
> "boost":1  
> }  
> ]  
> }  
> }  
> }
> 
> and i am getting this parse error:  
> [No parser for element [custom\_filters\_score]]
> 
> On Oct 2, 10:30 am, Shay Banon [kim...@gmail.com](mailto:kim...@gmail.com) wrote:
> 
> > Yes, its possible. The query you specify within a custom\_score query can  
> > be  
> > any query you want to use.
> > 
> > On Thu, Sep 29, 2011 at 5:41 PM, BlueZero [bluez...@bluezero.sk](mailto:bluez...@bluezero.sk) wrote:
> > 
> > > I need to search documents that are matching some filters. But i want  
> > > to sort them by custom score script.  
> > > FI:  
> > > filter by gender = 1
> > 
> > > and calculate score:  
> > > if ( age \> 10 )  
> > > \_score = age \* 2  
> > > else  
> > > \_score = age \* 1
> > 
> > > Thats just an example do not try to understand the score function 😃
> > 
> > > Is it possible?
> > 
> > > Thanx

---

<div class="post-metadata">

**Author:** ![BlueZero\_2](https://avatars.discourse-cdn.com/v4/letter/b/e68b1a/32.png) [@BlueZero\_2](https://discuss.elastic.co/u/BlueZero_2)\
**Post date:** [October 3, 2011, 11:19am UTC](https://discuss.elastic.co/t/is-it-possible-to-use-filtered-query-with-custom-filters-score/5479/5 "2011-10-03T11:19:25Z")

</div>

I've tried to put it almost everywhere.  
Shay, can i politely ask you to alter my request to correct order?  
Thank you very much.

On Oct 3, 11:36 am, Shay Banon [kim...@gmail.com](mailto:kim...@gmail.com) wrote:

> You need to put the "custom\_filters\_score" element in a "query" element that  
> represents the "query" part of the search request (otherwise, the search  
> request can't tell where the query part is).
> 
> On Mon, Oct 3, 2011 at 10:20 AM, BlueZero [bluez...@bluezero.sk](mailto:bluez...@bluezero.sk) wrote:
> 
> > Hm, ok, this is my request:
> 
> > {  
> > "from":0,  
> > "size":"20",  
> > "fields":  
> > [  
> > "idUser",  
> > "online",  
> > "idChat",  
> > "lastUpdate"  
> > ],  
> > "custom\_filters\_score":  
> > {  
> > "query":  
> > {  
> > "filtered":  
> > {  
> > "query":  
> > {  
> > "match\_all":{}  
> > },  
> > "filter":  
> > {  
> > "and":  
> > [  
> > {  
> > "term":  
> > {
> 
> > "userType":"0"  
> > }  
> > },  
> > {  
> > "term":  
> > {
> 
> > "NeDajVek":"0"  
> > }  
> > },  
> > {  
> > "numeric\_range":  
> > {  
> > "Vek":  
> > {
> 
> > "from":14,
> 
> > "to":50  
> > }  
> > }  
> > },  
> > {  
> > "term":  
> > {
> 
> > "Pohlavie":"2"  
> > }  
> > },  
> > {  
> > "term":  
> > {  
> > "idKraj":"1"  
> > }  
> > },  
> > {  
> > "term":  
> > {  
> > "Fotka":"1"  
> > }  
> > },  
> > {  
> > "numeric\_range":  
> > {  
> > "Level":  
> > {
> 
> > "from":1,
> 
> > "to":10  
> > }  
> > }  
> > }  
> > ],  
> > "not":  
> > }  
> > },  
> > "filters":  
> > [  
> > {  
> > "filter":  
> > {  
> > "term":  
> > {  
> > "Vek":25  
> > }  
> > },  
> > "boost":3  
> > },  
> > {  
> > "filter":  
> > {  
> > "term":  
> > {  
> > "Vek":22  
> > }  
> > },  
> > "boost":1  
> > }  
> > ]  
> > }  
> > }  
> > }
> 
> > and i am getting this parse error:  
> > [No parser for element [custom\_filters\_score]]
> 
> > On Oct 2, 10:30 am, Shay Banon [kim...@gmail.com](mailto:kim...@gmail.com) wrote:
> > 
> > > Yes, its possible. The query you specify within a custom\_score query can  
> > > be  
> > > any query you want to use.
> 
> > > On Thu, Sep 29, 2011 at 5:41 PM, BlueZero [bluez...@bluezero.sk](mailto:bluez...@bluezero.sk) wrote:
> > > 
> > > > I need to search documents that are matching some filters. But i want  
> > > > to sort them by custom score script.  
> > > > FI:  
> > > > filter by gender = 1
> 
> > > > and calculate score:  
> > > > if ( age \> 10 )  
> > > > \_score = age \* 2  
> > > > else  
> > > > \_score = age \* 1
> 
> > > > Thats just an example do not try to understand the score function 😃
> 
> > > > Is it possible?
> 
> > > > Thanx

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [October 3, 2011, 11:29am UTC](https://discuss.elastic.co/t/is-it-possible-to-use-filtered-query-with-custom-filters-score/5479/6 "2011-10-03T11:29:14Z")

</div>

On Mon, 2011-10-03 at 04:19 -0700, BlueZero wrote:

> I've tried to put it almost everywhere.  
> Shay, can i politely ask you to alter my request to correct order?

just change:

```
"custom_filters_score": {.... }

```

to:  
"query": {  
"custom\_filters\_score": {.... }  
}

clint

---

<div class="post-metadata">

**Author:** ![BlueZero\_2](https://avatars.discourse-cdn.com/v4/letter/b/e68b1a/32.png) [@BlueZero\_2](https://discuss.elastic.co/u/BlueZero_2)\
**Post date:** [October 4, 2011, 7:05am UTC](https://discuss.elastic.co/t/is-it-possible-to-use-filtered-query-with-custom-filters-score/5479/7 "2011-10-04T07:05:08Z")

</div>

Thank you, works

On Oct 3, 1:29 pm, Clinton Gormley [cl...@traveljury.com](mailto:cl...@traveljury.com) wrote:

> On Mon, 2011-10-03 at 04:19 -0700, BlueZero wrote:
> 
> > I've tried to put it almost everywhere.  
> > Shay, can i politely ask you to alter my request to correct order?
> 
> just change:
> 
> ```
> "custom_filters_score": {.... }
> 
> ```
> 
> to:  
> "query": {  
> "custom\_filters\_score": {.... }  
> }
> 
> clint

---

<div class="post-metadata">

**Author:** ![Kina\_Shah](https://avatars.discourse-cdn.com/v4/letter/k/4bbf92/32.png) [@Kina\_Shah](https://discuss.elastic.co/u/Kina_Shah)\
**Post date:** [March 26, 2014, 3:19pm UTC](https://discuss.elastic.co/t/is-it-possible-to-use-filtered-query-with-custom-filters-score/5479/8 "2014-03-26T15:19:37Z")

</div>

Can you please show me the final query that works? I am trying to do  
something similar using a filtered query and then applying custom filter  
score to the filtered query.

Thanks!

On Thursday, September 29, 2011 10:41:29 AM UTC-4, BlueZero wrote:

> I need to search documents that are matching some filters. But i want  
> to sort them by custom score script.  
> FI:  
> filter by gender = 1
> 
> and calculate score:  
> if ( age \> 10 )  
> \_score = age \* 2  
> else  
> \_score = age \* 1
> 
> Thats just an example do not try to understand the score function 😃
> 
> Is it possible?
> 
> Thanx

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/33ad8ad4-b5f2-4a3a-8949-ae68198dd5cd%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/33ad8ad4-b5f2-4a3a-8949-ae68198dd5cd%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 1:40am UTC](https://discuss.elastic.co/t/is-it-possible-to-use-filtered-query-with-custom-filters-score/5479/9 "2017-07-06T01:40:32Z")

</div>


