【问题标题】:Elasticsearch - Can't search using suggestion field ("is not a completion suggest field")Elasticsearch - 无法使用建议字段进行搜索(“不是完成建议字段”)
【发布时间】: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","re​​ason":"字段 [name_suggest] 是不是完成建议字段"}],"type":"search_phase_execution_exception","re​​ason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard" :0,"index":"trends","node":"YVcINtPlSU2u8R2lT9VxPQ","re​​ason":{"type":"illegal_argument_exception","re​​ason":"字段 [name_suggest] 不是完成建议字段"}} ],"caused_by":{"type":"illegal_argument_exception","re​​ason":"字段 [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


【解决方案1】:

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

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

【讨论】:

  • 就像我说的,我会在测试前删除所有数据。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多