PHP code example of nervetattoo / elasticsearch

1. Go to this page and download the library: Download nervetattoo/elasticsearch library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nervetattoo / elasticsearch example snippets




use \ElasticSearch\Client;
// The recommended way to go about things is to use an environment variable called ELASTICSEARCH_URL
$es = Client::connection();

// Alternatively you can use dsn string
$es = Client::connection('http://127.0.0.1:9200/myindex/mytype');

$es->index(array('title' => 'My cool document'), $id);
$es->get($id);
$es->search('title:cool');

$es->map(array(
    'title' => array(
        'type' => 'string',
	'index' => 'analyzed'
    )
));

$results = $es
    ->setIndex(array("one", "two"))
    ->setType(array("mytype", "other-type"))
    ->search('title:cool');

$es->search(array(
    'query' => array(
        'term' => array('title' => 'cool')
    )
);

$es = Client::connection(array(
    'servers' => '127.0.0.1:9200',
    'protocol' => 'http',
    'index' => 'myindex',
    'type' => 'mytype'
));

$document = array(
    'title' => 'My routed document',
    'user_id' => '42'
);
$es->index($document, $id, array('routing' => $document['user_id']));
$es->search('title:routed', array('routing' => '42'));

$document = array(
    'title' => 'My bulked entry',
    'user_id' => '43'
);
$es->beginBulk();
$es->index($document, $id, array('routing' => $document['user_id']));
$es->delete(2);
$es->delete(3);
$es->commitBulk();


$es->createBulk()
    ->delete(4)
    ->index($document, $id, 'myIndex', 'myType', array('parent' => $parentId));
    ->delete(5)
    ->delete(6)
    ->commit();


// ...

$loader->registerNamespaces(array(
    // ...
    'ElasticSearch' => __DIR__.'/path/to/your/vendor/nervetattoo/elasticsearch/src',
));

class FooController extends Controller
{
    // ...

    public function barAction()
    {
        // ...
        $es = $this->get('your_bundle.elastic_client');
        $results = $es
            ->setIndex(array("one", "two"))
            ->setType(array("mytype", "other-type"))
            ->search('title:cool');
    }
}