Elasticsearch: Order by date field (descending): gauss or field_value_factor?
<p>I have an issue concerning the modification of the score document according to its creation date. I have tried gauss function and field_value_factor.</p>

<p>The fist one is (all the query clause):</p>

<pre><code>@search_definition[:query] = {
                           function_score:{
                              query: {
                                  bool: {
                                      must: [
                                          {
                                  query_string: {
                                      query: &lt;query_term&gt;,
                                      fields: %w( field_1ˆ2
                                                         field_2ˆ3
                                                         ...
                                                         field_n^2),
                                      analyze_wildcard: true,
                                      auto_generate_phrase_queries: false,
                                      analyzer: 'brazilian',
                                      default_operator: 'AND'
                                  }
                              }
                             ],
                            filter: {
                                       bool: {
                                            should: [
                                                 { term: {"boolean_field": false}},
                                                 { terms:     {"array_field_1": options[:key].ids}},
                  { term: {"array_field_2.id": options[:key].id}}
                ]
             }
        }
                            }
                          },
                                gauss:{
                                  date_field: {
                                      scale: "1d",
                                      decay: "0.5"
                                  }
                                }
                  }
          }
</code></pre>

<p>With this configuration, I am telling elastic that the last documents must have a higher score. When I execute the query with it, the result is totally the opposite! The oldest documents are being returned firstly. Even if I change the origin to</p>

<pre><code>origin: "2010-05-01 00:00:00"
</code></pre>

<p>which is the date of the first document, the oldest ones are also being retrieved firstly. What am I doing wrong?</p>

<p>With field_value_factor, the things are better, but not yet what I am waiting for.... (all the query clause is)</p>

<pre><code>@search_definition[:query] = {
                           function_score:{
                              query: {
                                  bool: {
                                      must: [
                                          {
                                  query_string: {
                                      query: &lt;query_term&gt;,
                                      fields: %w( field_1ˆ2
                                                         field_2ˆ3
                                                         ...
                                                         field_n^2),
                                      analyze_wildcard: true,
                                      auto_generate_phrase_queries: false,
                                      analyzer: 'brazilian',
                                      default_operator: 'AND'
                                  }
                              }
                             ],
                            filter: {
                                       bool: {
                                            should: [
                                                 { term: {"boolean_field": false}},
                                                 { terms:     {"array_field_1": options[:key].ids}},
                  { term: {"array_field_2.id": options[:key].id}}
                ]
             }
        }
                            }
                          },
                                field_value_factor: {
                                     field: "date_field",
                                     factor : 100,
                                      modifier: "sqrt"
                                   }

                  }
          }
</code></pre>

<p>With this other configuration, the documents from 2016 and 2015 are being returned firstly, however there are tons of documents from 2016 that receive less score than others from 2015, even if I set a modifier "sqrt" with factor: 100 !!!!</p>

<p>I suppose guass function would be the appropriate solution. How can I invert this gauss result? Or how can I increase the field_value_factor so that the 2016 comes before the 2015??</p>

<p>Thanks a lot,</p>

<p>Guilherme</p>