我有equity_contracts表,其中有一个user_contract和许多pricesEquityContract模型如下所示:

class EquityContract < ActiveRecord::Base
  validates_presence_of :ticker, :name, :country, :currency

  has_one :user_contract, as: :contract
  has_many :prices

  searchkick

  scope :search_import, -> { includes(:user_contract, :prices) }

  def search_data
    {
      name: name,
      ticker: ticker,
      country: country,
      user_id: user_contract.try(:user_id),
      prices: prices.map do |price|
        {
          traded_on: price.traded_on.to_s,
          close: price.close
        }
      end
    }
  end
end
UserContract如下所示:
class UserContract < ActiveRecord::Base
  belongs_to :user
  belongs_to :contract, polymorphic: true
end

现在我想索引name中的键(tickercountryuser_idpricesEquityContract),以便运行EquityContract.reindex

现在,当我尝试做类似equity = EquityContract.search "APPL_US"的操作时,我得到了Searchkick::Results对象,看起来像这样:
[24] pry(main)> equity = EquityContract.search "APPL_US", limit: 1
ETHON: performed EASY effective_url=http://127.0.0.1:9200/equity_contracts_development/_search response_code=200 return_code=ok total_time=0.101687
  EquityContract Search (109.5ms)  curl http://127.0.0.1:9200/equity_contracts_development/_search?pretty -d '{"query":{"dis_max":{"queries":[{"match":{"_all":{"query":"APPL_US","operator":"and","boost":10,"analyzer":"searchkick_search"}}},{"match":{"_all":{"query":"APPL_US","operator":"and","boost":10,"analyzer":"searchkick_search2"}}},{"match":{"_all":{"query":"APPL_US","operator":"and","boost":1,"analyzer":"searchkick_search","fuzziness":1,"prefix_length":0,"max_expansions":3}}},{"match":{"_all":{"query":"APPL_US","operator":"and","boost":1,"analyzer":"searchkick_search2","fuzziness":1,"prefix_length":0,"max_expansions":3}}}]}},"size":1,"from":0,"fields":[]}'
=> #<Searchkick::Results:0x0000036f6915a0
 @klass=
  EquityContract(id: integer, ticker: text, name: string, country: string, currency: string, created_at: datetime, updated_at: datetime),
 @options=
  {:page=>1,
   :per_page=>1,
   :padding=>0,
   :load=>true,
   :includes=>nil,
   :json=>false,
   :match_suffix=>"analyzed",
   :highlighted_fields=>[]},
 @response=
  {"took"=>99,
   "timed_out"=>false,
   "_shards"=>{"total"=>5, "successful"=>5, "failed"=>0},
   "hits"=>
    {"total"=>3,
     "max_score"=>0.005845671,
     "hits"=>
      [{"_index"=>"equity_contracts_development_20160317185615341",
        "_type"=>"equity_contract",
        "_id"=>"234",
        "_score"=>0.005845671}]}}>

这里的主要问题是,我想像以前一样查看索引结果,并查看字段的结果(nametickercountryuser_idprices)。

当我尝试通过RESTful API访问ElasticSearch时:
curl http://127.0.0.1:9200/equity_contracts_development/_search\?pretty -d '{"query":{"filtered":{"query":{"match_all":{}},"filter":{"and":[{"not":{"filter":{"missing":{"field":"user_id","existence":true,"null_value":true}}}}]}}},"size":100,"from":0,"fields":[]}'

我得到的结果是:
{
  "took" : 54,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 207,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "equity_contracts_development_20160317185615341",
      "_type" : "equity_contract",
      "_id" : "64",
      "_score" : 1.0
    }, {
      "_index" : "equity_contracts_development_20160317185615341",
      "_type" : "equity_contract",
      "_id" : "83",
      "_score" : 1.0
    }, {
      "_index" : "equity_contracts_development_20160317185615341",
      "_type" : "equity_contract",
      "_id" : "90",
      "_score" : 1.0
    }, {
      "_index" : "equity_contracts_development_20160317185615341",
      "_type" : "equity_contract",
      "_id" : "127",
      "_score" : 1.0
    }, {
      "_index" : "equity_contracts_development_20160317185615341",
      "_type" : "equity_contract",
      "_id" : "139",
      "_score" : 1.0
    }, {
      "_index" : "equity_contracts_development_20160317185615341",
      "_type" : "equity_contract",
      "_id" : "590",
      "_score" : 1.0
    }, {
      "_index" : "equity_contracts_development_20160317185615341",
      "_type" : "equity_contract",
      "_id" : "608",
      "_score" : 1.0
    }, {
      "_index" : "equity_contracts_development_20160317185615341",
      "_type" : "equity_contract",
      "_id" : "622",
      "_score" : 1.0
    }, {
      "_index" : "equity_contracts_development_20160317185615341",
      "_type" : "equity_contract",
      "_id" : "658",
      "_score" : 1.0
    }, {
      "_index" : "equity_contracts_development_20160317185615341",
      "_type" : "equity_contract",
      "_id" : "665",
      "_score" : 1.0
    }, {
      "_index" : "equity_contracts_development_20160317185615341",
      "_type" : "equity_contract",
      "_id" : "672",
      "_score" : 1.0
    }]
  }
}

如何查看相关的映射对象的结果,例如prices中的结果?目前,equity.results返回 Activity 记录对象EquityContract的数组,而不返回索引属性。

我想返回EquityContract#search_data中指定的属性,并尝试equity.response['hits']['hits']位,其中不包含所需的属性。有任何想法吗?

最佳答案

看来我必须将load: false添加到#search中,例如:

equity = EquityContract.search "APPL_US", limit: 1, load: false

然后,我将获得带有响应的_source,其中包括属性。您可以将source=true添加到HTTP请求中,并且应该获得响应的完整属性。

10-05 20:31