【发布时间】:2014-10-24 12:14:20
【问题描述】:
我想在我的 Rails 项目中添加 char_filter html_strip。但我不确定如何以及在哪里使用(重新)轮胎宝石。我什至不确定我这样做的方式是否可行。
我目前的代码:
include Tire::Model::Search
include Tire::Model::Callbacks
mapping do
indexes :author, type: 'string'
indexes :content, type: 'string', index_options: "offsets", analyzer: 'snowball', language: "French", char_filter: 'html_strip'
indexes :name, type: 'string', index_options: "offsets", analyzer: 'snowball', language: "French", :boost => 5
indexes :topic_id, type: :integer, :index => :not_analyzed
end
def self.search params
query = params[:query]
tire.search do
query do
boolean minimum_number_should_match: 1 do
should { string query, fields: [:name], default_operator: "AND", analyzer: 'snowball' }
should { string query, fields: [:content], default_operator: "AND", analyzer: 'snowball' }
should { string query, fields: [:author], default_operator: "AND" }
must { range :topic_id, gt: 0 }
end
end
highlight :content, :author, :name, options: {pre_tags: ['<em style="background-color: yellow">'], post_tags: ['</em>'], :number_of_fragments => 50}
end
end
我不太确定如何实现它。我尝试了很多东西,但现在没有结果!
谢谢!
编辑
我按照 IS04 的回答将代码更改为:
settings analysis: {
analyzer: {
html_analyzer: {
type: 'custom',
tokenizer: 'standard',
filter: ['classic'],
char_filter: ['html_strip']
}
}
} do
mapping do
indexes :author, type: 'string'
indexes :content, type: 'string', index_options: "offsets", analyzer: 'html_analyzer', search_analyzer: 'snowball', language: "French"
indexes :name, type: 'string', index_options: "offsets", search_analyzer: 'snowball', language: "French", boost: 5
indexes :topic_id, type: :integer, index: :not_analyzed
end
end
我把它贴在这里,如果有一天它对某人有帮助:)
【问题讨论】:
-
或许值得解释一下您的尝试以及“无结果”的含义。 IE。这是否意味着搜索
content在您搜索 html 标签时仍会返回结果(这表明过滤器不起作用) - 或者content在从 es 返回时仍包含 html 标签(这表明您期待分析要返回的内容)。 -
您的解决方案是否适用于任何版本的 Elasticsearch?
标签: ruby-on-rails elasticsearch tire