Ruby Elasticsearch-无法使用建议字段进行搜索(“不是完成建议字段”)
在弹性搜索中,我将记录存储在趋势名称空间和趋势主题中。这些是仅具有名称字符串和id的简单对象 我想通过自动完成按名称搜索,为此我尝试使用索引搜索 下面是我将索引添加到name列的代码。在这里,我使用一个name\u suggest列,在其中插入与name列相同的数据,并在查询中使用Ruby Elasticsearch-无法使用建议字段进行搜索(“不是完成建议字段”),ruby,elasticsearch,Ruby,elasticsearch,在弹性搜索中,我将记录存储在趋势名称空间和趋势主题中。这些是仅具有名称字符串和id的简单对象 我想通过自动完成按名称搜索,为此我尝试使用索引搜索 下面是我将索引添加到name列的代码。在这里,我使用一个name\u suggest列,在其中插入与name列相同的数据,并在查询中使用 def self.index_column_mappings elasticsearch.index({ index: "trends", type: "trend",
def self.index_column_mappings
elasticsearch.index({
index: "trends",
type: "trend",
body: {
mappings: {
trend: {
properties: {
name: {
type: "string"
},
name_suggest: {
type: "completion"
}
}
}
}
}
})
end
首先,我删除*。然后我添加这个列映射并添加一些记录。下一步,我运行以下命令进行搜索:
def suggest(term)
@client.search({
index: "trends",
type: "trend",
body: {
suggest: {
name_suggest: {
prefix: term,
completion: {
field: "name_suggest"
}
}
}
}
})
end
我得到以下错误:
Elasticsearch::Transport::Transport::Errors::BadRequest:[400]{错误:{根本原因:[{类型:非法参数\异常,原因:字段[name \ U suggest]不是完成建议字段}],类型:搜索\阶段\执行\异常,原因:所有碎片失败,阶段:查询,分组:true,失败\碎片:[{碎片:0,索引:趋势,节点:YVcINtPlSU2u8R2lT9VxPQ,原因:{类型:非法参数\异常,原因:字段[名称\建议]不是完成建议字段}],原因:{类型:非法参数\异常,原因:字段[名称\建议]不是完成建议字段},状态:400}
from/home/max/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/elasticsearch-transport-5.0.3/lib/elasticsearch/transport/transport/base.rb:201:“\uu升高\传输\错误”中
看起来重要的部分是:字段[name_suggest]不是一个完成建议字段。我不明白我是如何设置该字段的
顺便说一句,我使用的是弹性搜索5.x
下面是GET/trends/\u映射的结果,以回应评论:
最后的答案。
您使用了错误的方法来索引数据。请查找
就像我说的,我在测试前删除了所有数据。你能发布你现在拥有的mappin吗?你不是,它的代码将映射。在浏览器服务器中打开:9200/trends/\u mapping?pretty@VolodymyrBilyachat查看更新是否看到映射错误?正如我在回答中所说的,删除索引,然后确保索引已创建,然后开始索引数据我不确定ruby是如何工作的,也许它是异步的,所以你可以同时发送两个请求。
{
"trends": {
"mappings": {
"trend": {
"properties": {
"id": {
"type": "long"
},
"mappings": {
"properties": {
"trend": {
"properties": {
"properties": {
"properties": {
"name": {
"properties": {
"type": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
},
"name_suggest": {
"properties": {
"type": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}
}
}
},
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"name_suggest": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}
def self.index_column_mappings
client.indices.create({
index: "trends",
body: {
mappings: {
trend: {
properties: {
name: { type: 'string' },
name_suggest: { type: 'completion' }
}
}
}
}
})
end