首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Elastica添加文档覆盖现有文档

Elastica添加文档覆盖现有文档
EN

Stack Overflow用户
提问于 2015-07-06 11:53:33
回答 2查看 899关注 0票数 0

当我试图将新文档添加到索引类型时,我会松散现有文档,这些文档被新添加的文档覆盖。该问题可能与每个添加的文档的id有关??

以下是代码:

代码语言:javascript
复制
     $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();

拜托,我需要你的帮助。谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-07-06 18:27:52

在此用例中使用uniqid的PHP 不建议。既然你想要一个随机的,安全的身份,那就让Elasticsearch为你做吧。Elastica 文献构造法注意到id字段是可选的。所以不要传递它,让Elasticsearch发出id。

票数 3
EN

Stack Overflow用户

发布于 2019-06-09 13:37:51

几样东西$弹性指数->创建(.)你只需要输入一次。在创建索引之后,索引是唯一的,您可以对索引进行注释或生成不同的索引和其他内容。我留下一个有用的例子。

代码语言:javascript
复制
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();
}

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31245173

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档