[英]rails tire gem elasticsearch not working properly with will_paginate
[英]Asciifolding with Elasticsearch and Tire in Rails 4 not Working
我在我的rails 4應用程序中創建了一個搜索引擎,它工作得很好,但是我很難讓asciifolding過濾器工作。 我的模型有很多帶有重音的術語,除非它們拼寫正確,否則不會出現,即:我想搜索“Rodriguez”以顯示“Rodríguez”的結果。 我試圖遵循許多不同的例子,但出於某種原因,當我使用以下代碼重置我的數據庫時,搜索根本不起作用(我沒有收到錯誤,但無論查詢是什么都沒有出現)。
這是我的模型:
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 db:setup
然后我跑:
rake environment tire:import CLASS=Product FORCE=true
我已經查看過許多不同的資源,包括彈性搜索和輪胎文檔,但無論出於何種原因(我預計會犯一個愚蠢的錯誤),它根本行不通。 小注意,為了填充我的數據庫我一直在導入一個csv文件,但我不明白為什么這會影響任何東西,特別是考慮到這個形式的搜索中沒有任何東西出現(當我刪除時,它的工作正常沒有重音問題設置部分,只有映射塊和搜索方法)。 我是否需要調用某種Tire.index並導入? 任何幫助表示贊賞!
UPDATE
所以我對搜索查詢進行了編輯,修復了問題,但提出了一個新問題:
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
通過識別默認字段,我現在可以搜索重音不可知,但現在我的搜索僅限於名稱,我無法接收以前工作的其他索引屬性的結果。 有誰知道如何設置多個默認字段?
這是我在制作中使用的示例。 這不是你的問題的解決方案 ,但它會讓你開始。 我的代碼使用icu_folding , shingle_filter , multi_field和Tire的 ICU插件 。 另請注意,自2013年9月起, Tire已退役 ,如果是新項目,您應該使用elasticsearch-ruby或elasticsearch-rails 。 使用此代碼,我可以使用像Rod
找到Rodrí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
希望能幫助到你!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.