簡體   English   中英

ElasticSearch + Tire如何強制條件返回與字段相同的值

[英]ElasticSearch + Tire how to force term to return same value as in field

我有國家列表,我希望用戶能夠按縣對結果進行排序。 所以我有這個幫手:

def facets_for model, field
ul = ""
links = ""
model.facets[field]['terms'].each do |facet|
  links << content_tag('li') do
    link_to("#{facet['term']} #{facet['count']}", params.merge(field => facet['term']))
  end
end
ul << content_tag("ul", class: field) do
  links.html_safe
end
ul.html_safe

結束

並在模型中:

class model
 ....
mapping do 
 indexes :country do
   indexes :name, :type => :string, index: "not_analyzed"
 end
end




def self.search params
  ...
  filter :term, destination: params[:destination] if params[:destination].present?
  facet("destination") {  terms 'country.name' }
  ...
end

面[“術語”]

始終以小寫形式返回國家/地區名稱。 我可以使用Country.find(facet).name做到這一點,但我認為這是不必要的。 有什么辦法可以存儲與字段中相同的字符串值?

更新

我的映射:

  {
      "wishes" : {
        "wish" : {
          "properties" : {
            "body" : {
              "type" : "string"
            },
            "country" : {
              "properties" : {
                "name" : {
                  "type" : "string"
                }
              }
            },
            "country_id" : {
              "type" : "string"
            },
            "created_at" : {
              "type" : "date",
              "format" : "dateOptionalTime"

      }
    } ... }}}

您的映射創建不正確,您可以嘗試重新索引數據。

Model.index.delete               # to delete index with bad mapping
Model.create_elasticsearch_index # this should create index with mapping defined in Model

之后,您可以嘗試再次運行Model.import

暫無
暫無

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

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