本文整理汇总了PHP中Elasticsearch\Client::index方法的典型用法代码示例。如果您正苦于以下问题:PHP Client::index方法的具体用法?PHP Client::index怎么用?PHP Client::index使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Elasticsearch\Client
的用法示例。
在下文中一共展示了Client::index方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: addObject
/**
* add an object to the elasticsearch index
*
* @param $object
* @param $objectId
* @return string
*/
public function addObject($object, $objectId)
{
$objectId = sha1($objectId);
$params = array_replace($this->defaultParams, array('id' => $objectId, 'body' => $object));
$this->client->index($params);
return $objectId;
}
示例2: create
/**
* @param JobInterface $job
* @return string jobId
*/
public function create(JobInterface $job)
{
$job->validate();
$jobData = ['index' => $this->index->getIndexNameCurrent(), 'type' => 'jobs', 'id' => $job->getId(), 'body' => $this->fillEmptyKeys($job->getData())];
$response = null;
$i = 0;
while ($i < 5) {
try {
$response = $this->client->index($jobData);
break;
} catch (ServerErrorResponseException $e) {
// ES server error, try again
$this->log('error', 'Elastic server error response', ['attemptNo' => $i, 'jobId' => $job->getId(), 'exception' => $e]);
}
sleep(1 + intval(pow(2, $i) / 2));
$i++;
}
if (!isset($response['created'])) {
throw new ApplicationException("Unable to index job", null, ['job' => $jobData, 'elasticResponse' => $response]);
}
//@todo: remove sleep in next (major) release
sleep(1);
$i = 0;
while ($i < 5) {
$resJob = $this->get($job->getId());
if ($resJob != null) {
return $response['_id'];
}
sleep(1 + intval(pow(2, $i) / 2));
$i++;
}
throw new ApplicationException("Unable to find job in index after creation", null, ['job' => $job->getData(), 'elasticResponse' => $response]);
}
示例3: indexAll
/**
* Index all products
*
* @return number
*/
public function indexAll()
{
$products = $this->database->table('product')->fetchAll();
foreach ($products as $product) {
$this->es->index(['index' => $this->indexName, 'type' => 'product', 'id' => $product['id'], 'body' => $product->toArray()]);
}
return count($products);
}
示例4: testIndexDocument2
public function testIndexDocument2()
{
/** @var \Iwai\Elasticsearch\FutureData $future */
$future = $this->client->index(['index' => 'my_index', 'type' => 'my_type', 'id' => 'my_id2', 'body' => ['testField' => 'abc']]);
$future->then(function ($response) {
$this->assertEquals(1, $response['created']);
});
$response = $future->wait();
}
示例5: set
/**
* {@inheritdoc}
*/
public function set($user, $imageIdentifier, array $imageData)
{
$params = $this->prepareParams($imageIdentifier, $imageData);
try {
return (bool) $this->client->index($params);
} catch (Exception $e) {
trigger_error('Elasticsearch metadata indexing failed for image: ' . $imageIdentifier, E_USER_WARNING);
return false;
}
}
示例6: addDocumentToIndex
/**
* @param Model $model
*
* @throws DocumentMissingException
*/
public function addDocumentToIndex(Model $model)
{
if (!$model->exists) {
throw new DocumentMissingException('Document does not exist.');
}
$params = $this->getConfigurator()->getParams($model);
// Get our document body data.
$params['body'] = $this->getConfigurator()->getDocumentData($model);
$this->client->index($params);
}
示例7: persist
/**
* @param SavableModelInterface $model
* @throws CouldNotPersistException
*/
public function persist(SavableModelInterface $model)
{
$params = ['index' => $this->index, 'type' => $this->type];
if ($model->getId()) {
$params['body'] = ['doc' => $model->toArray()];
$params['id'] = $model->getId();
$this->client->update($params);
$model->markAsStored();
return;
}
$params['body'] = $model->toArray();
$response = $this->client->index($params);
$model->id = $response['_id'];
$model->markAsStored();
}
示例8: saveDocument
/**
* {@inheritdoc}
*/
public function saveDocument(Searchable $model)
{
$document = $this->constructDocument($model);
if (!$this->indexIsNested($model->getSearchIndex())) {
$this->client->index($document);
return;
}
list($index, $type) = $this->retrieveNestedIndex($model->getSearchIndex());
$class = $this->retrieveParentClass($model->getSearchIndex());
$parent = $model->belongsTo($class, null, null, class_basename($class))->getResults();
$parentData = $this->client->get(['id' => $parent->getKey(), 'type' => $parent->getSearchType(), 'index' => $parent->getSearchIndex()])['_source'];
if (!isset($parentData[$type])) {
$parentData[$type] = [];
}
$children = Collection::make($parentData[$type]);
if ($child = $children->first(function ($child) use($model) {
return $child[$model->getKeyName()] == $model->getKey();
})) {
$newChildren = $children->map(function ($child) use($model) {
if ($child[$model->getKeyName()] == $model->getKey()) {
$child = $model->documentToArray();
if (!isset($document[$model->getKeyName()])) {
$child[$model->getKeyName()] = $model->getKey();
}
}
return $child;
});
} else {
$newChildren = $children->push($model->documentToArray());
}
$this->client->update(['id' => $parent->getKey(), 'type' => $parent->getSearchType(), 'index' => $parent->getSearchIndex(), 'body' => ['doc' => [$type => $newChildren]]]);
}
示例9: asyncLarge
/**
* @iterations 1000
* @group large
*/
public function asyncLarge()
{
$asyncDoc = $this->largeDocument;
$asyncDoc['client']['future'] = 'lazy';
$response = $this->client->index($asyncDoc);
$response = $response['body']['created'];
}
示例10: addDocuments
public static function addDocuments(\ElasticSearch\Client $client, $num = 3, $tag = 'cool')
{
$options = array('refresh' => true);
while ($num-- > 0) {
$doc = array('title' => "One cool document {$tag}", 'rank' => rand(1, 10));
$client->index($doc, $num + 1, $options);
}
return $client;
}
示例11: executeByElasticClient
/**
* Execute the request by elasticsearch client
*
* @param Client $client
* @return ResponseInterface
*/
public function executeByElasticClient(Client $client)
{
$responseClass = $this->getResponseClassOfRequest();
/** @var IndexResponseInterface $response */
$response = new $responseClass();
$rawResult = RawResponse::build($client->index($this->toElasticClient()));
$response = $response->build($rawResult);
$this->getDocument()->setId($response->id());
return $response;
}
示例12: asyncLarge
/**
* @iterations 10
* @group large
*/
public function asyncLarge()
{
$responses = [];
$asyncDoc = $this->largeDocument;
$asyncDoc['client']['future'] = 'lazy';
for ($i = 0; $i < 1000; $i++) {
$responses[] = $this->client->index($asyncDoc);
}
$responses[999]->wait();
}
示例13: handleMessage
public function handleMessage(Change $change, Message $message, Channel $channel)
{
if (!$change->getProduct()) {
throw new \InvalidArgumentException("Badly routed message '{$message->content}' with routing key '{$message->routingKey}'.");
}
$product = $change->getProduct();
/** @var Eshop $eshop */
$eshop = $this->eshopRepository->getOneById($product->getEshopId());
$product->setEshop($eshop);
$categoryIds = $product->getCategoryIds();
if (!empty($categoryIds)) {
$product->setCategories($this->categoryRepository->find(["_id" => ['$in' => $product->getCategoryIds()]]));
}
if (!$this->elasticsearch->indices()->existsAlias(["name" => $this->catalogIndexAliasName])) {
$this->initIndex();
}
$response = $this->elasticsearch->index(["index" => $this->catalogIndexAliasName, "type" => ProductMeta::SHORT_NAME, "id" => (string) $product->getId(), "version" => $product->getV(), "version_type" => "external_gte", "body" => ProductMeta::toObject($product, "json:")]);
$this->log->info("Indexed Product#{$product->getId()} (v={$product->getV()}, created=" . json_encode($response["created"]) . ").");
$channel->ack($message);
}
示例14: doIndex
/**
* Index a single document
*
* @param Posts $post
*/
protected function doIndex(Posts $post)
{
$params = [];
$karma = $post->number_views + ($post->votes_up - $post->votes_down) * 10 + $post->number_replies;
if ($karma > 0) {
$params['body'] = ['id' => $post->id, 'title' => $post->title, 'category' => $post->categories_id, 'content' => $post->content, 'karma' => $karma];
$params['index'] = $this->config->get('index', 'phosphorum');
$params['type'] = 'post';
$params['id'] = 'post-' . $post->id;
$this->client->index($params);
}
}
示例15: handle
public function handle()
{
$params = array();
$params['hosts'] = array('http://10.0.2.2:9200');
$es = new Client($params);
$models = Winwin::all();
foreach ($models as $model) {
$es->index(['index' => 'winwins', 'type' => 'winwins', 'id' => $model->id, 'body' => $model->toArray()]);
}
Log::info('Bancame indexed');
$models = Group::all();
foreach ($models as $model) {
$es->index(['index' => 'winwins', 'type' => 'groups', 'id' => $model->id, 'body' => $model->toArray()]);
}
Log::info('Groups indexed');
$models = UserDetail::all();
foreach ($models as $model) {
$es->index(['index' => 'winwins', 'type' => 'users', 'id' => $model->id, 'body' => $model->toArray()]);
}
Log::info('Users indexed');
}