问题描述
任何人都可以提供以下建议?
Can anyone provide advice on the following please?
我正在使用searchkick / elasticsearch,并希望在多个字段中搜索关键词或术语(名称,制造商)。所以例如,如果我寻找一个名为myproduct的产品使我的sommanufacturer,如果我搜索myproduct,sompersonufacturer或myproduct sommanufacturer,我会期望看到这个结果,因为这两个术语都包括在
I'm using searchkick / elasticsearch and would like to search for a key term or terms across multiple fields (name, manufacturer). So for example if im looking for a product called "myproduct" made my "somemanufacturer" i'd expect to see this result appear if I search either "myproduct", "somemanufacturer" or "myproduct somemanufacturer" as both these terms are included either in name or manufacturer fields.
我的问题如下:
@products = Product.search query
允许上面列出的所有搜索字词,并返回预期结果,但是很快,我添加
Allows all the search terms listed above and returns expected result however as soon as I add
@products = Product.search query, fields: [:name, :manufacturer_name]
它只会返回myproduct或somecompany的结果,而不是myproduct somecompany。
It will only return a result for "myproduct", or "somecompany", but not "myproduct somecompany".
现在这不是一个大问题,因为我可以完全删除字段选项,但我需要使用searchkicks word_start作为名称字段。所以我的最终查询是这样的:
Now this isn't a big deal as I can remove the fields option entirely BUT I need to utilise searchkicks word_start for the name field. So my final query is something like this:
@products = Product.search query, fields: [{name: :word_start}, :manufacturer_name]
我希望用户搜索第一个字符串的产品,要输入制造商,例如myprod somecompany不幸的是,当我希望它会返回由somecompany制造的名为myproduct的产品时,这会返回零结果。
I'd like users to search for the 1st string of the product and be able to enter a manufacturer too eg "myprod somecompany" unfortunately this returns zero results when I was hoping it would return the product named myproduct, made by somecompany.
我错过了一些真的很明显吗我可以更改添加
Am i missing something really obvious here? I can change add
operator: 'or'
但是我真的希望能够搜索名称,添加附加条款,如果两者都存在,则返回特定记录。
but really i want to be able to part search on the name, add additional terms and if both are present for a particular record it gets returned.
我的模型代码也
class Product < ActiveRecord::Base
searchkick word_start: [:name]
end
谢谢
推荐答案
如果您的所有字段共享相同的分析器,您可以使用 elasticsearch
功能称为 cross_fields
。如果不是这样,可以使用 query_string
。不幸的是, searchkick
不支持 cross_fields
和 query_string
。所以,你必须自己做。
If all of your fields share the same analyzer, you can use elasticsearch
feature called cross_fields
. If it is not the case, you can use query_string
. Unfortunately searchkick
does not support cross_fields
and query_string
yet. So, you have to do it by yourself.
索引(不同的分析器)
searchkick merge_mappings: true, mappings: {
product: {
properties: {
name: {
type: 'string',
analyzer: 'searchkick_word_start_index',
copy_to: 'grouped'
},
manufacturer_name: {
type: 'string',
analyzer: 'default_index',
copy_to: 'grouped'
},
grouped: {
raw: {type: 'string', index: 'not_analyzed'}
}
}
}
}
使用cross_fields搜索
Search with cross_fields
@products = Product.search(body: {
query: {
multi_match: {
query: query,
type: "cross_fields",
operator: "and",
fields: [
"name",
"manufacturer_name",
"grouped",
]
}
}
}
使用query_string搜索
@products = Product.search(body: {
query: {
query_string: {
query: query,
default_operator: "AND",
fields: [
"name",
"manufacturer_name",
"grouped",
]
}
}
}
更新
- 将我的答案更改为使用不同的分析器和多个字段遵循此。
遗憾的是将查询传递给 elasticsearch
由你自己失去 searchkick
功能,如高亮和转换,但您仍然可以将其添加到 elasticsearch
查询中。
Unfortunately passing the query to elasticsearch
by yourself you lose searchkick
features like highlight and conversions, but, you can still do it, adding it to the elasticsearch
query.
向查询添加hightlight
@products = Product.search(body: {
query: {
...
}, highlight: {
fields: {
name: {},
manufacturer_name: {}
}
}
向查询添加转化
@products = Product.search(body: {
query: {
bool: {
must: {
dis_max: {
queries: {
query_string: {
...
}
}
}
},
should: {
nested: {
path: 'conversions',
score_mode: 'sum',
query: {
function_score: {
boost_mode: 'replace',
query: {
match: {
"conversions.query": query
}
},
field_value_factor: {
field: 'conversions.count'
}
}
}
}
}
}
}
这篇关于当指定字段时,SearchKick不会搜索多个术语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!