Elastica 添加文档覆盖现有文档

Elastica add documents overrides existing ones

当我尝试将新文档添加到索引类型时,我丢失了被新添加的文档覆盖的现有文档。问题可能与每个添加文档的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();

我需要你的帮助。谢谢

PHP does not recommend using uniqid for this use case. Since you are wanting a random, safe id, let Elasticsearch do it for you. The Elastica Document construct method 注意 id 字段是可选的。所以不传,让Elasticsearch发出id。

几件事 $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();
}

}