ruby - 使用elasticsearch过滤带有空格的标签

标签 ruby mongoid search-engine elasticsearch tire

我正在使用带有 mongoid 的轮胎 (https://github.com/karmi/tire)。这是我的模型定义:

class SomethingWithTag
  include Mongoid::Document
  include Mongoid::Timestamps
  field :tags_array, type: Array

  include Tire::Model::Search
  include Tire::Model::Callbacks
  mapping do
      indexes :tags_array, type: :array, index: :not_analyzed
  end
end

假设我有一个文档 {tags_array: ["hello world"]}。然后以下查询可以正常工作:

SomethingWithTag.tire.search { filter :terms, :tags_array => ["hello"] }
SomethingWithTag.tire.search { filter :terms, :tags_array => ["world"] }
SomethingWithTag.tire.search { filter :terms, :tags_array => ["hello", "world"] }

但是以下内容不会返回任何结果:

SomethingWithTag.tire.search { filter :terms, :tags_array => ["hello world"] }

我应该怎么做才能让它发挥作用?

编辑:这里有一小段要测试的代码:http://pastebin.com/n1rUtK3e

最佳答案

问题已解决:

使用 keyword 分析器来分析 tags_array 属性:

class SomethingWithTag
  # ...
  mapping do
    indexes :tags_array, analyzer: 'keyword'
  end
end

关于ruby - 使用elasticsearch过滤带有空格的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11822019/

相关文章:

ajax - 我可以让我的 ajax 网站 'crawlable' 吗?

c# - 像 Google 搜索一样搜索组合框

ruby - 为什么 Ruby setter 需要 "self."类内的资格?

ruby-on-rails - 如何在 ubuntu 上的 LAMP 中的 Apache 中部署 Ruby On Rails 应用程序,而无需安装 Ruby 和 Rails

ruby-on-rails - 创建时从嵌入式文档访问父级(Mongoid)

ruby-on-rails - Mongoid::错误::混合关系

seo - 搜索引擎优化

ruby - 在 Ruby 中比较两个 lambdas/Procs

ruby-on-rails - 管理员和用户的 Pundit- Index 方法

solr - 维护 Sunspot Solr 索引的正确方法是什么?