/** * 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; }
/** * @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'); }
public function testGeoPoint() { $client = $this->_getClient(); $index = $client->getIndex('test'); $index->create(array(), true); $type = $index->getType('test'); // Set mapping $type->setMapping(array('point' => array('type' => 'geo_point'))); // Add doc 1 $doc1 = new Document(1, array('name' => 'ruflin')); $doc1->addGeoPoint('point', 17, 19); $type->addDocument($doc1); // Add doc 2 $doc2 = new Document(2, array('name' => 'ruflin')); $doc2->addGeoPoint('point', 30, 40); $type->addDocument($doc2); $index->optimize(); $index->refresh(); // Only one point should be in radius $query = new Query(); $geoFilter = new GeoDistance('point', array('lat' => 30, 'lon' => 40), '1km'); $query = new Query(new MatchAll()); $query->setFilter($geoFilter); $this->assertEquals(1, $type->search($query)->count()); // Both points should be inside $query = new Query(); $geoFilter = new GeoDistance('point', array('lat' => 30, 'lon' => 40), '40000km'); $query = new Query(new MatchAll()); $query->setFilter($geoFilter); $index->refresh(); $this->assertEquals(2, $type->search($query)->count()); }
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; }
/** * @group functional */ public function testGeoPoint() { $index = $this->_createIndex(); $type = $index->getType('test'); // Set mapping $type->setMapping(array('location' => array('type' => 'geo_point'))); // Add doc 1 $doc1 = new Document(1, array('name' => 'ruflin')); $doc1->addGeoPoint('location', 17, 19); $type->addDocument($doc1); // Add doc 2 $doc2 = new Document(2, array('name' => 'ruflin')); $doc2->addGeoPoint('location', 30, 40); $type->addDocument($doc2); $index->refresh(); // Only one point should be in polygon $query = new Query(); $points = array(array(16, 16), array(16, 20), array(20, 20), array(20, 16), array(16, 16)); $geoFilter = new GeoPolygon('location', $points); $query = new Query(new MatchAll()); $query->setPostFilter($geoFilter); $this->assertEquals(1, $type->search($query)->count()); // Both points should be inside $query = new Query(); $points = array(array(16, 16), array(16, 40), array(40, 40), array(40, 16), array(16, 16)); $geoFilter = new GeoPolygon('location', $points); $query = new Query(new MatchAll()); $query->setPostFilter($geoFilter); $this->assertEquals(2, $type->search($query)->count()); }
/** * @param Document|array $documentOrSource Document object or source array * @return Message * * @throws \Exception * @throws GdbotsPbjException */ public function unmarshal($documentOrSource) { if ($documentOrSource instanceof Document) { return $this->doUnmarshal($documentOrSource->getData()); } return $this->doUnmarshal($documentOrSource); }
/** * Populate elastica with the location of the photograph * @param \Elastica\Document $document Representation of an Elastic Search document * @return \Elastica\Document modified version of the document */ public function updateElasticsearchDocument(\Elastica\Document $document) { // self::$ctr++; $coors = array('lat' => $this->owner->Lat, 'lon' => $this->owner->Lon); $document->set('location', $coors); $sortable = $this->owner->ShutterSpeed; $sortable = explode('/', $sortable); if (sizeof($sortable) == 1) { $sortable = trim($sortable[0]); if ($this->owner->ShutterSpeed == null) { $sortable = null; } if ($sortable === '1') { $sortable = '1.000000'; } } else { if (sizeof($sortable) == 2) { $sortable = floatval($sortable[0]) / intval($sortable[1]); $sortable = round($sortable, 6); } } $sortable = $sortable . '|' . $this->owner->ShutterSpeed; $document->set('ShutterSpeed', $sortable); return $document; }
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; }
/** * {@inheritDoc} */ public function toDocument(Model $model, Document $document) { $mapping = ['city' => 'city', 'state' => 'state', 'country' => 'country', 'zip_code' => 'zip_code']; foreach ($mapping as $field => $key) { $document->set($key, $model->{$field}); } if (!empty($model->lat) && !empty($model->lon)) { $document->set('location', ['lon' => $model->lon, 'lat' => $model->lat]); } $fullAddress = []; if (!empty($model->city)) { $fullAddress[] = $model->city; } if (!empty($model->state)) { $fullAddress[] = $model->state; } if (!empty($model->zip_code)) { $fullAddress[] = $model->zip_code; } if (!empty($model->country)) { $fullAddress[] = $model->country; } $document->set('full_address', implode(', ', $fullAddress)); return $document; }
/** * Convert a log message into an Elastica Document * * @param array $record Log message * @return Document */ protected function getDocument($record) { $document = new Document(); $document->setData($record); $document->setType($this->type); $document->setIndex($this->index); return $document; }
public function getDocument($record) { $user = $this->token->getToken()->getUser(); $record['extra']['user'] = $user->getId(); $document = new Document(); $document->setData($record); $document->setType($this->type); $document->setIndex($this->index); return $document; }
/** * DocumentFactory constructor. * * @param Document $prototype */ public function __construct(Document $prototype) { if ($prototype->getIndex() === '' || $prototype->getType() === '') { throw new \InvalidArgumentException('prototype not properly inited'); } $this->docPrototype = $prototype; $fieldValuePairs = get_object_vars(new User()); array_walk($fieldValuePairs, function ($value, $field) { $this->fieldList[$field] = gettype($value); }); }
/** * ElasticaHelper constructor. * * @param string $gameVersion * @param string $indexName * @param int $magicNumber */ public function __construct($gameVersion, $indexName, $magicNumber = 500) { $docPrototype = new Document(); $docPrototype->setIndex($indexName)->setType('user:'******'/elastica.error'); }; }
/** * @param Document $document * @param Content $content */ public function indexExtraFields(Document $document, Content $content) { if (!$content instanceof EntityContent || $content->isRedirect() === true) { return; } $fields = $this->fieldDefinitions->getFields(); $entity = $content->getEntity(); foreach ($fields as $fieldName => $field) { $data = $field->getFieldData($entity); $document->set($fieldName, $data); } }
/** * 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); } }
/** * 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; }
/** * {@inheritdoc} * @see \Silex\ServiceProviderInterface::register() */ public function register(Application $app) { $app['elastic.client'] = $app->share(function () use($app) { $config = $app['config']('elastic.connection', array()); $client = new Client($config); return $client; }); $app['elastic.bulk'] = $app->protect(function ($index) use($app) { $bulk = new Bulk($app['elastic.client']); $bulk->setIndex($index); return $bulk; }); $app['elastic.document'] = $app->protect(function ($type, array $data) { $document = new Document(); $document->setType($type); $document->setData($data); return $document; }); }
/** * 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)); } }
/** * @dataProvider configProvider */ public function testSearchRequest($config) { // Creates a new index 'xodoa' and a type 'user' inside this index $client = new Client($config); $index = $client->getIndex('elastica_test1'); $index->create(array(), true); $type = $index->getType('user'); // Adds 1 document to the index $doc1 = new Document(1, array('username' => 'hans', 'test' => array('2', '3', '5'))); $doc1->setVersion(0); $type->addDocument($doc1); // Adds a list of documents with _bulk upload to the index $docs = array(); $docs[] = new Document(2, array('username' => 'john', 'test' => array('1', '3', '6'))); $docs[] = new Document(3, array('username' => 'rolf', 'test' => array('2', '3', '7'))); $type->addDocuments($docs); // Refresh index $index->refresh(); $resultSet = $type->search('rolf'); $this->assertEquals(1, $resultSet->getTotalHits()); }
/** * {@inheritDoc} */ public function toDocument(Model $model, Document $document) { foreach ($this->mapping as $field => $key) { $document->set($key, $model->{$field}); } if (!empty($model->job_ziplat) && !empty($model->job_ziplon)) { $document->set('location', $model->job_ziplat . ',' . $model->job_ziplon); } $fullAddress = []; if (!empty($model->job_address)) { $fullAddress[] = $model->job_address; } if (!empty($model->city)) { $fullAddress[] = $model->city; } if (!empty($model->job_postcode)) { $fullAddress[] = $model->job_postcode; } $document->set('full_address', implode(', ', $fullAddress)); return $document; }
/** * @param \Elastica\Document $document * @param string $opType * @return \Elastica\Bulk\Action\AbstractDocument */ public static function create(Document $document, $opType = null) { if (null === $opType && $document->hasOpType()) { $opType = $document->getOpType(); } switch ($opType) { case self::OP_TYPE_DELETE: $action = new DeleteDocument($document); break; case self::OP_TYPE_CREATE: $action = new CreateDocument($document); break; case self::OP_TYPE_UPDATE: $action = new UpdateDocument($document); break; case self::OP_TYPE_INDEX: default: $action = new IndexDocument($document); break; } return $action; }
public function testMatchDoc() { $client = new Client(array('persistent' => false)); $index = $client->getIndex('elastica_test'); $index->create(array('index' => array('number_of_shards' => 1, 'number_of_replicas' => 0)), true); $percolator = new Percolator($index); $percolatorName = 'percotest'; $query = new Term(array('name' => 'ruflin')); $response = $percolator->registerQuery($percolatorName, $query); $this->assertTrue($response->isOk()); $this->assertFalse($response->hasError()); $doc1 = new Document(); $doc1->set('name', 'ruflin'); $doc2 = new Document(); $doc2->set('name', 'nicolas'); $index = new Index($index->getClient(), '_percolator'); $index->optimize(); $index->refresh(); $matches1 = $percolator->matchDoc($doc1); $this->assertTrue(in_array($percolatorName, $matches1)); $this->assertEquals(1, count($matches1)); $matches2 = $percolator->matchDoc($doc2); $this->assertEmpty($matches2); }
/** * More like this query based on the given object * * The id in the given object has to be set * * @param \Elastica\Document $doc Document to query for similar objects * @param array $params OPTIONAL Additional arguments for the query * @param string|array|\Elastica\Query $query OPTIONAL Query to filter the moreLikeThis results * @return \Elastica\ResultSet ResultSet with all results inside * @link http://www.elasticsearch.org/guide/reference/api/more-like-this.html */ public function moreLikeThis(Document $doc, $params = array(), $query = array()) { $path = $doc->getId() . '/_mlt'; $query = Query::create($query); $response = $this->request($path, Request::GET, $query->toArray(), $params); return new ResultSet($response, $query); }
/** * @param \Elastica\Response $response * @param \Elastica\Document $document * @param string $fields Array of field names to be populated or '_source' if whole document data should be updated */ protected function _populateDocumentFieldsFromResponse(Response $response, Document $document, $fields) { $responseData = $response->getData(); if ('_source' == $fields) { if (isset($responseData['get']['_source']) && is_array($responseData['get']['_source'])) { $document->setData($responseData['get']['_source']); } } else { $keys = explode(',', $fields); $data = $document->getData(); foreach ($keys as $key) { if (isset($responseData['get']['fields'][$key])) { $data[$key] = $responseData['get']['fields'][$key]; } elseif (isset($data[$key])) { unset($data[$key]); } } $document->setData($data); } }
/** * Update document, using update script. Requires elasticsearch >= 0.19.0 * * @param int $id document id * @param array|\Elastica\Script|\Elastica\Document $data raw data for request body * @param string $index index to update * @param string $type type of index to update * @param array $options array of query params to use for query. For possible options check es api * @return \Elastica\Response * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html */ public function updateDocument($id, $data, $index, $type, array $options = array()) { $path = $index . '/' . $type . '/' . $id . '/_update'; if ($data instanceof Script) { $requestData = $data->toArray(); } elseif ($data instanceof Document) { $requestData = array('doc' => $data->getData()); if ($data->getDocAsUpsert()) { $requestData['doc_as_upsert'] = true; } $docOptions = $data->getOptions(array('version', 'version_type', 'routing', 'percolate', 'parent', 'fields', 'retry_on_conflict', 'consistency', 'replication', 'refresh', 'timeout')); $options += $docOptions; // set fields param to source only if options was not set before if ($data instanceof Document && ($data->isAutoPopulate() || $this->getConfigValue(array('document', 'autoPopulate'), false)) && !isset($options['fields'])) { $options['fields'] = '_source'; } } else { $requestData = $data; } //If an upsert document exists if ($data instanceof Script || $data instanceof Document) { if ($data->hasUpsert()) { $requestData['upsert'] = $data->getUpsert()->getData(); } } if (!isset($options['retry_on_conflict'])) { $retryOnConflict = $this->getConfig("retryOnConflict"); $options['retry_on_conflict'] = $retryOnConflict; } $response = $this->request($path, Request::POST, $requestData, $options); if ($response->isOk() && $data instanceof Document && ($data->isAutoPopulate() || $this->getConfigValue(array('document', 'autoPopulate'), false))) { $responseData = $response->getData(); if (isset($responseData['_version'])) { $data->setVersion($responseData['_version']); } if (isset($options['fields'])) { $this->_populateDocumentFieldsFromResponse($response, $data, $options['fields']); } } return $response; }
public function testAddDocumentVersion() { $client = $this->_getClient(); $index = $client->getIndex('test'); $index->create(array(), true); $type = new Type($index, 'test'); $doc1 = new Document(1); $doc1->set('title', 'Hello world'); $return = $type->addDocument($doc1); $data = $return->getData(); $this->assertEquals(1, $data['_version']); $return = $type->addDocument($doc1); $data = $return->getData(); $this->assertEquals(2, $data['_version']); }
/** * {@inheritdoc} */ public function logStringQuery($searchTerm, $ipAddress) { $document = new Document(); $document->setData(array('search_term' => $searchTerm, 'ip_address' => $ipAddress)); $this->type->addDocuments(array($document)); }
/** * Persists an entity based on the fields that are marked as dirty and * returns the same entity after a successful save or false in case * of any error. * * Triggers the `Model.beforeSave` and `Model.afterSave` events. * * ## Options * * - `checkRules` Defaults to true. Check deletion rules before deleting the record. * * @param \Cake\Datasource\EntityInterface $entity The entity to be saved * @param array $options An array of options to be used for the event * @return \Cake\Datasource\EntityInterface|bool */ public function save(EntityInterface $entity, $options = []) { $options += ['checkRules' => true]; $options = new ArrayObject($options); $event = $this->dispatchEvent('Model.beforeSave', ['entity' => $entity, 'options' => $options]); if ($event->isStopped()) { return $event->result; } if ($entity->errors()) { return false; } $mode = $entity->isNew() ? RulesChecker::CREATE : RulesChecker::UPDATE; if ($options['checkRules'] && !$this->checkRules($entity, $mode, $options)) { return false; } $type = $this->connection()->getIndex()->getType($this->name()); $id = $entity->id ?: null; $data = $entity->toArray(); unset($data[$id]); $doc = new ElasticaDocument($id, $data); $doc->setAutoPopulate(true); $result = $type->addDocument($doc); $entity->id = $doc->getId(); $entity->_version = $doc->getVersion(); $entity->isNew(false); $entity->source($this->name()); $entity->clean(); $this->dispatchEvent('Model.afterSave', ['entity' => $entity, 'options' => $options]); return $entity; }
/** * @param \Elastica\Document|array $data * * @return $this */ public function setUpsert($data) { $document = Document::create($data); $this->_upsert = $document; return $this; }
/** * @param User $user * * @return Document */ protected function makeDocument(User $user) { $document = new Document($user->snsid, (new Factory())->toArray($user)); $document->setDocAsUpsert(true)->setIndex($this->getIndexName())->setType($this->getTypeName()); return $document; }