简体   繁体   中英

Elasticsearch search query issue

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":[]}}

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.

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