# Ignore relevance score completely by using post\_filter only

**URL:** <https://discuss.elastic.co/t/ignore-relevance-score-completely-by-using-post-filter-only/192660>\
**Category:** Elasticsearch\
**Created:** [July 29, 2019, 9:36am UTC](https://discuss.elastic.co/t/ignore-relevance-score-completely-by-using-post-filter-only/192660 "2019-07-29T09:36:35Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![kshi\_yelpale000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kshi_yelpale000/32/42759_2.png) [@kshi\_yelpale000](https://discuss.elastic.co/u/kshi_yelpale000)\
**Post date:** [July 29, 2019, 9:36am UTC](https://discuss.elastic.co/t/ignore-relevance-score-completely-by-using-post-filter-only/192660/1 "2019-07-29T09:36:35Z")

</div>

Hi,

In my product, i don't want to use relevance score in my search queries. I have my own sorting algorithm once i have results.

On searching i found that to avoid scoring, use post\_filter insread of query clause.

Here i want to get to know the side effects of using post\_filter directly.

Please discuss the problems with me.

Thanks 🙂

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [July 29, 2019, 11:13am UTC](https://discuss.elastic.co/t/ignore-relevance-score-completely-by-using-post-filter-only/192660/2 "2019-07-29T11:13:57Z")

</div>

Hi Kshitij,  
Post-filter is about filtering not scoring or sorting.  
It's usually used so that aggregations get an unfiltered view of results (eg to show all the available product colours) but the stream of top-matching results returned as `hits` is then filtered by aggregation choices (like colour must = red). This way customers can see the count of alternative colour choices if they want to add them to the query.

> [@kshi\_yelpale000](#):
>
> i don't want to use relevance score in my search queries.

Check out the [constant\_score query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-constant-score-query.html) as a wrapper for your search clause.

> [@kshi\_yelpale000](#):
>
> I have my own sorting algorithm once i have results.

It's often best to try do this stuff "in-line" rather than post-processing elasticsearch results. The [function score](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html) query is designed to support custom scoring in-line.

---

<div class="post-metadata">

**Author:** ![kshi\_yelpale000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kshi_yelpale000/32/42759_2.png) [@kshi\_yelpale000](https://discuss.elastic.co/u/kshi_yelpale000)\
**Post date:** [August 6, 2019, 6:48pm UTC](https://discuss.elastic.co/t/ignore-relevance-score-completely-by-using-post-filter-only/192660/3 "2019-08-06T18:48:21Z")

</div>

Hi Mark,

Thanks for suggestion but constant\_score query doesn't help me here because if i have multiple bool clauses which doesn't give me right results and also multiple combination of should, must not worked because query is not supported in constant\_score.

Please suggest me some other api or logic.

Bye and thanks 🙂

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [August 6, 2019, 7:37pm UTC](https://discuss.elastic.co/t/ignore-relevance-score-completely-by-using-post-filter-only/192660/4 "2019-08-06T19:37:32Z")

</div>

I’m not sure I understand your response. I may need to see some JSON

---

<div class="post-metadata">

**Author:** ![kshi\_yelpale000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kshi_yelpale000/32/42759_2.png) [@kshi\_yelpale000](https://discuss.elastic.co/u/kshi_yelpale000)\
**Post date:** [August 7, 2019, 6:09am UTC](https://discuss.elastic.co/t/ignore-relevance-score-completely-by-using-post-filter-only/192660/5 "2019-08-07T06:09:14Z")

</div>

Hi Mark,

```
GET cs180esacs18_export_pdmarticle_1/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "Label": "player"
          }
        }
      ],
      "should": [
        {
          "match": {
            "ExternalKey": "CS-77"
          }
        }
      ]
    }
  },
  "_source": "false"
}

```

Gives following result  
{  
"took": 8,  
"timed\_out": false,  
"\_shards": {  
"total": 5,  
"successful": 5,  
"skipped": 0,  
"failed": 0  
},  
"hits": {  
"total": 9,  
"max\_score": 8.017768,  
"hits": [  
{  
"\_index": "cs180esacs18\_export\_pdmarticle\_1",  
"\_type": "item",  
"\_id": "10241",  
"\_score": 8.017768,  
"\_source": {}  
},  
{  
"\_index": "cs180esacs18\_export\_pdmarticle\_1",  
"\_type": "item",  
"\_id": "77",  
"\_score": 7.8447104,  
"\_source": {}  
},  
{  
"\_index": "cs180esacs18\_export\_pdmarticle\_1",  
"\_type": "item",  
"\_id": "10255",  
"\_score": 4.8923726,  
"\_source": {}  
},  
{  
"\_index": "cs180esacs18\_export\_pdmarticle\_1",  
"\_type": "item",  
"\_id": "62",  
"\_score": 4.7425256,  
"\_source": {}  
},  
{  
"\_index": "cs180esacs18\_export\_pdmarticle\_1",  
"\_type": "item",  
"\_id": "67",  
"\_score": 4.6269364,  
"\_source": {}  
},  
{  
"\_index": "cs180esacs18\_export\_pdmarticle\_1",  
"\_type": "item",  
"\_id": "76",  
"\_score": 4.017816,  
"\_source": {}  
},  
{  
"\_index": "cs180esacs18\_export\_pdmarticle\_1",  
"\_type": "item",  
"\_id": "79",  
"\_score": 3.5518925,  
"\_source": {}  
},  
{  
"\_index": "cs180esacs18\_export\_pdmarticle\_1",  
"\_type": "item",  
"\_id": "78",  
"\_score": 3.4534197,  
"\_source": {}  
},  
{  
"\_index": "cs180esacs18\_export\_pdmarticle\_1",  
"\_type": "item",  
"\_id": "81",  
"\_score": 3.382641,  
"\_source": {}  
}  
]  
}  
}

But constant\_score query  
GET cs180esacs18\_export\_pdmarticle\_1/\_search  
{  
"query": {  
"constant\_score": {  
"filter": {  
"bool": {  
"must": [  
{  
"match": {  
"Label": "player"  
}  
}  
],  
"should": [  
{  
"match": {  
"ExternalKey": "CS-77"  
}  
}  
]  
}  
},  
"boost": 1.2  
}  
},  
"\_source": "false"  
}

gives following result  
{  
"took": 3,  
"timed\_out": false,  
"\_shards": {  
"total": 5,  
"successful": 5,  
"skipped": 0,  
"failed": 0  
},  
"hits": {  
"total": 2,  
"max\_score": 1.2,  
"hits": [  
{  
"\_index": "cs180esacs18\_export\_pdmarticle\_1",  
"\_type": "item",  
"\_id": "10241",  
"\_score": 1.2,  
"\_source": {}  
},  
{  
"\_index": "cs180esacs18\_export\_pdmarticle\_1",  
"\_type": "item",  
"\_id": "77",  
"\_score": 1.2,  
"\_source": {}  
}  
]  
}  
}

Btw post\_filter query -  
GET cs180esacs18\_export\_pdmarticle\_1/\_search  
{  
"post\_filter": {  
"bool": {  
"must": [  
{  
"match": {  
"Label": "player"  
}  
}  
],  
"should": [  
{  
"match": {  
"ExternalKey": "CS-77"  
}  
}  
]  
}  
},  
"\_source": "false"  
}

Gives following result 🙂  
{  
"took": 5,  
"timed\_out": false,  
"\_shards": {  
"total": 5,  
"successful": 5,  
"skipped": 0,  
"failed": 0  
},  
"hits": {  
"total": 9,  
"max\_score": 1,  
"hits": [  
{  
"\_index": "cs180esacs18\_export\_pdmarticle\_1",  
"\_type": "item",  
"\_id": "79",  
"\_score": 1,  
"\_source": {}  
},  
{  
"\_index": "cs180esacs18\_export\_pdmarticle\_1",  
"\_type": "item",  
"\_id": "10255",  
"\_score": 1,  
"\_source": {}  
},  
{  
"\_index": "cs180esacs18\_export\_pdmarticle\_1",  
"\_type": "item",  
"\_id": "10241",  
"\_score": 1,  
"\_source": {}  
},  
{  
"\_index": "cs180esacs18\_export\_pdmarticle\_1",  
"\_type": "item",  
"\_id": "78",  
"\_score": 1,  
"\_source": {}  
},  
{  
"\_index": "cs180esacs18\_export\_pdmarticle\_1",  
"\_type": "item",  
"\_id": "62",  
"\_score": 1,  
"\_source": {}  
},  
{  
"\_index": "cs180esacs18\_export\_pdmarticle\_1",  
"\_type": "item",  
"\_id": "81",  
"\_score": 1,  
"\_source": {}  
},  
{  
"\_index": "cs180esacs18\_export\_pdmarticle\_1",  
"\_type": "item",  
"\_id": "77",  
"\_score": 1,  
"\_source": {}  
},  
{  
"\_index": "cs180esacs18\_export\_pdmarticle\_1",  
"\_type": "item",  
"\_id": "67",  
"\_score": 1,  
"\_source": {}  
},  
{  
"\_index": "cs180esacs18\_export\_pdmarticle\_1",  
"\_type": "item",  
"\_id": "76",  
"\_score": 1,  
"\_source": {}  
}  
]  
}  
}

So that's why i am using post\_filter.

Have a look and suggest something.

Bye and thanks

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [August 7, 2019, 7:10am UTC](https://discuss.elastic.co/t/ignore-relevance-score-completely-by-using-post-filter-only/192660/6 "2019-08-07T07:10:13Z")

</div>

> [@kshi\_yelpale000](#):
>
> Have a look and suggest something.

On the face of it I’d say it looks like a bug that the same query returns 8, 2 or 9 results depending on how it’s wrapped.  
Can you supply:

1. the version of elasticsearch you’re using
2. the JSON for the docs that match this query (just the 2 searched fields and ids will do)

---

<div class="post-metadata">

**Author:** ![kshi\_yelpale000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kshi_yelpale000/32/42759_2.png) [@kshi\_yelpale000](https://discuss.elastic.co/u/kshi_yelpale000)\
**Post date:** [August 7, 2019, 8:23am UTC](https://discuss.elastic.co/t/ignore-relevance-score-completely-by-using-post-filter-only/192660/7 "2019-08-07T08:23:39Z")

</div>

Hi Mark,

we have above only 2 result sets, query and post\_filter both returns 9 results and constant\_score returns 2 results.  
The reason for constant\_score to return 2 results is constant\_score don't support query and whatever we provide in filter clause that should be strictly matched, so doesn't matter you provided must or should clause.

Answers to your questions:

1. Elastic version is 6.3
2. So here are the some json documents with searched fileds

{  
"\_index": "cs180esacs18\_export\_pdmarticle\_1",  
"\_type": "item",  
"\_id": "77",  
"\_version": 1,  
"found": true,  
"\_source": {  
"Label": "Shuffle Player 1 GB Magenta",  
"ExternalKey": "CS-77"  
}

{  
"\_index": "cs180esacs18\_export\_pdmarticle\_1",  
"\_type": "item",  
"\_id": "10241",  
"\_version": 3,  
"found": true,  
"\_source": {  
"Label": "Shuffle Player 1 GB Magenta",  
"ExternalKey": "CS-77"  
}  
}

{  
"\_index": "cs180esacs18\_export\_pdmarticle\_1",  
"\_type": "item",  
"\_id": "76",  
"\_version": 1,  
"found": true,  
"\_source": {  
"Label": "Shuffle Player 1 GB Blue",  
"ExternalKey": "CS-76"  
}  
}

Mapping for Label

{  
"cs180esacs18\_export\_pdmarticle\_1": {  
"mappings": {  
"item": {  
"Label": {  
"full\_name": "Label",  
"mapping": {  
"Label": {  
"type": "text",  
"fields": {  
"raw": {  
"type": "keyword",  
"ignore\_above": 10922,  
"normalizer": "ExportRawAnalyzer"  
}  
},  
"analyzer": "ExportPrimaryAnalyzer"  
}  
}  
}  
}  
}  
}  
}

Same mapping is for ExternalKey  
and here are the analysers  
"analysis": {  
"normalizer": {  
"ExportRawAnalyzer": {  
"filter": "lowercase",  
"type": "custom",  
"char\_filter": {  
"char\_filter": "html\_strip"  
}  
}  
},  
"analyzer": {  
"ExportPrimaryAnalyzer": {  
"filter": "lowercase",  
"char\_filter": "html\_strip",  
"type": "custom",  
"tokenizer": "whitespace"  
}  
}  
}

Bye and thanks 🙂

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [August 7, 2019, 9:43am UTC](https://discuss.elastic.co/t/ignore-relevance-score-completely-by-using-post-filter-only/192660/8 "2019-08-07T09:43:26Z")

</div>

This looks to be a bug no longer present in 7.2.  
I'm chasing down where the fix was (Lucene or elasticsearch) but the reproduction script is [here](https://gist.github.com/markharwood/2ebe657115835bc0483aad7deb7489f9)

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [August 7, 2019, 10:25am UTC](https://discuss.elastic.co/t/ignore-relevance-score-completely-by-using-post-filter-only/192660/9 "2019-08-07T10:25:51Z")

</div>

Details behind the fix are [here](https://github.com/elastic/elasticsearch/pull/35354)

---

<div class="post-metadata">

**Author:** ![kshi\_yelpale000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kshi_yelpale000/32/42759_2.png) [@kshi\_yelpale000](https://discuss.elastic.co/u/kshi_yelpale000)\
**Post date:** [August 7, 2019, 11:31am UTC](https://discuss.elastic.co/t/ignore-relevance-score-completely-by-using-post-filter-only/192660/10 "2019-08-07T11:31:50Z")

</div>

Hi Mark,

Thanks and got it.

But what do you think simply using post\_filter instead of query to avoid scoring, would be sufficient ? will that cause any side effect ? Or any limitation it has ?

But anyways it doesn't improve much, slight time save. Is it because query is cached and post\_filter not ?

Bye

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [August 7, 2019, 12:38pm UTC](https://discuss.elastic.co/t/ignore-relevance-score-completely-by-using-post-filter-only/192660/11 "2019-08-07T12:38:01Z")

</div>

To be honest I'm not sure how it might perform differently - as I mentioned before the use case is normally applying facet choices to a main `query` as opposed to your `match_all`. Caching may well be a concern - benchmarking will give you the truth.

You could use a regular query in a constant\_score or bool filter if you scrubbed it of the `should` clauses (not sure why you'd want to send those if you're not interested in extra scoring points)

---

<div class="post-metadata">

**Author:** ![kshi\_yelpale000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kshi_yelpale000/32/42759_2.png) [@kshi\_yelpale000](https://discuss.elastic.co/u/kshi_yelpale000)\
**Post date:** [August 7, 2019, 2:59pm UTC](https://discuss.elastic.co/t/ignore-relevance-score-completely-by-using-post-filter-only/192660/12 "2019-08-07T14:59:49Z")

</div>

Hi Mark,

> [@Mark\_Harwood](#):
>
> Caching may well be a concern - benchmarking will give you the truth.

Didn't get this point, what benchmarking ?

> [@Mark\_Harwood](#):
>
> You could use a regular query in a constant\_score or bool filter if you scrubbed it of the `should` clauses

Only in the latest ES version 7.2 not in old 6.x

> [@Mark\_Harwood](#):
>
> not sure why you'd want to send those if you're not interested in extra scoring points

Do you means should clause ? Want to boost performance, so one of the way i find out that ignore the scoring because i don't need scores, that's why need to reframe all previous query clauses with post\_filter without touching inside logic of bool clauses.

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [August 7, 2019, 3:05pm UTC](https://discuss.elastic.co/t/ignore-relevance-score-completely-by-using-post-filter-only/192660/13 "2019-08-07T15:05:16Z")

</div>

> [@kshi\_yelpale000](#):
>
> Didn't get this point, what benchmarking ?

Your own testing on your own hardware + docs + queries

> [@kshi\_yelpale000](#):
>
> Only in the latest ES version 7.2 not in old 6.x

My [gist](https://gist.github.com/markharwood/2ebe657115835bc0483aad7deb7489f9) showed a 6.6 constant score wrapping a query - the problem is it effectively elevates the `should` clauses to `musts` which is not what 7.2 does. When you have a must clause then `should` clauses are only optional-extra clauses to improve ranking. In the context of a filter or constant-score-query they serve no purpose because ranking is constant.

---

<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 4, 2019, 3:05pm UTC](https://discuss.elastic.co/t/ignore-relevance-score-completely-by-using-post-filter-only/192660/14 "2019-09-04T15:05:18Z")

</div>

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