Elasticsearch似乎没有按预期工作 [英] Elasticsearch does not seem to be working as expected
问题描述
我正在使用Elasticsearch并将全球化的gem进行全球化以进行全文搜索,我期望我可以使用czech/english分析器来搜索供应商名称和本地化描述.
I am using elasticsearch and globalize gems for full text searching and what I expect is that I can search for supplier name, localised description using czech/english analyzer.
示例:供应商名称:"Bonami.cz"供应商描述_CZ:捷克语中的测试描述."
Example: Supplier Name: "Bonami.cz" Supplier Description_CZ: "Test description in czech."
当我搜索"Bonami.cz"时,它可以工作,但是当我搜索时,它不起作用(0个结果):
It works when I search for "Bonami.cz", but it does not work (0 results) when I search for:
- "Bonami"(单词的一部分)
- 测试"(说明)
基于文档,以下方法应该可以使用,但是显然我错过了一些东西.我确认索引和数据在ElasticSearch中.
Based on documentation, the below methods should work, but apparently I have missed something. I verified the indexes and data is in ElasticSearch.
在模型中使用czech/english分析器之前,还需要以某种方式安装它吗?
Also do I need to install somehow the czech/english analyzer before using it in the model?
require 'elasticsearch/model'
require 'activerecord-import'
class Supplier < ActiveRecord::Base
after_commit lambda { __elasticsearch__.index_document }, on: :create
after_commit lambda { __elasticsearch__.update_document }, on: :update
translates :description, :fallbacks_for_empty_translations => true
accepts_nested_attributes_for :translations
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
include Elasticsearch::Model::Globalize::MultipleFields
mapping do
indexes :id, type: 'integer'
indexes :name, analyzer: 'czech'
indexes :description_ma, analyzer: 'czech'
indexes :description_cs, analyzer: 'czech'
indexes :description_en, analyzer: 'english'
end
def as_indexed_json(options={})
{ id: id,
name: name,
description_ma: description_ma,
description_cs: description_cs,
description_en: description_en
}
end
def self.search(query)
__elasticsearch__.search(
{
query: {
multi_match: {
query: query,
fields: ['name^10', 'description_ma', 'description_cs', 'description_en']
}
}
})
end
end
任何主意,怎么了?谢谢,米罗斯拉夫
Any idea, what is wrong? Thanks, Miroslav
更新1 我从第4条轨,elasticsearch-rails 中获得了解决方案的启发,但是当我尝试立即搜索时,对于任何一个字,我总是得到零结果.
UPDATE 1 I inspired with the solution from Rails 4, elasticsearch-rails, but when I try to search now, for any word I always get zero results.
settings index: {
number_of_shards: 1,
analysis: {
filter: {
trigrams_filter: {
type: 'ngram',
min_gram: 2,
max_gram: 10
},
content_filter: {
type: 'ngram',
min_gram: 4,
max_gram: 20
}
},
analyzer: {
index_trigrams_analyzer: {
type: 'custom',
tokenizer: 'standard',
filter: ['lowercase', 'trigrams_filter']
},
search_trigrams_analyzer: {
type: 'custom',
tokenizer: 'whitespace',
filter: ['lowercase']
},
english: {
tokenizer: 'standard',
filter: ['standard', 'lowercase', 'content_filter']
},
czech: {
tokenizer: 'standard',
filter: ['standard','lowercase','content_filter']
}
}
}
} do
mappings dynamic: 'false' do
indexes :name, index_analyzer: 'index_trigrams_analyzer', search_analyzer: 'search_trigrams_analyzer'
indexes :description_en, index_analyzer: 'english', search_analyzer: 'english'
indexes :description_ma, index_analyzer: 'czech', search_analyzer: 'czech'
indexes :description_cs, index_analyzer: 'czech', search_analyzer: 'czech'
end
end
def as_indexed_json(options={})
{ id: id,
name: name,
description_ma: description_ma,
description_cs: description_cs,
description_en: description_en
}
end
def self.search(query)
__elasticsearch__.search(
{
query: {
multi_match: {
query: query,
fields: ['name^10', 'description_ma', 'description_cs', 'description_en']
}
},
highlight: {
pre_tags: ['<em>'],
post_tags: ['</em>'],
fields: {
name: {},
description_ma: {},
description_cs: {},
description_en: {}
}
}
}
)
end
这是打开给定模型的弹性搜索URL时看到的:
This is what I see when I open elastic search URL for the given model:
{"suppliers":{"aliases":{},"mappings":{"supplier":
{"dynamic":"false","properties":{"description_cs":
{"type":"string","analyzer":"czech"},"description_en":
{"type":"string","analyzer":"english"},"description_ma":
{"type":"string","analyzer":"czech"},"name":
{"type":"string","index_analyzer":"index_trigrams_analyzer","search_analyzer":"search_trigrams_analyzer"}}}},"settings":{"index":
{"creation_date":"1445797508427","analysis":{"filter":
{"trigrams_filter":
{"type":"ngram","min_gram":"2","max_gram":"10"},"content_filter":
{"type":"ngram","min_gram":"4","max_gram":"20"}},"analyzer":{"english":
{"filter":["standard","lowercase","content_filter"],"tokenizer":"standard"},"index_trigrams_analyzer":{"type":"custom","filter":["lowercase","trigrams_filter"],"tokenizer":"standard"},"search_trigrams_analyzer":{"type":"custom","filter":["lowercase"],"tokenizer":"whitespace"},"czech":{"filter":["standard","lowercase","content_filter"],"tokenizer":"standard"}}},"number_of_shards":"1","number_of_replicas":"1","version":
{"created":"1060099"},"uuid":"wX9kf3OQSva24Iwk7sZ8AQ"}},"warmers":{}}}
更新2
缺少使它按预期工作的两个步骤=>1.重新导入模型数据2.在描述字段的名称中输入错误(而不是description_ma/en/cs,我必须使用ma/cs/en_description.
Two steps were missing to have it working as expected => 1. Re-import model data 2. Typo in names of description fields (instead of description_ma/en/cs, I had to use ma/cs/en_description.
settings index: {
number_of_shards: 1,
analysis: {
filter: {
trigrams_filter: {
type: 'ngram',
min_gram: 2,
max_gram: 10
},
content_filter: {
type: 'ngram',
min_gram: 4,
max_gram: 20
}
},
analyzer: {
index_trigrams_analyzer: {
type: 'custom',
tokenizer: 'standard',
filter: ['lowercase', 'trigrams_filter']
},
search_trigrams_analyzer: {
type: 'custom',
tokenizer: 'whitespace',
filter: ['lowercase']
},
english: {
tokenizer: 'standard',
filter: ['standard', 'lowercase', 'content_filter']
},
czech: {
tokenizer: 'standard',
filter: ['standard','lowercase','content_filter' ]
}
}
}
} do
mappings dynamic: 'false' do
indexes :name, index_analyzer: 'index_trigrams_analyzer', search_analyzer: 'search_trigrams_analyzer'
indexes :en_description, index_analyzer: 'english', search_analyzer: 'english'
indexes :ma_description, index_analyzer: 'czech', search_analyzer: 'czech'
indexes :cs_description, index_analyzer: 'czech', search_analyzer: 'czech'
end
end
def as_indexed_json(options={})
{ id: id,
name: name,
ma_description: ma_description,
cs_description: cs_description,
en_description: en_description
}
end
def self.search(query)
__elasticsearch__.search(
{
query: {
multi_match: {
query: query,
fields: ['name^10', 'ma_description', 'cs_description', 'en_description']
}
},
highlight: {
pre_tags: ['<em>'],
post_tags: ['</em>'],
fields: {
name: {},
ma_description: {},
cs_description: {},
en_description: {}
}
}
}
)
end
推荐答案
为了能够执行搜索,您将尝试使用Ngram分析器.(如评论中所述)
In order to be able to perform the search you are trying to do you'll to use the Ngram analyzer. (as discussed in the comments)
这篇关于Elasticsearch似乎没有按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!