PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 23, 2022

[FIXED] Elastica add documents overrides existing ones

 January 23, 2022     elastica, elasticsearch, php, symfony     No comments   

Issue

When i try to add new documents to an index type , i loose existing documents which are overwritten by the new added ones . The problem can be related to the id of each added document ??

Here is the code :

     $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();

Please i need your help . Thank you


Solution

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 notes that the id field is optional. So don't pass it and let Elasticsearch issue the id.



Answered By - hubbardr
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing