簡體   English   中英

ElasticSearch&Tire。 搜索具有嵌套屬性的對象

[英]ElasticSearch & Tire. Search for an object with nested attributes

我花了幾個小時試圖解決問題。 我希望有一個人可以幫助我。

首先,這里有一些代碼來說明結構:

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"
    }
  ]
}'

我已經嘗試了很多東西來獲得結果。 沒有任何效果。 看來我一定錯過了一些基本的東西。

您正在對“addresses.street”字段使用term查詢,該字段未進行分析,因此您必須傳入較小的等值。

嘗試使用match查詢。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM