【问题标题】:Tire / Elasticsearch: Sort not working轮胎/弹性搜索:排序不起作用
【发布时间】:2013-08-27 14:35:14
【问题描述】:

我正在使用tire gem 从我的 Rails 应用程序访问弹性搜索。我的代码生成以下查询,据我了解docs for sort 是正确的:

[2013-08-09 15:06:08,538][DEBUG][action.search.type       ] [Nitro] [tweets][3], node[INKC2ryGQ4Sx1qYP4qC-Og], [P], s[STARTED]: Failed to execute [org.elasticsearch.action.search.SearchRequest@54daad1d]
org.elasticsearch.search.SearchParseException: [tweets][3]: query[ConstantScore(*:*)],from[-1],size[-1]: Parse Failure [Failed to parse source [{
  "query": {
    "match_all": {

    }
  },
  "sort": [
    {
      "author": "asc"
    }
  ],
  "filter": {
    "terms": {
      "entities_ids": [
        "10"
      ]
    }
  },
  "size": 10,
  "from": 0
}]]

生成它的方法如下所示:

  def Tweet.search_tweets(params = {})
Tweet.search(page: params[:page], per_page: params[:per_page]) do

  if params[:query_string].present? || params[:sentiment].present? ||
    (params[:start_date].present? && params[:end_date].present?)
    query do
      boolean do
        if params[:query_string].present?
          must { string params[:query_string], default_operator: "AND" }
        end

        if params[:sentiment].present?
          must { term :sentiment, params[:sentiment]}
        end

        if params[:start_date].present? && params[:end_date].present?
          must { string "created_at:[#{params[:start_date]} TO #{params[:end_date]}]"}
        end
      end
    end
  else
    query do
      all
    end
  end

  if params[:entity_id].present?
    filter :terms, entities_ids: [params[:entity_id]]
  end

  if params[:sort].present?
    sort { by params[:sort][:by], params[:sort][:order] }
  end

end

结束

我完全不知道为什么这不起作用。

【问题讨论】:

    标签: sorting elasticsearch tire


    【解决方案1】:

    我在排序方面遇到了类似的问题...取决于您如何为该字段编制索引。如果它是一个标记化的字符串,你可能会遇到困难。相反,您可以使用多字段而不对其进行分析,或者您可以使用基于 _source 的 _script 排序:

    {   "query": {
        "query_string": {
          "query": "something or other"
        }   },   "sort": {
        "_script": {
          "script": "_source.contentLegend",
          "type": "string",
          "order": "desc"
        }   } }
    

    希望能有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-14
      • 2012-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多