使用ruby,如何从elasticsearch查询中访问返回的各个元素?

时间:2015-06-07 12:25:22

标签: ruby json elasticsearch

以下是我从搜索查询中获得的信息:

2015-06-07 21:19:06 +0900: < {"took":5,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":40,"max_score":3.0971029,"hits":[{"_index":"swiss_candidates","_type":"person","_id":"AU3N1_EHqjpMTv_qUiFM","_score":3.0971029,"_source":{"owner":"richtalent@operamail.com","first":"Frankie","last":"Ng","city":"Hong Kong","designation":"Systems Engineer","email":"frankieng@systex.com.hk","phone":"phone","country":"China","industry":"Technology","tags":["remarks","virtualization big data"],"screened":["false","none","no date yet"]}},{"_index":"swiss_candidates","_type":"person","_id":"AU3N1_FeqjpMTv_qUiFW","_score":3.0971029,"_source":{"owner":"richtalent@operamail.com","first":"Panny","last":"Li","city":"Hong Kong","designation":"Client Services Manager","email":"pannyli1210@gmail.com","phone":"phone","country":"China","industry":"Technology","tags":["remarks","virtualization big data"],"screened":["false","none","no date yet"]}},{"_index":"swiss_candidates","_type":"person","_id":"AU3N1_sxqjpMTv_qUiK3","_score":3.0971029,"_source":{"owner":"richtalent@operamail.com","first":"Joseph","last":"Wong","city":"Hong Kong","designation":"Senior Sales Manager","email":"joseph.wong@datacraft-asia.com","phone":"phone","country":"China","industry":"Technology","tags":["remarks","virtualization big data"],"screened":["false","none","no date yet"]}},{"_index":"swiss_candidates","_type":"person","_id":"AU3N1_vbqjpMTv_qUiLS","_score":3.0971029,"_source":{"owner":"richtalent@operamail.com","first":"Pagena","last":"Yeung","city":"Hong Kong","designation":"Sales Director","email":"pagena@gmail.com","phone":"phone","country":"China","industry":"Technology","tags":["remarks","virtualization big data"],"screened":["false","none","no date yet"]}},{"_index":"swiss_candidates","_type":"person","_id":"AU3N1_sSqjpMTv_qUiKy","_score":2.9766436,"_source":{"owner":"richtalent@operamail.com","first":"Danielle","last":"Ho","city":"Hong Kong","designation":"Senior Pre-sales","email":"kphodaniel@yahoo.com.hk","phone":"phone","country":"China","industry":"Technology","tags":["remarks","virtualization big data"],"screened":["false","none","no date yet"]}},{"_index":"swiss_candidates","_type":"person","_id":"AU3N1_tUqjpMTv_qUiK9","_score":2.9766436,"_source":{"owner":"richtalent@operamail.com","first":"Robin","last":"Yu","city":"Hong Kong","designation":"Sales Director","email":"robinwhyu@gmail.com","phone":"phone","country":"China","industry":"Technology","tags":["remarks","virtualization big data"],"screened":["false","none","no date yet"]}},{"_index":"swiss_candidates","_type":"person","_id":"AU3N1_yhqjpMTv_qUiL0","_score":2.9766436,"_source":{"owner":"richtalent@operamail.com","first":"Karl","last":"Tsang","city":"Hong Kong","designation":"Storage Architect","email":"karltsang1001@gmail.com","phone":"phone","country":"China","industry":"Technology","tags":["remarks","virtualization big data"],"screened":["false","none","no date yet"]}},{"_index":"swiss_candidates","_type":"person","_id":"AU3N1_y0qjpMTv_qUiL4","_score":2.9766436,"_source":{"owner":"richtalent@operamail.com","first":"Kelvin","last":"Chan","city":"Hong Kong","designation":"Practice Consultant","email":"ctkelvin@yahoo.com.hk","phone":"phone","country":"China","industry":"Technology","tags":["remarks","virtualization big data"],"screened":["false","none","no date yet"]}},{"_index":"swiss_candidates","_type":"person","_id":"AU3N1_zEqjpMTv_qUiL8","_score":2.9766436,"_source":{"owner":"richtalent@operamail.com","first":"Anita","last":"Chan","city":"Hong Kong","designation":"Head Global Marketing","email":"chan_anl@yahoo.com.hk","phone":"phone","country":"China","industry":"Technology","tags":["remarks","virtualization big data"],"screened":["false","none","no date yet"]}},{"_index":"swiss_candidates","_type":"person","_id":"AU3N1_qQqjpMTv_qUiKf","_score":2.7742429,"_source":{"owner":"richtalent@operamail.com","first":"Maggie","last":"Poon","city":"Hong Kong","designation":"Account Executive","email":"poon_maggie@hotmail.com","phone":"phone","country":"China","industry":"Technology","tags":["remarks","virtualization big data"],"screened":["false","none","no date yet"]}}]}}

以下是产生它的ruby代码:

require "elasticsearch"
require "json"

search_term = "big data"
city = "Hong Kong"
client = Elasticsearch::Client.new log: true

h = JSON.parse(client.search index: 'swiss_candidates', body:
{
  query: {
    bool: {
      must: [
        {
          match: {
            tags: search_term
          }
        },
        {
          match: {
            city: city
          }
        }
      ]
    }
  }
})

以下是我尝试使用以下代码访问它的代码:

h.each do |r|
  puts r["hits"]["_id"]

end

并尝试了

h.each do |r|
 puts r["hits"]["hits"][0][_id]
end

puts r["hits"]["hits"][0]["_id"]

我尝试了以下变体:puts row["_id"]puts row["_source"]["city"],一般错误消息为no implicit conversion of Hash into String (TypeError)。我被困在这里的人,前进的最佳方式是什么?

我还尝试在第一个{之后}之前放置单引号但是,当我这样做时,搜索查询会立即抛出错误。

1 个答案:

答案 0 :(得分:0)

还有另一个级别的点击:

result["hits"]["hits"][0][_id]

{  
   "took":5,
   "timed_out":false,
   "_shards":{  
      "total":5,
      "successful":5,
      "failed":0
   },
   "hits":{  
      "total":40,
      "max_score":3.0971029,
      "hits":[  
         {  
            "_index":"swiss_candidates",
            "_type":"person",
            "_id":"AU3N1_EHqjpMTv_qUiFM",
            "_score":3.0971029,
            "_source":{  
               "owner":"richtalent@operamail.com",
               "first":"Frankie",