本文整理汇总了PHP中Elastica\Document类的典型用法代码示例。如果您正苦于以下问题:PHP Document类的具体用法?PHP Document怎么用?PHP Document使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Document类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: transform
/**
* 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;
}
示例2: testHasParent
/**
* @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');
}
示例3: testGeoPoint
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());
}
示例4: prepareSearchData
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' => 'parent1@test.com'));
$parentType->addDocument($parent1);
$parent2 = new Document('parent2', array('id' => 'parent2', 'user' => 'parent2', 'email' => 'parent2@test.com'));
$parentType->addDocument($parent2);
$child1 = new Document('child1', array('id' => 'child1', 'user' => 'child1', 'email' => 'child1@test.com'));
$child1->setParent('parent1');
$childType->addDocument($child1);
$child2 = new Document('child2', array('id' => 'child2', 'user' => 'child2', 'email' => 'child2@test.com'));
$child2->setParent('parent2');
$childType->addDocument($child2);
$child3 = new Document('child3', array('id' => 'child3', 'user' => 'child3', 'email' => 'child3@test.com', 'alt' => array(array('name' => 'testname'))));
$child3->setParent('parent2');
$childType->addDocument($child3);
$index->refresh();
return $index;
}
示例5: testGeoPoint
/**
* @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());
}
示例6: unmarshal
/**
* @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);
}
示例7: updateElasticsearchDocument
/**
* 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;
}
示例8: _getTestIndex
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' => 'parent1@test.com'));
$parentType->addDocument($parent1);
$parent2 = new Document('parent2', array('id' => 'parent2', 'user' => 'parent2', 'email' => 'parent2@test.com'));
$parentType->addDocument($parent2);
$child1 = new Document('child1', array('id' => 'child1', 'user' => 'child1', 'email' => 'child1@test.com'));
$child1->setParent('parent1');
$childType->addDocument($child1);
$child2 = new Document('child2', array('id' => 'child2', 'user' => 'child2', 'email' => 'child2@test.com'));
$child2->setParent('parent2');
$childType->addDocument($child2);
$child3 = new Document('child3', array('id' => 'child3', 'user' => 'child3', 'email' => 'child3@test.com', 'alt' => array(array('name' => 'testname'))));
$child3->setParent('parent2');
$childType->addDocument($child3);
$index->refresh();
return $index;
}
示例9: toDocument
/**
* {@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;
}
示例10: getDocument
/**
* 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;
}
示例11: getDocument
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;
}
示例12: __construct
/**
* 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);
});
}
示例13: __construct
/**
* 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:' . $gameVersion);
$this->documentFactory = new DocumentFactory($docPrototype);
$this->elastica = $this->makeElastica();
$this->magicNumber = $magicNumber;
$this->errorHandler = function (\Exception $exception) {
error_log(print_r($exception->getMessage(), true), 3, CONFIG_DIR . '/elastica.error');
};
}
示例14: indexExtraFields
/**
* @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);
}
}
示例15: update
/**
* 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);
}
}