我花了几个小时试图解决这个问题。我希望有人能帮助我。
首先,这里有一些代码来说明这个结构:
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"
}
]
}'为了得到结果,我尝试了很多方法。什么都不管用。看来我一定漏掉了一些基本的东西。
发布于 2012-12-30 17:51:52
您正在对"addresses.street“字段使用term查询,该查询不会被分析,因此您必须传入小写的etc值。
尝试使用match查询。
https://stackoverflow.com/questions/14082535
复制相似问题