gpt4 book ai didi

php - Elastica添加文档将覆盖现有文档

转载 作者:行者123 更新时间:2023-12-03 00:42:31 26 4
gpt4 key购买 nike

当我尝试将新文档添加到索引类型时,我会丢失现有文档,这些文档将被新添加的文档覆盖。该问题可能与每个已添加文档的ID有关。

这是代码:

     $elasticaClient = new \Elastica\Client(array(
'host' => $this->container->getParameter('elastic_host'),
'port' => $this->container->getParameter('elastic_port')
));
$elasticaIndex = $elasticaClient->getIndex('app');
$elasticaIndex->create(
array(
'number_of_shards' => 4,
'number_of_replicas' => 1,
'analysis' => array(
'analyzer' => array(
'indexAnalyzer' => array(
'type' => 'custom',
'tokenizer' => 'standard',
'filter' => array('lowercase', 'mySnowball')
),
'searchAnalyzer' => array(
'type' => 'custom',
'tokenizer' => 'standard',
'filter' => array('standard', 'lowercase', 'mySnowball')
)
),
'filter' => array(
'mySnowball' => array(
'type' => 'snowball',
'language' => 'German'
)
)
)
),
true
);
$elasticaType = $elasticaIndex->getType('type');
$mapping = new \Elastica\Type\Mapping();
$mapping->setType($elasticaType);
$mapping->setParam('index_analyzer', 'indexAnalyzer');
$mapping->setParam('search_analyzer', 'searchAnalyzer');
$mapping->setProperties(array(
'id' => array('type' => 'string'),
'title' => array('type' => 'string'),
'duration' => array('type' => 'string'),
'start' => array('type' => 'string'),
'end' => array('type' => 'string'),
));

// Send mapping to type
$mapping->send();

$documents = array();
foreach($medias as $media) {
$id = uniqid() ;
$documents[] = new \Elastica\Document(
$id,
array(
'id' => $id,
'title' => $media['title'],
'duration' => $media['duration'],
'start' => $media['start'],
'end' => $media['end'],

)
);
}

$elasticaType->addDocuments($documents);
$elasticaType->getIndex()->refresh();

拜托我需要你的帮忙 。谢谢

最佳答案

此用例使用uniqid的PHP does not recommend。由于您需要一个随机,安全的ID,因此让Elasticsearch为您完成。 Elastica Document construct method指出id字段是可选的。因此,请不要传递它,而让Elasticsearch发布ID。

关于php - Elastica添加文档将覆盖现有文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31245173/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com