karmi/tire — Issue #206: how to do exact match with elasticsearch Chinese analysis plugin?
Author: hau
Created: 2012-01-15 16:18:40+00:00
State: open

I was using the https://github.com/elasticsearch/elasticsearch-analysis-smartcn plugin to perform full text search in Chinese, and the tire gem in a rails application.

I had a activerecord model `Article` which has a string content and many tags. Tags could be written in Chinese and I want to do exact match on the tags. I created this mapping inside of `Article`:
```ruby
class Article < ActiveRecord::Base
include Tire::Model::Search
include Tire::Model::Callbacks
settings analysis: {
filter: {
smartcn_word: {
'type' => 'smartcn_word'
}
},
analyzer: {
smart_chinese: {
'tokenizer' => 'smartcn_sentence',
'filter' => ['smartcn_word'],
'type' => 'smart_chinese'
}
}
} do
mapping do
indexes :content, type: 'string', analyzer: 'smart_chinese'
indexes :tags, type: 'string', index: 'not_analyzed'
end
end
end
```
where `Article`'s `tags` is an array of strings.

I had an `Article` with `tags`: `["irresistible"]` and another `Article` with `tags`: `["恭喜发财"]`. I wanted to search by exact matching on the `tags`. When I did
```ruby
Article.search("resistible")
```
, it correctly returned zero match. But if I searched by
```ruby
Article.search("发财")
```
, it returned one match. So, it's doing exact matching for English but a substring match in Chinese. How can I make it also do exact matching for Chinese?

Thanks for your help.