首页 文章

Elasticsearch - 无法使用建议字段进行搜索(“不是完成建议字段”)

提问于
浏览
0

在弹性搜索中,我将记录存储在 trends 命名空间和 trend 主题中 . 这些是简单的对象,只有 name (字符串)和 id .

我想通过自动填充按名称搜索,为此我正在尝试使用索引搜索 .

这里's my code to add the index to the name column. Here I' m使用 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”:“Field [name_suggest]不是完成建议字段“}],”类型“:”search_phase_execution_exception“,”reason“:”所有分片失败“,”阶段“:”查询“,”分组“:true,”failed_shards“:[{”shard“:0 ,“index”:“trends”,“node”:“YVcINtPlSU2u8R2lT9VxPQ”,“reason”:{“type”:“illegal_argument_exception”,“reason”:“Field [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'

看来重要的是这个: Field [name_suggest] is not a completion suggest field . 我没有't understand how I' m未能设置该字段 .

顺便说一下,我正在使用弹性搜索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
                     }
                  }
               }
            }
         }
      }
   }
}

1 回答

  • 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
    

相关问题