简体   繁体   中英

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

I have list of countries and I whant users be able to sort results by county. So I have this helper:

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

end

and in model:

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

but

facet['term']

always return country name in lowercase. I could make it with Country.find(facet).name but I think it is unnecessary. Is there any way to store in facet same string value as in field?

Updated

my mapping:

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

      }
    } ... }}}

Your mapping is not created right, you can try to reindex your data.

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

And after that you can try to run Model.import again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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