gpt4 book ai didi

ruby - Elasticsearch-无法使用建议字段(“is not a completion suggest field”进行搜索)

转载 作者:行者123 更新时间:2023-12-03 01:48:32 25 4
gpt4 key购买 nike

在 Elasticsearch 中,我将记录存储在trends命名空间和trend主题中。这些是仅带有name(字符串)和id的简单对象。

我想通过自动完成功能按名称搜索,为此,我尝试使用索引搜索。

这是将索引添加到名称列的代码。在这里,我使用name_suggest列,在其中插入与name列相同的数据,并且也在查询中使用它。

  def self.index_column_mappings
elasticsearch.index({
index: "trends",
type: "trend",
body: {
mappings: {
trend: {
properties: {
name: {
type: "string"
},
name_suggest: {
type: "completion"
}
}
}
}
}
})
end

从I DELETE *开始。然后,我添加此列映射并添加一些记录。接下来,我运行此命令进行搜索:
  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] {"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"Field [name_suggest] is not a completion suggest field"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"trends","node":"YVcINtPlSU2u8R2lT9VxPQ","reason":{"type":"illegal_argument_exception","reason":"Field [name_suggest] is not a completion suggest field"}}],"caused_by":{"type":"illegal_argument_exception","reason":"Field [name_suggest] is not a completion suggest field"}},"status":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:in `__raise_transport_error'



似乎重要的部分是: 字段[name_suggest]不是完成建议字段。我不明白我如何无法设置该字段。

顺便说一句我正在使用 Elasticsearch 5.x

为了回应评论,这里是 GET /trends/_mapping的结果:
{
"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
}
}
}
}
}
}
}
}

最佳答案

最终答案。
您正在使用错误的方法来索引数据。寻找example here

 def self.index_column_mappings
client.indices.create({
index: "trends",
body: {
mappings: {
trend: {
properties: {
name: { type: 'string' },
name_suggest: { type: 'completion' }
}
}
}
}
})
end

关于ruby - Elasticsearch-无法使用建议字段(“is not a completion suggest field”进行搜索),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42150417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com