0

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

1 Answer 1

1

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
}'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.