Elasticsearch搜索查询问题

时间:2018-11-20 12:15:27

标签: ruby-on-rails elasticsearch

我无法从elasticsearch服务器获得匹配。我的代码-

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" }}
                ]
              })

这是我得到的响应输出-

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

2018-11-20 18:25:34 +0530:> {“查询”:{“布尔”:{“必须”:[{“匹配”:{“状态”:“有效”}}},{ “ match”:{“ is_published”:true}},{“ match”:{“ paid”:true}},{“ match”:{“ advertise_type”:“出售”}}}}},“ sort”: [{“ updated_at”:{“ order”:“ desc”}}]} 2018-11-20 18:25:34 +0530:<{“ took”:3,“ timed_out”:false,“ _ shards”:{“ total”:1,“成功”:1,“失败”:0} ,“ hits”:{“ total”:0,“ max_score”:null,“ hits”:[]}} 2018-11-20 18:25:34 +0530:GET http://localhost:9200/_search [状态:200,请求:0.008s,查询:0.002s] 2018-11-20 18:25:34 +0530:> {“查询”:{“布尔”:{“必须”:[{“匹配”:{“状态”:“有效”}}},{“匹配” :{“ 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,“成功”:1,“失败”:0} ,“ hits”:{“ total”:0,“ max_score”:null,“ hits”:[]}}

1 个答案:

答案 0 :(得分:0)

如果我们不了解查询的结构或您要通过查询实现的目标,很难说出问题所在。

信息日志显示以下内容:

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

这意味着查询成功,并且服务器未遇到任何错误。它只是找不到与您的查询匹配的文档。

我建议您使用适当的工具首先尝试您的查询,例如Kibanas的搜索分析器(https://www.elastic.co/guide/en/kibana/current/xpack-profiler.html)。

这将向您显示有关查询的信息,一旦找到合适的查询,便可以将其集成到代码中。