簡體   English   中英

ElasticSearch和Tire / Rails:對單個構面使用兩個字段

[英]ElasticSearch and tire/Rails: use two fields for a single facet

將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

任何提示,不勝感激!

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

根據Elasticsearch文檔中有關方面方面的術語http://www.elasticsearch.org/guide/reference/api/search/facets/terms-facet.html,這應該是可能的:

多字段:

可以針對多個字段執行術語構面,從而在這些字段之間返回聚合結果。 例如:

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

您是否嘗試為術語方面提供一系列字段,例如terms :country1, :country2

這似乎可行,但我需要對其進行更多測試:facet('country'){術語字段:[:country1,:country2]}

暫無
暫無

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

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