我花了几个小时试图解决这个问题。我希望有一个人可以帮助我。
首先,这里有一些代码来说明结构:
class Company < ActiveRecord::Base
include Tire::Model::Search
include Tire::Model::Callbacks
attr_accessible :name, :website, :addresses_attributes
has_many :addresses, dependent: :destroy
accepts_nested_attributes_for :addresses, allow_destroy: true
after_touch() { tire.update_index }
include_root_in_json = false
mapping do
indexes :name, boost: 10
indexes :addresses do
indexes :street
end
end
def self.search(params)
s = tire.search(load: true) do
query do
boolean do
must { string params[:query] } if params[:query]
must { term "addresses.street", params[:location] } if params[:location]
end
end
s.results
end
def to_indexed_json
to_json( include: { addresses: { only: [:street] } } )
end
end
class Address < ActiveRecord::Base
# Attributes
attr_accessible :street
# Associations
belongs_to :company, touch: true
end
当我试图搜索具有特定街道名称的公司时(打电话Company.search({location: 'example_street_name', query: 'example_name'})
我没有得到任何结果。
索引对我来说看起来不错。这是请求之一:
curl -X POST "http://localhost:9200/companies/company/1351" -d '{
"created_at": "2012-12-29T13:41:17Z",
"name": "test name",
"updated_at": "2012-12-29T13:41:17Z",
"website": "text.website.com",
"addresses": [
{
"street": "test street name"
}
]
}'
我已经尝试了很多事情来获得结果。没有任何效果。看来我一定错过了一些基本的东西。