24
Elasticsearch & Symfony Integration

Elasticsearch and Symfony Integration - Debarko De

Embed Size (px)

Citation preview

Elasticsearch & Symfony Integration

Elasticsearch

• Powerful Search & Analytics

• Object based documents

• Every field is indexed

• Distributed & Auto Scaling

• JSON, Nested Data & All Searchable

SQL vs ES

• SQL is Relational, ES is a search engine

• SQL does binary matches, ES does binary + relevance

• SQL is easier for vertical scaling, ES is better in horizontal scaling

database

table1

table2

row1

row3

row3

row2

row2

row1

index

type1

type2

document1

document3

document2

document3

document2

document1

SQL World ES World

Scaaa…aaaling

• Easy Horizontal Scaling

• Nodes

• Shards

• Replicas

• Request Handling by any node

ES takes REST

:verb /:index/:type/:document

{ “key”: “value”, “complex”: [1, 2] }

Symfony Integration

Installation

{ "require": { "elasticsearch/elasticsearch": "~1.0" }}

Including in Project

require 'vendor/autoload.php';

$client = new Elasticsearch\Client();

Connecting to remote server

$params = array();$params['hosts'] = array ( '192.168.1.1:9200', '192.168.1.2', 'mydomain.server.com:9201', 'mydomain2.server.com', 'https://localhost', 'https://192.168.1.3:9200', 'http://user:pass@localhost:9200', 'https://user:pass@localhost:9200', // SSL + HTTP Basic Auth);

$client = new Elasticsearch\Client($params);

Logging

$params['logging'] = true;$params['logPath'] = '/var/logs/elasticsearch/elasticsearch.log';

http://www.elastic.co/guide/en/elasticsearch/client/php-api/current/_configuration.html

The Query

{ "query" : { "match" : { "content" : "quick brown fox" } }, "highlight" : { "fields" : { "content" : {} } } }

The Query (in PHP)

$params['body'] = array( 'query' => array( 'match' => array( 'content' => 'quick brown fox' ) ), 'highlight' => array( 'fields' => array( 'content' => new \stdClass() ) ) ); $results = $client->search($params);

Index Creation$client = new Elasticsearch\Client(); $indexParams['index'] = 'my_index';

// Index Settings $indexParams['body']['settings']['number_of_shards'] = 3; $indexParams['body']['settings']['number_of_replicas'] = 2;

// Example Index Mapping $myTypeMapping = array( '_source' => array( 'enabled' => true ), 'properties' => array( 'first_name' => array( 'type' => 'string', 'analyzer' => 'standard' ), 'age' => array( 'type' => 'integer' ) ) ); $indexParams['body']['mappings']['my_type'] = $myTypeMapping;

// Create the index $client->indices()->create($indexParams);

Indexing

$params = array(); $params['body'] = array('testField' => 'abc');

$params['index'] = 'my_index'; $params['type'] = 'my_type'; $params['id'] = 'my_id';

// Document will be indexed to my_index/my_type/my_id $ret = $client->index($params);

Upsert & Bulkfor($i = 0; $i < 100; $i++) { $params['body'][] = array( 'update' => array( '_id' => $i ) );

$params['body'][] = array( 'doc_as_upsert' => 'true', 'doc' => array( 'my_field' => 'my_value', 'second_field' => 'some more values' ) ); }

$responses = $client->bulk($params);

Search

curl -XGET 'localhost:9200/my_index/my_type/_search' -d '{ "query" : { "match" : { "testField" : "abc" } } }'

Search (in PHP)

$params['index'] = 'my_index'; $params['type'] = 'my_type'; $params['body']['query']['match']['testField'] = 'abc';

$results = $client->search($params);

Delete

$deleteParams = array(); $deleteParams['index'] = 'my_index'; $deleteParams['type'] = 'my_type'; $deleteParams['id'] = ‘my_id';

$retDelete = $client->delete($deleteParams);

• http://www.elastic.co/guide/en/elasticsearch/client/php-api/current/

Debarko De

+91 9986465883

[email protected]

@debarko