Ruby on rails A使用弹性搜索和轨道4中的轮胎进行固定不起作用
我在rails 4应用程序中创建了一个搜索引擎,它运行得很好,但我很难让asciifolding过滤器正常工作。我的模型中有很多带有重音的术语,除非拼写完全正确,否则不会出现,即:我希望搜索“Rodriguez”以显示带有“Rodríguez”的结果。我尝试了许多不同的示例,但由于某些原因,当我用以下代码重置数据库时,搜索根本无法工作(我没有收到任何错误,但无论查询结果如何,都不会出现任何结果) 这是我的模型:Ruby on rails A使用弹性搜索和轨道4中的轮胎进行固定不起作用,ruby-on-rails,ruby,ruby-on-rails-4,elasticsearch,tire,Ruby On Rails,Ruby,Ruby On Rails 4,elasticsearch,Tire,我在rails 4应用程序中创建了一个搜索引擎,它运行得很好,但我很难让asciifolding过滤器正常工作。我的模型中有很多带有重音的术语,除非拼写完全正确,否则不会出现,即:我希望搜索“Rodriguez”以显示带有“Rodríguez”的结果。我尝试了许多不同的示例,但由于某些原因,当我用以下代码重置数据库时,搜索根本无法工作(我没有收到任何错误,但无论查询结果如何,都不会出现任何结果) 这是我的模型: class Product < ActiveRecord::Base
class Product < ActiveRecord::Base
include Tire::Model::Search
include Tire::Model::Callbacks
settings :analysis => {
:analyzer => {
:default => {
:tokenizer => "standard",
:filter => ["standard", "asciifolding"]
}
}
} do
mapping do
indexes :id, type: 'integer', index: :not_analyzed
indexes :name, boost: 5, analyzer: 'default'
indexes :website, index: :not_analyzed
indexes :price, type: 'integer', index: :not_analyzed
indexes :artist, boost: 3, analyzer: 'default'
indexes :company, boost: 4, analyzer: 'default'
indexes :date, type: 'date', index: :not_analyzed
end
end
def self.search(params)
tire.search(page: params[:page], per_page: 12) do
query { string params[:query], default_operator: "AND" } if params[:query].present?
end
end
然后我跑:
rake environment tire:import CLASS=Product FORCE=true
我看过很多不同的资源,包括elasticsearch和tire文档,但不管出于什么原因(我预计会有一个愚蠢的错误)小注,为了填充我的数据库,我已经导入了一个csv文件,但我不明白为什么这会影响任何事情,特别是考虑到在这种形式的搜索中没有出现任何问题(当我删除设置部分,只使用映射块和搜索方法时,它可以很好地解决重音问题).我需要打电话给某种轮胎吗?索引和导入?非常感谢您的帮助
更新
因此,我对搜索查询进行了编辑,修复了问题,但提出了一个新问题:
def self.search(params)
tire.search(page: params[:page], per_page: 12) do
query { string params[:query], analyzer: :search_analyzer, :default_field => 'name' } if params[:query].present?
end
end
通过识别默认字段,我现在可以搜索不区分重音的字段,但现在我的搜索仅限于名称,并且我无法接收以前工作过的其他索引属性的结果。有人知道如何设置多个默认字段吗?这是我在生产中使用的示例。这不是您的pr的解决方案有问题,但它会让你离开地面。我的代码用于,和。还要注意的是,从2013年9月开始,你应该使用,或者如果它是一个新项目。使用此代码,我可以自动完成类似于
Rod
findingRodríguez
的案例
class Profile
# ...
include Tire::Model::Persistence
# I'm also using ES as persistance storage.
include Tire::Model::DynamicPersistence
property :title, analyzer: 'snowball', type: :multi_field,
fields: {
title: {
type: 'string',
boost: 20.0,
search_analyzer: "autocomplete_search_analyzer",
index_analyzer: "autocomplete_indexer_analyzer"
},
completed: {
type: 'string',
boost: 15.0,
search_analyzer: "autocomplete_search_analyzer",
index_analyzer: "autocomplete_indexer_analyzer"
}
}
CONFIGURATION = {
settings: {
analysis: {
analyzer: {
shingle_analyzer: {
tokenizer: "keyword",
filter: ["icu_folding", "lowercase", "shingle_filter"]
},
sx_index_analyzer: {
tokenizer: "standard",
filter: ["icu_folding", "lowercase"]
},
sx_search_analyzer: {
tokenizer: "standard",
filter: ["icu_folding", "lowercase"]
}
},
filter: {
shingle_filter: {
type: "shingle",
min_shingle_size: 2,
max_shingle_size: 5
}
}
}
},
mappings: {
profile: {
"_all" => {
enabled: true,
index_analyzer: "sx_index_analyzer",
search_analyzer: "sx_search_analyzer"
},
properties: {
title: {
type: "multi_field",
fields: {
title: {
type: "string",
store: "yes",
boost: 20.0
#index_analyzer: "sx_index_analyzer",
#search_analyzer: "sx_search_analyzer"
},
sortable: {
type: "string",
index: "analyzed"
},
autocomplete: {
type: "string",
index_analyzer: "shingle_analyzer",
boost: 15.0
},
}
}
}
}
}
}
def self.rebuild_index
Tire.index Profile.index_name do
delete if Profile.index.exists?
create Profile::CONFIGURATION
end
end
end
希望有帮助!谢谢,我现在没有时间测试,但我会在可能的时候让你知道它是如何工作的!如果你发现这是正确的答案,请做适当的标记。谢谢。;)
class Profile
# ...
include Tire::Model::Persistence
# I'm also using ES as persistance storage.
include Tire::Model::DynamicPersistence
property :title, analyzer: 'snowball', type: :multi_field,
fields: {
title: {
type: 'string',
boost: 20.0,
search_analyzer: "autocomplete_search_analyzer",
index_analyzer: "autocomplete_indexer_analyzer"
},
completed: {
type: 'string',
boost: 15.0,
search_analyzer: "autocomplete_search_analyzer",
index_analyzer: "autocomplete_indexer_analyzer"
}
}
CONFIGURATION = {
settings: {
analysis: {
analyzer: {
shingle_analyzer: {
tokenizer: "keyword",
filter: ["icu_folding", "lowercase", "shingle_filter"]
},
sx_index_analyzer: {
tokenizer: "standard",
filter: ["icu_folding", "lowercase"]
},
sx_search_analyzer: {
tokenizer: "standard",
filter: ["icu_folding", "lowercase"]
}
},
filter: {
shingle_filter: {
type: "shingle",
min_shingle_size: 2,
max_shingle_size: 5
}
}
}
},
mappings: {
profile: {
"_all" => {
enabled: true,
index_analyzer: "sx_index_analyzer",
search_analyzer: "sx_search_analyzer"
},
properties: {
title: {
type: "multi_field",
fields: {
title: {
type: "string",
store: "yes",
boost: 20.0
#index_analyzer: "sx_index_analyzer",
#search_analyzer: "sx_search_analyzer"
},
sortable: {
type: "string",
index: "analyzed"
},
autocomplete: {
type: "string",
index_analyzer: "shingle_analyzer",
boost: 15.0
},
}
}
}
}
}
}
def self.rebuild_index
Tire.index Profile.index_name do
delete if Profile.index.exists?
create Profile::CONFIGURATION
end
end
end