轮胎/弹性搜索属性过滤

时间:2014-04-24 09:00:36

标签: ruby-on-rails elasticsearch

因此,当客户登录到应用程序时,他们应该只能在公司内搜索,以便我们尝试过滤客户端名称。但是日志输出似乎是正确的,但没有返回搜索结果。

#report.rb
def self.search_for_client(params, client)
 tire.search(load: true, page: params[:page], per_page: 20) do
  query  { string params[:q] } if params[:q].present?
  filter :term, :client => client
 end
end

#reports_controller.rb
def full_search
 @query = params[:q]
 if current_user.client.nil?
  @results = Report.search params
 else
  @results = Report.search_for_client params, current_user.client.title
 end
end

#log output
curl -X GET 'http://localhost:9200/reports/report/_search?load=true&size=20&pretty' -d '{"query":{"query_string":{"query":"thio"}},"filter":{"term":{"client":"ApplusRTD Norway"}},"size":20}'

1 个答案:

答案 0 :(得分:1)

实际上主要问题是索引后我没有重新启动elasticsearch服务器。我也调整了search_for_client方法。

#report.rb
def self.search_for_client(params, client)
tire.search(load: true, page: params[:page], per_page: 20) do
  query do
    boolean do
      must { string params[:q], default_operator: "AND" } if params[:q].present?
      must { string 'status:active' }
    end
  end
  filter :term, :client => [client]
end

#output
curl -X GET 'http://localhost:9200/reports/report/_search?size=20&pretty' -d '{
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "query": "UT-12-541"
          }
        }
      ]
    }
  },
  "filter": {
    "term": {
      "client": [
        "Fabricom GDF Suez"
      ]
    }
  },
  "size": 20
}'
相关问题