0
我花了幾個小時試圖解決問題。我希望有一個人可以幫助我。ElasticSearch&Tire。搜索具有嵌套屬性的對象
首先,這裏的一些代碼來說明結構:
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"
}
]
}'
我試過很多事情要得到的結果毫無效果看來我一定是錯過了什麼BA SIC。
感謝您的回答。不幸的是它不會改變任何東西。我嘗試了很多東西。通過API。我也嘗試過濾器。沒有工作。我試圖實現的是'Company.includes(:addresses).where(地址:{street:params [:location]})' – Eru