我花了好几个小时试图解决这个问题。我希望有人能帮助我。
首先,这里有一些代码来说明这个结构: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"
}
]
}'
我尝试了很多方法来得到结果。毫无效果。看来我一定错过了一些基本的东西。
您正在使用term
查询"地址"。Street "字段,它不被分析,所以你必须传入小写的等值。
尝试使用match
查询