【发布时间】:2017-06-28 06:32:15
【问题描述】:
在弹性搜索中,我将记录存储在 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
从我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":"字段 [name_suggest] 是不是完成建议字段"}],"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":"字段 [name_suggest] 不是完成建议字段"}} ],"caused_by":{"type":"illegal_argument_exception","reason":"字段 [name_suggest] 不是完成建议字段"}},"status":400} 来自/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] 不是完成建议字段。我不明白我是如何未能设置字段的。
顺便说一句,我正在使用 Elastic search 5.x
响应 cmets 这里是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
}
}
}
}
}
}
}
}
【问题讨论】:
-
你能发布你现在拥有的地图吗?
-
N 你不是,它的代码放置了映射。在浏览器中打开 SERVER:9200/trends/_mapping?pretty
-
@VolodymyrBilyachat 查看更新
-
您看到映射错误了吗?正如我在回答中所说,删除 INDEX,然后确保创建索引,然后开始索引您的数据
-
我不确定 ruby 是如何工作的,也许它是异步的,所以你可以同时发送两个请求。
标签: ruby elasticsearch