# Help with function\_score

**URL:** <https://discuss.elastic.co/t/help-with-function-score/66551>\
**Category:** Elasticsearch\
**Created:** [November 18, 2016, 2:41pm UTC](https://discuss.elastic.co/t/help-with-function-score/66551 "2016-11-18T14:41:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alkalit](https://avatars.discourse-cdn.com/v4/letter/a/57b2e6/32.png) [@Alkalit](https://discuss.elastic.co/u/Alkalit)\
**Post date:** [November 18, 2016, 2:41pm UTC](https://discuss.elastic.co/t/help-with-function-score/66551/1 "2016-11-18T14:41:00Z")

</div>

I need to tune search relevance for ads. In the project we are not deleting ads in time.  
But we need to show up more-or-less fresh ads for users. So I decided to use the function\_score query.  
But it does not works - relevance is changing, but actually I'm getting the same result every time.

I tried to choose different values for the function, but did not notice the difference.

The request:

```auto
curl -XGET 'http://localhost:9200/_search?pretty' -d '{
 "from": 0,
 "query": {
   "filtered": {
     "filter": {
       "missing": {
         "field": "deleted_at"
       }
     },
     "query": {
       "bool": {
         "must": [
           {
             "range": {
               "created_at": {
                 "lt": "2016-11-18T14:27:30.524856+00:00"
               }
             }
           },
           {
             "term": {
               "ad_type": "sell"
             }
           },
           {
             "function_score": {
               "functions": [
                 {
                   "linear": {
                     "updated_at": {
                       "decay": 0.3,
                       "offset": "1w",
                       "scale": "4w"
                     }
                   }
                 }
               ]
             }
           },
           {
             "match": {
               "text": "\u0431\u0438\u0433 \u0431\u0435\u0433\u0438"
             }
           }
         ]
       }
     }
   }
 },
 "size": 20
}'

```

The mapping: [http://pastebin.com/hnvgVyKS](http://pastebin.com/hnvgVyKS)  
ES version is 2.3

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [November 21, 2016, 5:43pm UTC](https://discuss.elastic.co/t/help-with-function-score/66551/2 "2016-11-21T17:43:07Z")

</div>

There are a few things that stand out with your query.

- The `filtered_query` that you are using is [deprecated in Elasticsearch version 2.0 and is not supported for version 5.0](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html#query-dsl-filtered-query) .This is probably a good time to [put your filters inside of the bool query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html#query-dsl-filtered-query) instead.
- The `ad_type` field on which you are doing a `term` query is analyzed using the `russian` analyzer. You probably want to make it a `not_analyzed` field if you are using it for `term` queries?
- You have put the `range` query on the `created_at` field and the `term` query on the `ad_type` field inside of the `must` clause of your `bool` query. Things that you put inside of a `must` clause will contribute to the score that's calculated for your documents when you query them. In the case of these `range` and `term` queries, the score will be 1 if a document matches or 0 if it doesn't match. However, it probably doesn't make sense that you score on these clauses? Either a document is in this `range` and matches the `ad_type` or it doesn't. These two clauses should not contribute to the score. So, I'd recommend that you put them in a `filter` clause instead.

My first step would be to clean up the query along the points that I raised above. It will give you a better score, and a score that's easier to understand. If you still have problems after doing so, let me know and I'd be happy to delve deeper.

By the way, do you know about the [explain API](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-explain.html)? It will explain in great detail how a score is calculated.

---

<div class="post-metadata">

**Author:** ![Alkalit](https://avatars.discourse-cdn.com/v4/letter/a/57b2e6/32.png) [@Alkalit](https://discuss.elastic.co/u/Alkalit)\
**Post date:** [December 15, 2016, 2:35pm UTC](https://discuss.elastic.co/t/help-with-function-score/66551/3 "2016-12-15T14:35:52Z")

</div>

Thanks alot for your response. I'm sorry for I could not replay earlier.

> You probably want to make it a not\_analyzed field if you are using it for term queries?

Yes, you are absolutly correct. It's my mistake with the mapping.

After some searches and experiments I got something like this:

```auto
{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "filter": [
              {
                "missing": {
                  "field": "deleted_at"
                }
              },
              {
                "range": {
                  "created_at": {
                    "lt": "now"
                  }
                }
              },
              {
                "term": {
                  "ad_type": "sell"
                }
              }
            ]
          }
        },
        {
          "function_score": {
            "functions": [
              {
                "exp": {
                  "updated_at": {
                    "decay": 0.33,
                    "scale": "12w"
                  }
                }
              }
            ],
            "query": {
              "match": {
                "text": "search phrase"
              }
            }
          }
        }
      ]
    }
  },
}

```

---

<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:** [January 12, 2017, 2:36pm UTC](https://discuss.elastic.co/t/help-with-function-score/66551/4 "2017-01-12T14:36:31Z")

</div>

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