using elasticsearch to filter through tags with wh

2019-04-02 09:33发布

问题:

I am using tire (https://github.com/karmi/tire) with mongoid. Here is my model definition:

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

Say I have a document {tags_array: ["hello world"]}. Then the following queries work fine:

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"] }

But the following doesn't return any results:

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

What should I do to make it work?

Edit: here's a small piece of code to test: http://pastebin.com/n1rUtK3e

回答1:

Issue solved at :

Use the keyword analyzer for the tags_array property:

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