ruby-on-rails - ElasticSearch + Tire如何强制条件返回与字段相同的值

标签 ruby-on-rails search elasticsearch

我有国家列表,我希望用户能够按县对结果进行排序。所以我有这个 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

结束

并在模型中:
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



facet['term']



始终以小写形式返回国家/地区名称。我可以使用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

关于ruby-on-rails - ElasticSearch + Tire如何强制条件返回与字段相同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19506555/

相关文章:

ruby-on-rails - Rails PostgreSQL - 更改套接字名称/位置

elasticsearch - 使用 OSGi 包时 java.util.ServiceConfigurationError Provider 不是子类型

linux - 运行 ElasticSearch。 Bash : ./bin/elasticsearch:权限被拒绝

Jquery 在 iframe 中搜索文本

search - Sitecore 7 : Sorting Lucene results by field

php/mysql 搜索多个值

python - ElasticSearch 更新不是立即的,你如何等待 ElasticSearch 完成更新它的索引?

jquery - 使用 jQuery.html 方法将 Div 设置为带有跨度的 Rails link_to

mysql - 如何查询日期范围 A 或日期范围 B 内的记录

ruby-on-rails - Rails .include?在不应该的时候返回真