|
| 1 | +# Using with [ElasticSearch/ElasticSearch](https://github.com/elastic/elasticsearch-php) |
| 2 | +First we need to prepare query for what we want to search. |
| 3 | +```php |
| 4 | +$query = new \Spameri\ElasticQuery\ElasticQuery(); |
| 5 | +$query->query()->must()->add( |
| 6 | + new \Spameri\ElasticQuery\Query\Match( |
| 7 | + 'name', |
| 8 | + 'Avengers' |
| 9 | + ) |
| 10 | +); |
| 11 | +``` |
| 12 | + |
| 13 | +`\Spameri\ElasticQuery\ElasticQuery` is base for every query you want to perform. |
| 14 | +- It has must, should sections. |
| 15 | +- Every must, should section is collection of `\Spameri\ElasticQuery\Query\LeafQueryInterface` |
| 16 | +item and should, must collection are also implementations of **LeafQueryInterface** so it allows you to nest them as you need. |
| 17 | + |
| 18 | +Next we need to set up document to send to elasticsearch library. |
| 19 | +```php |
| 20 | +$document = new \Spameri\ElasticQuery\Document( |
| 21 | + 'spameri_video', |
| 22 | + new \Spameri\ElasticQuery\Document\Body\Plain($elasticQuery->toArray()) |
| 23 | +); |
| 24 | +``` |
| 25 | +- Document is very straightforward, you need to specify index in first argument and document body in second argument |
| 26 | +third argument is type if needed by you ElasticSearch version, also fourth is ID. |
| 27 | + |
| 28 | +Last you need to send document to ElasticSearch client. |
| 29 | +```php |
| 30 | +$response = \Elasticsearch\Client::search( |
| 31 | + $document->toArray() |
| 32 | +); |
| 33 | +``` |
| 34 | + |
| 35 | +Elasticsearch library will return array as response. You can map this response to object like this: |
| 36 | +```php |
| 37 | +$resultMapper = new \Spameri\ElasticQuery\Response\ResultMapper(); |
| 38 | +$resultObject = $resultMapper->map($response); |
| 39 | +``` |
| 40 | +Object is implementation of `\Spameri\ElasticQuery\Response\ResultInterface` depending on your type of query. |
| 41 | + |
| 42 | +Or you can map result to specific object by calling direct mapping methods. |
| 43 | +- For single result you get by searching by ID. (as parameter in document object) |
| 44 | +```php |
| 45 | +\Spameri\ElasticQuery\Response\ResultMapper::mapSingleResult($response); |
| 46 | +``` |
| 47 | +- For search response with multiple hits. |
| 48 | +```php |
| 49 | +\Spameri\ElasticQuery\Response\ResultMapper::mapSearchResults($response); |
| 50 | +``` |
| 51 | +- For bulk result, where you have information about bulk actions. |
| 52 | +```php |
| 53 | +\Spameri\ElasticQuery\Response\ResultMapper::mapBulkResult($response); |
| 54 | +``` |
| 55 | + |
| 56 | +# Using with Spameri/Elastic |
| 57 | +This is implementation for Nette framework, so you need to register it to your application accordingly. |
| 58 | +[How to here](https://github.com/Spameri/Elastic/blob/master/doc/01_intro.md#1-config-elasticsearch) |
| 59 | + |
| 60 | +Then you inject client provider where you need and do query like this. |
| 61 | +```php |
| 62 | +$response = $this->clientProvider->client()->search( |
| 63 | + $document->toArray() |
| 64 | +); |
| 65 | +``` |
| 66 | +Response mapping is same, so is constructing query. |
| 67 | + |
| 68 | +For more advanced use see Spameri/Elastic [documentation](https://github.com/Spameri/Elastic/blob/master/doc/01_intro.md) |
| 69 | + |
| 70 | +# Using with Guzzle |
| 71 | +Difference is you dont need document object. You have to specify index and type in requested url by yourself. |
| 72 | +```php |
| 73 | +$client = new \GuzzleHttp\Client(); |
| 74 | +$response = $client->request('GET', 'https://localhost:9200/spameri_video/_search', [ |
| 75 | + 'body' => \json_encode($elasticQuery->toArray()) |
| 76 | +]); |
| 77 | +``` |
| 78 | +Rest is same. Setting up query and mapping to result object. |
0 commit comments