我已启动并运行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($数据):
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
的这个“包”作为array
来处理,而是替代我过去几天寻找的其他数据类型。)
示例:
[tags] => Array
(
[0] => Some tag
[2] => Some other tag
[3] => Some other tag
)
//...
[tags] => Array
(
)
答案 0 :(得分:1)
在array_unique之后,返回的数组中有漏洞:在你的例子中,没有关键字1的项目。因此它被Elasticsearch索引为地图, 这不是你想要的行为。
在代码中使用array_merge(array_unique($tags))
,这将生成一个没有漏洞的数组,Elasticsearch将对其进行索引。
您必须在重新索引之前删除索引以重新生成映射;)