PHP Elasticsearch "Set" 映射

PHP Elasticsearch "Set" mapping

我有一个Elastic服务器(版本2.3.1),运行我尝试使用官方的PHP client来存储文档。我在 PHP:

中创建的结构
$data = Array(
  "index"=> "my_index",
  "type" => "my_type",
  "id"   => $my_generated_id,
  "body" => Array(
    "name"        => $name, 
    "other_field" => $other_data,
    "tags"        => array_unique($tags)
  )
);

print_r($data):

Array
(
    [index] => my_index
    [type] => my_type
    [id] => AFCDEFGH
    [body] => Array
        (
            [name] => It is my name
            [other_field] =>  some other information 
            [tags] => Array
                (
                    [0] => Some tag
                    [2] => Some other tag
                )
        )
)

第一个文档存储得很好(它创建了索引)但是当我尝试插入一个具有不同标签数量的文档时,我得到了一些错误:

PHP Fatal error:  Uncaught Elasticsearch\Common\Exceptions\BadRequest400Exception: {"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"object mapping for [tags] tried to parse field [null] as object, but found a concrete value"}],"type":"mapper_parsing_exception","reason":"object mapping for [tags] tried to parse field [null] as object, but found a concrete value"},"status":400} in /home/test/test/php/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php:655

我知道索引建错了:

{
   "my_index":{
      "aliases":{
      },
      "mappings":{
         "targy":{
            "properties":{
               "other_field":{
                  "type":"string"
               },
               "name":{
                  "type":"string"
               },
               "tags":{
                  "properties":{
                     "0":{
                        "type":"string"
                     },
                     "2":{
                        "type":"string"
                     }
                  }
               }
            }
         }
      },
      "settings":{
         "index":{
            "creation_date":"1477414478664",
            "number_of_shards":"5",
            "number_of_replicas":"1",
            "uuid":"yZiN4uUgRXe9vyaN4uWbGg",
            "version":{
               "created":"2030199"
            }
         }
      },
      "warmers":{
      }
   }
}

如何在 PHP 中手动建立映射以便能够同时处理零个或多个标签? (我认为解决方案不是将 strings 的 "bag" 作为 array 来处理,而是用过去几天我一直在寻找的其他数据类型来处理。)

示例:

[tags] => Array
          (
             [0] => Some tag
             [2] => Some other tag
             [3] => Some other tag
          )
//...
[tags] => Array
          (
          )

在 array_unique 之后,返回的数组中有空洞:在您的示例中没有键 1 的项目。因此它被 Elasticsearch 索引为地图, 这不是您想要的行为。

在您的代码中使用 array_merge(array_unique($tags)),这将生成一个没有空洞的数组,Elasticsearch 将对其进行索引。

在重建索引以重新生成映射之前,您必须删除索引 ;)