【问题标题】:ElasticSearch and tire/Rails: use two fields for a single facetElasticSearch 和轮胎/Rails:对单个方面使用两个字段
【发布时间】:2012-06-10 19:00:42
【问题描述】:

将 Elasticsearch 与 Rails 3 和轮胎 gem 结合使用。

我在几个领域都有工作需要,但我现在有一个特殊要求,不确定是否可行。 我的模型项目中有两个字段都存储相同的值:Country1 和 Country2

用户最多可以为一个项目存储两个国家/地区。两者的下拉菜单相同。两个字段都不是必需的。

我想要的是一个“合并”来自 Country1 和 Country2 的值​​并智能地处理点击这些方面的单一方面(即无论是在 1 还是在 2 中都会找到它)

到目前为止,这是我的模型:(注意 Country1/2 可以是多个单词)

class Project < ActiveRecord::Base
  mapping do
    indexes :id
    indexes :title, :boost => 100
    indexes :subtitle
    indexes :country1, :type => 'string', :index => 'not_analyzed'
    indexes :country2, :type => 'string', :index => 'not_analyzed'
  end
  def self.search(params)
    tire.search(load: true, page: params[:page], per_page: 10) do
    query do
      boolean do
        must { string params[:query], default_operator: "AND" } if params[:query].present?
        must { term :country1, params[:country] } if params[:country].present?
      end
    end
    sort { by :display_type, "desc" }
    facet "country" do
      terms :country1
    end

  end
end

非常感谢任何提示!

【问题讨论】:

    标签: ruby-on-rails search elasticsearch tire


    【解决方案1】:

    Tire 中的此提交 https://github.com/karmi/tire/commit/730813f 支持在“术语”方面聚合多个字段。

    界面是:

    Tire.search('articles-test') do
      query { string 'foo' }
      # Pass fields as an array, not string
      facet('multi') { terms ['bar', 'baz'] }
    end
    

    【讨论】:

    • 感谢卡雷尔,喜欢它的发展速度
    • 有没有办法让轮胎返回不止一个术语?例如,如果我为用户模型使用 facet,并且 facet 将返回一个 user_id 和一个名称,这样我就不必通过数据库查找用户的名称。
    【解决方案2】:

    这似乎可行,但我需要进行更多测试:facet('country') { terms fields: [:country1, :country2]}

    【讨论】:

      【解决方案3】:

      根据术语 facet http://www.elasticsearch.org/guide/reference/api/search/facets/terms-facet.html 的弹性搜索文档,这应该是可能的:

      多字段:

      术语 facet 可以针对多个字段执行,返回 这些字段的聚合结果。例如:

      {
          "query" : {
              "match_all" : {  }
          },
          "facets" : {
              "tag" : {
                  "terms" : {
                      "fields" : ["tag1", "tag2"],
                      "size" : 10
                  }
              }
          }
      }
      

      您是否尝试为诸如terms :country1, :country2 之类的术语构面提供一组字段?

      【讨论】:

      • 谢谢,但是没有...使用“terms :country1, :country2”会出错(未定义的方法 `delete' 用于 :country2:Symbol)并使用“terms [:country1, :country2]”显示一个空白方面...(无值)
      • 这似乎可行,但我需要进行更多测试:facet('country') { terms fields: [:country1, :country2]}
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-26
      • 2013-09-26
      • 2013-01-01
      • 1970-01-01
      • 2013-07-21
      • 2014-10-24
      • 2012-03-29
      相关资源
      最近更新 更多