如何在ElasticSearch DSL python中创建Decay函数

时间:2016-10-06 15:36:54

标签: python elasticsearch

我使用elasticsearch-dsl/2.0.0

查询弹性搜索
 "linear": {
        "date": {
              "origin": "now", 
              "scale": "10d",
              "offset": "5d", 
              "decay" : 0.5 
        }
    }

https://www.elastic.co/guide/en/elasticsearch/reference/2.4/query-dsl-function-score-query.html#function-decay

如何使用elasticsearch-dsl构建线性函数查询

1 个答案:

答案 0 :(得分:1)

尝试以下查询

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})
        ]
    )

如果您打印function_score_query.to_dict(),您将获得

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

您可以查看github上的test cases,了解如何在DSL中使用功能得分查询