如何在 Elasticsearch 中提升最近的created_at字段



我想按照一些提升规则对查询结果进行排序,同时我希望它们尽可能按创建日期排序,如果我添加created_at排序,它会改变一切,我的结果不再相关。所以我想唯一的方法是提升created_at领域(最新的在计算该提升的分数方面有最大的好处),但我不知道如何实现它。这是我的查询:

query = {
  "query" : {
    "bool" : {
      "must" : [
        {
          "range" : {
            "deadline" : {
              "gte" : "2016-05-30T11:39:10+02:00"
            }
          }
        },
        {
          "terms" : {
            "state" : [
              "open"
            ]
          }
        },
        {
          "query_string" : {
            "query" : "chant",
            "default_operator" : "AND",
            "analyzer" : "search_francais",
            "fields" : [
              "title^6",
              "description",
              "brand",
              "category_name"
            ]
          }
        }
      ]
    }
  },
  "filter" : {
    "and" : [
      {
        "geo_distance" : {
          "distance" : "40km",
          "location" : {
            "lat" : 48.855736,
            "lon" : 2.32927300000006
          }
        }
      }
    ]
  },
  "sort" : [
    {
      "_score" : "desc"
    },
    #{
    #  "created_at" : "desc"   ==> i tried this but it doesnt change results
    #}
  ]
}

尝试在 应该阻止中添加您的条件。

一)如果创建的日期应该更接近搜索查询中的值,或者您对日期应该有多近有任何想法,请给出范围查询。ii) 如果您不确定所有这些值,可以使用衰减函数。在这种情况下,查询应更改为函数查询。

 {
  "query" : {
    "bool" : {
      "must" : [
        {
          "range" : {
            "deadline" : {
              "gte" : "2016-05-30T11:39:10+02:00"
            }
          }
        },
        {
          "terms" : {
            "state" : [
              "open"
            ]
          }
        },
        {
          "query_string" : {
            "query" : "chant",
            "default_operator" : "AND",
            "analyzer" : "search_francais",
            "fields" : [
              "title^6",
              "description",
              "brand",
              "category_name"
            ]
          }
        }
      ],
      "should": [
        {"created_at" : "condition here .. "}
      ]
    }
  },
  "filter" : {
    "and" : [
      {
        "geo_distance" : {
          "distance" : "40km",
          "location" : {
            "lat" : 48.855736,
            "lon" : 2.32927300000006
          }
        }
      }
    ]
  }
}

相关内容

  • 没有找到相关文章

最新更新