【问题标题】:How create the Decay function in ElasticSearch DSL python如何在 ElasticSearch DSL python 中创建 Decay 函数
【发布时间】:2017-02-15 10:44:44
【问题描述】:

我正在使用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构造线性函数查询

【问题讨论】:

    标签: python elasticsearch


    【解决方案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 中使用函数评分查询

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多