简体   繁体   English

ElasticSearch&Tire。 搜索具有嵌套属性的对象

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

I've spent several hours trying to solve the problem. 我花了几个小时试图解决问题。 I hope someone can help me. 我希望有一个人可以帮助我。

First, here's some code to illustrate the structure: 首先,这里有一些代码来说明结构:

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

When I'm trying to search for company with particular street name (calling Company.search({location: 'example_street_name', query: 'example_name'}) I get no results. 当我尝试搜索具有特定街道名称的公司时(调用Company.search({location: 'example_street_name', query: 'example_name'})我得不到任何结果。

Indexing looks fine for me. 索引对我来说很好看。 That's one of the requests: 这是其中一个要求:

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

I've tried many things to get the results. 我已经尝试了很多东西来获得结果。 Nothing worked. 没有任何效果。 It seems I must have missed something basic. 看来我一定错过了一些基本的东西。

You're using a term query for the "addresses.street" field, which is not analyzed, so you must pass in lowercased etc. values. 您正在对“addresses.street”字段使用term查询,该字段未进行分析,因此您必须传入较小的等值。

Try using the match query. 尝试使用match查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM