简体   繁体   中英

Tire/Elasticsearch Filtering on Attribute

so when clients are logged into the app they should only be able to search within their company so we are trying to filter on client name. The log output appears to be correct though but no search results are being returned.

#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}'

Actually the main issue was that I was not restarting the elasticsearch server after indexing. I've tweaked the search_for_client method also.

#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

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
}'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM