0

I am not being able to get the hits from the elasticsearch server. My code-

client = Elasticsearch::Client.new log: true
client.indices.refresh index: 'property_index'
# search_results = client.search(body: { query: { multi_match: { query: search_params, fields: ['street_address', 'suburb'] } } })
match_query = [
  { match: { status: 'Active'} }
]
match_query << { match: { is_published: true} }
match_query << { match: { paid: true} }
match_query << { match: { suburb: params[:suburb].to_s} } if !params[:suburb].blank?
match_query << { match: { advertise_type: params[:advertise_type].to_s} } if !params[:advertise_type].blank?
match_query << { match: { state: params[:state].to_s} } if !params[:state].blank?
match_query << { match: { postal_code: params[:postal_code]} } if !params[:postal_code].blank?
response = client.search(body: {
                                  query: { bool: { must: match_query }},
                                  sort: [
                                    { updated_at: { order: "desc" }}
                                  ]
                              }, from: params[:offset], size: params[:limit])

all_records = client.search(body: {
                query: { bool: { must: match_query }},
                sort: [
                  { updated_at: { order: "desc" }}
                ]
              })

This is the response output that i am getting-

GET http://localhost:9200/_search?from=0&size=10 [status:200, request:0.010s, query:0.003s]

2018-11-20 18:25:34 +0530: > {"query":{"bool":{"must":[{"match":{"status":"Active"}},{"match":{"is_published":true}},{"match":{"paid":true}},{"match":{"advertise_type":"Sell"}}]}},"sort":[{"updated_at":{"order":"desc"}}]} 2018-11-20 18:25:34 +0530: < {"took":3,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}} 2018-11-20 18:25:34 +0530: GET http://localhost:9200/_search [status:200, request:0.008s, query:0.002s] 2018-11-20 18:25:34 +0530: > {"query":{"bool":{"must":[{"match":{"status":"Active"}},{"match":{"is_published":true}},{"match":{"paid":true}},{"match":{"advertise_type":"Sell"}}]}},"sort":[{"updated_at":{"order":"desc"}}]} 2018-11-20 18:25:34 +0530: < {"took":2,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}

2
  • please provide more information... error and logs Commented Nov 20, 2018 at 12:53
  • i am not getting any errors but i will post the information log Commented Nov 20, 2018 at 12:56

1 Answer 1

0

It's kind of difficult to tell what's wrong if we do not know about the structure or what you're trying to achieve with the query.

The information log says the following:

timed_out:false
Shards:
 total:1 
 successful: 1
 failed:0
Hits: 
 total: 0 

Which means, that the query was successful, and the server encountered no errors. It just did not find any matching documents to your query.

I'd recommend using a proper tool to first try your queries, for an example Kibanas' search profiler (https://www.elastic.co/guide/en/kibana/current/xpack-profiler.html).

This shows you information about your query, once you find your query suitable, you can integrate it into your code.

Sign up to request clarification or add additional context in comments.

2 Comments

i am trying to get all the records which match the match_query from the table. But the problem is i am always getting nil in HITS
Lets say i want to achieve something like this - http://localhost:9200/property_index/_search?size=1000&pretty=true&q=*:*

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.