从 elastic v1.7 到 elastic 7.X 问题的建议者重新索引问题

re-indexing problem with suggesters from elastic v1.7 to elastic 7.X problem

我对重建索引有疑问,我不能 100% 确定我找到的解决方案是最好的,因为我们在使用来自 java 客户端的索引时遇到问题。所以我想确认重新索引的步骤是否正确:

问题描述: 我在旧的 v1.7 上有一个索引,需要迁移到使用上下文建议的 elasticsearch v7.x。

GET /autocompleteterms_v20/_search?pretty 
{
 "took" : 2,
 "timed_out" : false,
 "_shards" : {
 "total" : 1,
 "successful" : 1,
 "failed" : 0
},
 "hits" : {
 "total" : 4130,
 "max_score" : 1.0,
 "hits" : [ {
 "_index" : "autocompleteterms_v20",
 "_type" : "autoitem",
 "_id" : "en_incorporated_by_reference",
 "_score" : 1.0,
 "_source":{ "name": "incorporated by reference", "name_suggest": { "input": "incorporated by reference", "context": { "lang": "en" }, "weight": 393 } }
 }, {
 "_index" : "autocompleteterms_v20",
 "_type" : "autoitem",
 "_id" : "en_double_gaming",
 "_score" : 1.0,
 "_source":{ "name": "double gaming", "name_suggest": { "input": "double gaming", "context": { 
 "lang": "en" }, "weight": 371 } }
 }, {
 "_index" : "autocompleteterms_v20",
"_type" : "autoitem",
"_id" : "en_checking_for_players",
"_score" : 1.0,
"_source":{ "name": "checking for players", "name_suggest": { "input": "checking for players", 
 "context": { "lang": "en" }, "weight": 2 } }
},

GET /autocompleteterms_v20/_mapping?pretty
{
"autocompleteterms_v20" : {
"mappings" : {
"autoitem" : {
"properties" : {
"name" : {
"type" : "string"
},
"name_suggest" : {
"type" : "completion",
"analyzer" : "standard",
"payloads" : true,
"preserve_separators" : false,
"preserve_position_increments" : false,
"max_input_length" : 50,
 "context" : {
 "lang" : {
 "type" : "category",
 "path" : "lang_field",
 "default" : [ "en" ]
  }
 }
 }
 }
 }
 }
 }
 }

为了重新索引到新集群,我发现建议者在 v5 中有一些重大变化,所以当尝试在重新索引上转换索引字段时

  1. 首先我创建映射:

    PUT autocompleteterms_v20 {
     "settings": {
     "index": {
     "number_of_shards": 2,
     "number_of_replicas": 0
     }
    },
    "mappings": {
     "properties": {
     "name": {
      "type": "text"
    },
     "name_suggest": {
    "type": "completion",
    "analyzer": "standard",
    "preserve_separators": false,
    "preserve_position_increments": false,
    "max_input_length": 50,
    "contexts": [
    {
     "name": "lang",
     "type": "category",
     "path": "lang_field"
      }
    ]
    }
    }
    }
    }
    {
     "acknowledged" : true,
     "shards_acknowledged" : true,
     "index" : "autocompleteterms_v20"
    }
    
  2. 之后我重新索引将字段上下文更改为上下文:

     POST _reindex {
     "source": {
     "remote": {
      "host": http://x.x.x.x:9200,
      "username": "user",
      "password": "password"
     },
     "index": "autocompleteterms_v20"
     },
     "dest": {
     "index": "autocompleteterms_v20"
    },
    "script": {
    "source": """ 
    ctx._source.name_suggest.contexts = ctx._source.name_suggest.context;
    ctx._source['name_suggest'].remove('context');
    """,
    "lang": "painless"
    }
    }
    {
    "took" : 366,
    "timed_out" : false,
    "total" : 4130,
    "updated" : 0,
    "created" : 4130,
    "deleted" : 0,
    "batches" : 5,
    "version_conflicts" : 0,
    "noops" : 0,
    "retries" : {
    "bulk" : 0,
    "search" : 0
    },
    "throttled_millis" : 0,
    "requests_per_second" : -1.0,
    "throttled_until_millis" : 0,
    "failures" : [ ]
    }
    
  3. 当我使用显式上下文进行查询时,我得到的结果是:

     POST /autocompleteterms_v20/_search {
     "suggest": {
     "text": "spirit",
     "completion": {
     "field": "name_suggest",
     "contexts": {
     "lang": [ 
        { "context": "en" }
       ]
       }
      }
      }
      }
      }
      {
      "took" : 5,
      "timed_out" : false,
      "_shards" : {
      "total" : 2,
      "successful" : 2,
      "skipped" : 0,
      "failed" : 0
      },
      "hits" : {
      "total" : {
      "value" : 0,
      "relation" : "eq"
      },
      "max_score" : null,
      "hits" : [ ]
      },
      "suggest" : {
      "completeMe" : [
      {
      "text" : "spirit",
      "offset" : 0,
      "length" : 6,
      "options" : [
      {
       "text" : "spirit",
       "_index" : "autocompleteterms_v20",
       "_type" : "_doc",
       "_id" : "en_spirit",
       "_score" : 290.0,
       "_source" : {
       "name_suggest" : {
       "input" : "spirit",
       "weight" : 290,
       "contexts" : {
       "lang" : "en"
        }
       },
       "name" : "spirit"
       },
       "contexts" : {
       "lang" : [
       "en"
      ]
      }
      },
      {
     "text" : "spirit of the game",
     "_index" : "autocompleteterms_v20",
     "_type" : "_doc",
     "_id" : "en_spirit_of_the_game",
     "_score" : 34.0,
     "_source" : {
     "name_suggest" : {
      "input" : "spirit of the game",
     "weight" : 34,
     "contexts" : {
     "lang" : "en"
     }
    },
    "name" : "spirit of the game"
     },
    "contexts" : {
     "lang" : [
        "en"
       ]
       }
     }
    
  4. 但是当我在没有上下文的情况下进行查询时,我得到一个错误:

     POST /autocompleteterms_v20/_search
     {
     "suggest": {
      "completeMe": {
      "text": "spirit",
      "completion": {
      "field": "name_suggest"
      }
     }
    }
    }
     {
      "error" : {
       "root_cause" : [
      {
       "type" : "illegal_argument_exception",
       "reason" : "Missing mandatory contexts in context query"
      }
      ],
      "type" : "search_phase_execution_exception",
     "reason" : "all shards failed",
     "phase" : "query",
     "grouped" : true,
     "failed_shards" : [
     {
     "shard" : 0,
     "index" : "autocompleteterms_v20",
     "node" : "B7EpKWRVRzGkLCnl7CZapQ",
     "reason" : {
     "type" : "illegal_argument_exception",
     "reason" : "Missing mandatory contexts in context query"
     }
     }
      ],
     "caused_by" : {
    "type" : "illegal_argument_exception",
    "reason" : "Missing mandatory contexts in context query",
    "caused_by" : {
    "type" : "illegal_argument_exception",
    "reason" : "Missing mandatory contexts in context query"
     }
     }
      },
      "status" : 400
    }
    

但是在 v5.0 的重大变化中我们有 https://www.elastic.co/guide/en/elasticsearch/reference/5.0/breaking_50_suggester.html#_completion_mapping_with_multiple_contexts

提到: “没有上下文的新查询针对启用上下文的完成字段会产生来自所有索引建议的结果”

在这种情况下,我有两个主要问题:

  1. 旧索引的重新索引是否正确完成?
  2. 如何在不针对该索引选择上下文的情况下进行查询?
  1. 是的,你做对了一切!

  2. 你不能,因为在 ES 7.0 中还有另一个 suggester change 强制提供上下文,否则性能会下降

Context Completion Suggester

The ability to query and index context enabled suggestions without context, deprecated in 6.x, has been removed. Context enabled suggestion queries without contexts have to visit every suggestion, which degrades the search performance considerably.