ElasticSearch + Tire如何强制术语返回与字段中相同的值

时间:2013-10-21 23:27:27

标签: ruby-on-rails search elasticsearch

我有国家列表,我希望用户能够按县排序结果。所以我有这个帮手:

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来实现它,但我认为这是不必要的。有没有办法在facet中存储与字段相同的字符串值?

更新

我的映射:

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

      }
    } ... }}}

1 个答案:

答案 0 :(得分:0)

您的映射未正确创建,您可以尝试重新索引数据。

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

之后,您可以尝试再次运行Model.import