public function testGetOptions() { $document = new Document(); $document->setIndex('index'); $document->setOpType('create'); $document->setParent('2'); $document->setId(1); $options = $document->getOptions(array('index', 'type', 'id', 'parent')); $this->assertInternalType('array', $options); $this->assertEquals(3, count($options)); $this->assertArrayHasKey('index', $options); $this->assertArrayHasKey('id', $options); $this->assertArrayHasKey('parent', $options); $this->assertEquals('index', $options['index']); $this->assertEquals(1, $options['id']); $this->assertEquals('2', $options['parent']); $this->assertArrayNotHasKey('type', $options); $this->assertArrayNotHasKey('op_type', $options); $this->assertArrayNotHasKey('_index', $options); $this->assertArrayNotHasKey('_id', $options); $this->assertArrayNotHasKey('_parent', $options); $options = $document->getOptions(array('parent', 'op_type', 'percolate'), true); $this->assertInternalType('array', $options); $this->assertEquals(2, count($options)); $this->assertArrayHasKey('_parent', $options); $this->assertArrayHasKey('_op_type', $options); $this->assertEquals('2', $options['_parent']); $this->assertEquals('create', $options['_op_type']); $this->assertArrayNotHasKey('percolate', $options); $this->assertArrayNotHasKey('op_type', $options); $this->assertArrayNotHasKey('parent', $options); }
/** * Partial Update * * @param string $docId * @param array $data * @param integer $version * * @throws IndexingException * * @return null */ public function update($docId, array $data, $version = 1) { $document = new Document(); $document->setData($data); $document->setId($docId); $document->setDocAsUpsert(true); try { $this->getType($version)->updateDocument($document); } catch (\Exception $e) { throw new IndexingException('Throw exception while updating', $e->getCode(), $e); } }
/** * Insert the repository objects in the type index * * @param \Closure $loggerClosure * @param array $options */ public function populate(\Closure $loggerClosure = null, array $options = array()) { if ($loggerClosure) { $loggerClosure('Indexing movies'); } $allMovies = $this->movieManager->getAllMovies(); $languages = $this->getLanguagesAvailable(); foreach ($allMovies as $movie) { $document = new Document(); $id = $movie->getId(); $document->setId($id); $titleFr = $this->movieManager->getMovieTitleInLocale($id, $languages['fr']); $titleEn = $this->movieManager->getMovieTitleInLocale($id, $languages['en']); // $titleEn = $this->movieManager->getMovieTitleInLocale($id, 'en'); // $titleFr = $this->movieManager->getMovieTitleInLocale($id, 'fr'); $document->setData(array('id' => $id, 'title_fr' => $titleFr['title'], 'title_en' => $titleEn['title'])); $this->movieType->addDocuments(array($document)); } }
/** * Adds the given document to the search index * * @param \Elastica\Document $doc Document with data * @return \Elastica\Response */ public function addDocument(Document $doc) { $path = urlencode($doc->getId()); $type = Request::PUT; // If id is empty, POST has to be used to automatically create id if (empty($path)) { $type = Request::POST; } $options = $doc->getOptions(array('version', 'version_type', 'routing', 'percolate', 'parent', 'ttl', 'timestamp', 'op_type', 'consistency', 'replication', 'refresh', 'timeout')); $response = $this->request($path, $type, $doc->getData(), $options); $data = $response->getData(); // set autogenerated id to document if (($doc->isAutoPopulate() || $this->getIndex()->getClient()->getConfigValue(array('document', 'autoPopulate'), false)) && $response->isOk()) { if (!$doc->hasId()) { if (isset($data['_id'])) { $doc->setId($data['_id']); } } if (isset($data['_version'])) { $doc->setVersion($data['_version']); } } return $response; }