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

时间:2016-05-30 10:00:08

标签: ruby-on-rails sorting elasticsearch solr-boost

我想按照一些提升规则对我的查询结果进行排序,同时我希望它们按创建日期排序,如果我添加一个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
    #}
  ]
}

1 个答案:

答案 0 :(得分:0)

尝试在should block中添加条件。

i)如果创建日期应该在搜索查询中更接近于值,或者您对日期的接近程度有任何了解,请提供范围查询。 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
          }
        }
      }
    ]
  }
}