public function testParent() { $index = $this->_createIndex(); $typeBlog = new Type($index, 'blog'); $typeComment = new Type($index, 'comment'); $mapping = new Mapping(); $mapping->setParam('_parent', array('type' => 'blog')); $typeComment->setMapping($mapping); $entry1 = new Document(1); $entry1->set('title', 'Hello world'); $typeBlog->addDocument($entry1); $entry2 = new Document(2); $entry2->set('title', 'Foo bar'); $typeBlog->addDocument($entry2); $entry3 = new Document(3); $entry3->set('title', 'Till dawn'); $typeBlog->addDocument($entry3); $comment = new Document(1); $comment->set('author', 'Max'); $comment->setParent(2); // Entry Foo bar $typeComment->addDocument($comment); $index->optimize(); $query = new HasChild('Max', 'comment'); $resultSet = $typeBlog->search($query); $this->assertEquals(1, $resultSet->count()); $this->assertEquals(array('title' => 'Foo bar'), $resultSet->current()->getData()); }
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); }
/** * @group functional */ public function testHasParent() { $index = $this->_createIndex(); $shopType = $index->getType('shop'); $productType = $index->getType('product'); $mapping = new Mapping(); $mapping->setParent('shop'); $productType->setMapping($mapping); $shopType->addDocuments(array(new Document('zurich', array('brand' => 'google')), new Document('london', array('brand' => 'apple')))); $doc1 = new Document(1, array('device' => 'chromebook')); $doc1->setParent('zurich'); $doc2 = new Document(2, array('device' => 'macmini')); $doc2->setParent('london'); $productType->addDocument($doc1); $productType->addDocument($doc2); $index->refresh(); // All documents $parentQuery = new HasParent(new MatchAll(), $shopType->getName()); $search = new Search($index->getClient()); $results = $search->search($parentQuery); $this->assertEquals(2, $results->count()); $match = new Match(); $match->setField('brand', 'google'); $parentQuery = new HasParent($match, $shopType->getName()); $search = new Search($index->getClient()); $results = $search->search($parentQuery); $this->assertEquals(1, $results->count()); $result = $results->current(); $data = $result->getData(); $this->assertEquals($data['device'], 'chromebook'); }
protected function _getTestIndex() { $index = $this->_createIndex('has_child_test'); $parentType = $index->getType('parent'); $childType = $index->getType('child'); $childMapping = new Mapping($childType); $childMapping->setParent('parent'); $childMapping->send(); $altType = $index->getType('alt'); $altDoc = new Document('alt1', array('name' => 'altname')); $altType->addDocument($altDoc); $parent1 = new Document('parent1', array('id' => 'parent1', 'user' => 'parent1', 'email' => '*****@*****.**')); $parentType->addDocument($parent1); $parent2 = new Document('parent2', array('id' => 'parent2', 'user' => 'parent2', 'email' => '*****@*****.**')); $parentType->addDocument($parent2); $child1 = new Document('child1', array('id' => 'child1', 'user' => 'child1', 'email' => '*****@*****.**')); $child1->setParent('parent1'); $childType->addDocument($child1); $child2 = new Document('child2', array('id' => 'child2', 'user' => 'child2', 'email' => '*****@*****.**')); $child2->setParent('parent2'); $childType->addDocument($child2); $child3 = new Document('child3', array('id' => 'child3', 'user' => 'child3', 'email' => '*****@*****.**', 'alt' => array(array('name' => 'testname')))); $child3->setParent('parent2'); $childType->addDocument($child3); $index->refresh(); return $index; }
/** * Transforms an object into an elastica object having the required keys * * @param object $object the object to convert * @param array $fields the keys we want to have in the returned array * * @return Document **/ public function transform($object, array $fields) { $identifier = $this->propertyAccessor->getValue($object, $this->options['identifier']); $document = new Document($identifier); foreach ($fields as $key => $mapping) { if ($key == '_parent') { $property = null !== $mapping['property'] ? $mapping['property'] : $mapping['type']; $value = $this->propertyAccessor->getValue($object, $property); $document->setParent($this->propertyAccessor->getValue($value, $mapping['identifier'])); continue; } $value = $this->propertyAccessor->getValue($object, $key); if (isset($mapping['type']) && in_array($mapping['type'], array('nested', 'object')) && isset($mapping['properties']) && !empty($mapping['properties'])) { /* $value is a nested document or object. Transform $value into * an array of documents, respective the mapped properties. */ $document->set($key, $this->transformNested($value, $mapping['properties'])); continue; } if (isset($mapping['type']) && $mapping['type'] == 'attachment') { // $value is an attachment. Add it to the document. if ($value instanceof \SplFileInfo) { $document->addFile($key, $value->getPathName()); } else { $document->addFileContent($key, $value); } continue; } $document->set($key, $this->normalizeValue($value)); } return $document; }
/** * Transforms an object into an elastica object * * @param \Message $message the object to convert * @param array $fields the keys we want to have in the returned array * * @return Document **/ public function transform($message, array $fields = array()) { $data = array('content' => $message->getContent()); $document = new Document($message->getId(), $data); $document->setParent($message->getGroup()->getId()); return $document; }
private function prepareSearchData() { $client = $this->_getClient(); $index = $client->getIndex('has_child_test'); $index->create(array(), true); $parentType = $index->getType('parent'); $childType = $index->getType('child'); $childMapping = new \Elastica\Type\Mapping($childType); $childMapping->setParent('parent'); $childMapping->send(); $altType = $index->getType('alt'); $altDoc = new Document('alt1', array('name' => 'altname')); $altType->addDocument($altDoc); $parent1 = new Document('parent1', array('id' => 'parent1', 'user' => 'parent1', 'email' => '*****@*****.**')); $parentType->addDocument($parent1); $parent2 = new Document('parent2', array('id' => 'parent2', 'user' => 'parent2', 'email' => '*****@*****.**')); $parentType->addDocument($parent2); $child1 = new Document('child1', array('id' => 'child1', 'user' => 'child1', 'email' => '*****@*****.**')); $child1->setParent('parent1'); $childType->addDocument($child1); $child2 = new Document('child2', array('id' => 'child2', 'user' => 'child2', 'email' => '*****@*****.**')); $child2->setParent('parent2'); $childType->addDocument($child2); $child3 = new Document('child3', array('id' => 'child3', 'user' => 'child3', 'email' => '*****@*****.**', 'alt' => array(array('name' => 'testname')))); $child3->setParent('parent2'); $childType->addDocument($child3); $index->refresh(); return $index; }
/** * Transforms the given object to an elastica document * * @param object $object the object to convert * @param array $fields the keys we want to have in the returned array * @param string $identifier the identifier for the new document * @return Document */ protected function transformObjectToDocument($object, array $fields, $identifier = '') { $document = new Document($identifier); foreach ($fields as $key => $mapping) { if ($key == '_parent') { $property = null !== $mapping['property'] ? $mapping['property'] : $mapping['type']; $value = $this->propertyAccessor->getValue($object, $property); $document->setParent($this->propertyAccessor->getValue($value, $mapping['identifier'])); continue; } $path = isset($mapping['property_path']) ? $mapping['property_path'] : $key; if (false === $path) { continue; } $value = $this->propertyAccessor->getValue($object, $path); if (isset($mapping['type']) && in_array($mapping['type'], array('nested', 'object')) && isset($mapping['properties']) && !empty($mapping['properties'])) { /* $value is a nested document or object. Transform $value into * an array of documents, respective the mapped properties. */ $document->set($key, $this->transformNested($value, $mapping['properties'])); continue; } if (isset($mapping['type']) && $mapping['type'] == 'attachment') { // $value is an attachment. Add it to the document. if ($value instanceof \SplFileInfo) { $document->addFile($key, $value->getPathName()); } else { $document->addFileContent($key, $value); } continue; } $document->set($key, $this->normalizeValue($value)); } if ($this->dispatcher) { $event = new TransformEvent($document, $fields, $object); $this->dispatcher->dispatch(TransformEvent::POST_TRANSFORM, $event); $document = $event->getDocument(); } return $document; }