我正在将 Ruby on Rails 与 elasticsearch-rails gem 一起使用,并且我正在尝试使用同义词过滤器。我一直在关注此处发布的问题以获取指导(我的实现按预期工作,除了同义词部分):
https://github.com/elastic/elasticsearch-rails/issues/63
这是我的代码:
settings index: { number_of_shards: 1 },
analysis: {
filter: {
synonym: {
type: "synonym",
ignore_case: true,
synonyms:[
"roller,wheel"
]
}
},
analyzer: {
synonym: {
tokenizer: "whitespace",
filter: ["synonym", "lowercase", "stop", "snowball"]
}
}
} do
mappings dynamic: 'false' do
indexes :name, analyzer: 'synonym'
indexes :status, analyzer: 'english'
#indexes :description, analyzer: 'english'
indexes :part_number, analyzer: 'english'
indexes :text, analyzer: 'english'
indexes :normal_model, type: 'nested' do
indexes :name, analyzer: 'english'
indexes :number, analyzer: 'english'
indexes :machine_type, analyzer: 'english'
indexes :normal_brand, type: 'nested' do
indexes :name, analyzer: 'english'
end
end
end
end
这是我在控制器操作中搜索的代码:
@products = Product.search(
query: {
query_string: {
#query: "*manual* AND status:\"Disabled\""
#fields: ["normal_model.name", "normal_brand.name"],
query: "*#{params[:q]}* AND status:\"Viewable On Website & Backend\""
#query: "*" + params[:q]+ "*"
}
}
)
我有名称字段设置为“wheel”的记录,但是当我搜索“roller”时,我得到 0 个结果并且没有错误。我希望此时检索名称为“wheel”的记录。我还完全删除了索引,并确认它已被删除并重新创建了我的索引,以确保我不只是面临索引问题。我现在不确定该怎么做。任何帮助,将不胜感激。
这也是我的 as_indexed_json 方法
def as_indexed_json(options={})
as_json(
only: [:name, :description, :part_number, :url_key, :image, :price, :shipping, :warranty, :eta, :status, :sku],
include: {
normal_model: { only: [:name, :number, :machine_type],
include: {
normal_brand: { only: :name}
}
}
}
)
end
谢谢
更新:
我还尝试将以下代码(建议在下面的答案中)添加到我的控制器搜索操作中。
fields: ['name', '_all'],
query: "#{params[:q]} AND status:\"Viewable On Website & Backend\""
我在搜索操作中用这个代码代替了我的原始代码,但是当我搜索“roller”这个词时,这仍然没有产生任何结果。我仍然可以搜索“wheel”并检索几个结果,但我对指定的同义词没有运气。
更新:
这是在产品名称字段中包含单词“wheel”的文档之一。
{
"_index" : "products",
"_type" : "product",
"_id" : "288374",
"_version" : 1,
"found" : true,
"_source" : {
"name" : "wheel",
"description" : "This is the O.E.M. wheel for the Spirit CE800 Elliptical with a model number 800049.",
"shipping" : null,
"sku" : "58511",
"eta" : "3 to 5 Business Days",
"warranty" : "1 Year",
"part_number" : "N/A",
"url_key" : "spirit-ce800-elliptical-model-800049-lubricant",
"price" : 19.99,
"image" : "noimage-main_20837.jpg",
"status" : "Viewable On Website & Backend",
"normal_model" : {
"name" : "CE800",
"number" : "800049",
"machine_type" : "Elliptical",
"normal_brand" : {
"name" : "Spirit"
}
}
}
}
更新:
这是我的产品映射
{
"products" : {
"mappings" : {
"product" : {
"dynamic" : "false",
"properties" : {
"name" : {
"type" : "text",
"analyzer" : "synonym"
},
"normal_model" : {
"type" : "nested",
"properties" : {
"machine_type" : {
"type" : "text",
"analyzer" : "english"
},
"name" : {
"type" : "text",
"analyzer" : "english"
},
"normal_brand" : {
"type" : "nested",
"properties" : {
"name" : {
"type" : "text",
"analyzer" : "english"
}
}
},
"number" : {
"type" : "text",
"analyzer" : "english"
}
}
},
"part_number" : {
"type" : "text",
"analyzer" : "english"
},
"status" : {
"type" : "text",
"analyzer" : "english"
},
"text" : {
"type" : "text",
"analyzer" : "english"
}
}
}
}
}
}