How create the Decay function in ElasticSearch DSL python
<p>I am querying the Elastic Search with <a href="https://pypi.python.org/pypi/elasticsearch-dsl/2.0.0" rel="nofollow">elasticsearch-dsl/2.0.0</a></p>

<pre><code> "linear": {
        "date": {
              "origin": "now", 
              "scale": "10d",
              "offset": "5d", 
              "decay" : 0.5 
        }
    }
</code></pre>

<p><a href="https://www.elastic.co/guide/en/elasticsearch/reference/2.4/query-dsl-function-score-query.html#function-decay" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/reference/2.4/query-dsl-function-score-query.html#function-decay</a></p>

<p>How to construct the linear function query with elasticsearch-dsl</p>

<p>Try the following query</p>

<pre><code>from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search, query

client = Elasticsearch()

s = Search(using=client)

function_score_query = query.Q(
        'function_score',
        query=query.Q('match', your_field='your_query'),
        functions=[
            query.SF('linear',date={"origin":"now","scale":"10d",
                                    "offset":"5d", "decay":0.5})
        ]
    )
</code></pre>

<p>If you print the <code>function_score_query.to_dict()</code> you will get</p>

<pre><code>{
    'function_score': {
        'query': {
            'match': {
                'your_field': 'your_query'
            }
        },
        'functions': [{
            'linear': {
                'date': {
                    'origin': 'now',
                    'decay': 0.5,
                    'scale': '10d',
                    'offset': '5d'
                }
            }
        }]
    }
}
</code></pre>

<p>You could check the <a href="https://github.com/elastic/elasticsearch-dsl-py/blob/3ebb486363b2c94172b81668e41fdb2411e5a022/test_elasticsearch_dsl/test_query.py#L284" rel="nofollow">test cases</a> on github to see how function score query can be used in DSL</p>

python elasticsearch