当我尝试将新文档添加到索引类型时,我松开了现有文档,这些文档被新添加的文档覆盖。问题可能与每个添加文件的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();
我需要你的帮助。谢谢
答案 0 :(得分:3)
PHP does not recommend使用uniqid用于此用例。既然你想要一个随机的,安全的id,让Elasticsearch为你做。 Elastica Document construct method注意到id字段是可选的。所以不要通过它,让Elasticsearch发出id。
答案 1 :(得分:0)
几件事 $ elasticaIndex-> create(....),只需输入一次。创建索引后,索引是唯一的,您可以对其进行注释或生成其他索引以及其他内容。我留下一个可行的例子。
class PersistencyElastic
{
private $ conection;
public function __construct ()
{
$ this-> conection = new \ Elastica \ Client (['host' => '127.0.0.1', 'port' => 9200]);
}
public function save ($ msg)
{
// $ msg is an array with whatever you want inside
$ index = $ this-> conection-> getIndex ('googlephotos');
// This is the index I created, it's called googlephotos
// $ index-> create (array (), true);
$ type = $ index-> getType ('googlephotos');
$ type-> addDocument (new Document (uniqid ('id _', false), $ msg, $ type, $ index));
$ index-> refresh ();
}
}