Skip to content

Commit 2b97cf8

Browse files
committed
Added documentation
1 parent c961428 commit 2b97cf8

File tree

10 files changed

+214
-129
lines changed

10 files changed

+214
-129
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
/.idea
22
/vendor
3-
/.composer.lock
3+
composer.lock

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,22 @@
22
Elastic documentation converted to php objects for easy using.
33

44
[![Build Status](https://travis-ci.org/Spameri/ElasticQuery.svg?branch=master)](https://travis-ci.org/Spameri/ElasticQuery)
5+
6+
Installation
7+
------------
8+
9+
The best way to install Spameri/ElasticQuery is using [Composer](http://getcomposer.org/):
10+
11+
```sh
12+
$ composer require spameri/elastic-query
13+
```
14+
15+
Documentation
16+
------------
17+
18+
Learn more in the [documentation](https://github.com/Spameri/ElasticQuery/tree/master/doc).
19+
20+
- [Usage](https://github.com/Spameri/ElasticQuery/tree/master/doc/01-usage.md)
21+
- [Query objects](https://github.com/Spameri/ElasticQuery/tree/master/doc/02-query-objects.md)
22+
- [Aggregation objects](https://github.com/Spameri/ElasticQuery/tree/master/doc/03-aggregation-objects.md)
23+
- [Result objects](https://github.com/Spameri/ElasticQuery/tree/master/doc/04-result-objects.md)

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
},
1616
"require-dev": {
1717
"phpstan/phpstan-shim": "v0.10.3",
18-
"nette/tester": "v2.1.0"
18+
"nette/tester": "v2.1.0",
19+
"elasticsearch/elasticsearch": "^6.1",
20+
"guzzlehttp/guzzle": "^6.3"
1921
},
2022
"autoload": {
2123
"psr-4": {

composer.lock

Lines changed: 0 additions & 126 deletions
This file was deleted.

doc/01-usage.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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.

doc/02-query-objects.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Query objects
2+
3+
Every object is as close to documentation as possible. Also reference provided in doc blocks where possible.
4+
[Like this](https://github.com/Spameri/ElasticQuery/blob/master/src/Query/Match.php#L7)
5+
6+
Every query object implements `\Spameri\ElasticQuery\Query\LeafQueryInterface` and is capable of converting to array.
7+
8+
Must and should collection of objects is also **LeafQueryInterface**, so you can nest rules as you need.
9+
10+
## Implementations
11+
##### Match Query
12+
- Class `\Spameri\ElasticQuery\Query\Match`
13+
- [Documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html)
14+
- [Implementation](https://github.com/Spameri/ElasticQuery/blob/master/src/Query/Match.php)
15+
- [Sample usage](https://github.com/Spameri/ElasticQuery/blob/master/tests/SpameriTests/ElasticQuery/Query/Match.phpt#L8)
16+
17+
##### Fuzzy Query
18+
- Class `\Spameri\ElasticQuery\Query\Fuzzy`
19+
- [Documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-fuzzy-query.html)
20+
- [Implementation](https://github.com/Spameri/ElasticQuery/blob/master/src/Query/Fuzzy.php)
21+
22+
##### Range Query
23+
- Class `\Spameri\ElasticQuery\Query\Range`
24+
- [Documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html)
25+
- [Implementation](https://github.com/Spameri/ElasticQuery/blob/master/src/Query/Range.php)
26+
27+
##### Term Query
28+
- Class `\Spameri\ElasticQuery\Query\Term`
29+
- [Documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html)
30+
- [Implementation](https://github.com/Spameri/ElasticQuery/blob/master/src/Query/Term.php)
31+
32+
##### Terms Query
33+
- Class `\Spameri\ElasticQuery\Query\Terms`
34+
- [Documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html)
35+
- [Implementation](https://github.com/Spameri/ElasticQuery/blob/master/src/Query/Terms.php)
36+
37+
##### WildCard Query
38+
- Class `\Spameri\ElasticQuery\Query\WildCard`
39+
- [Documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-wildcard-query.html)
40+
- [Implementation](https://github.com/Spameri/ElasticQuery/blob/master/src/Query/WildCard.php)
41+
42+
##### QueryCollection Query
43+
- Class `\Spameri\ElasticQuery\Query\QueryCollection`
44+
- [Implementation](https://github.com/Spameri/ElasticQuery/blob/master/src/Query/QueryCollection.php)
45+
- This is query for nesting your must, must_not and should sub queries.
46+
47+
##### MustCollection Query
48+
- Class `\Spameri\ElasticQuery\Query\MustCollection`
49+
- [Implementation](https://github.com/Spameri/ElasticQuery/blob/master/src/Query/MustCollection.php)
50+
51+
##### MustNotCollection Query
52+
- Class `\Spameri\ElasticQuery\Query\MustNotCollection`
53+
- [Implementation](https://github.com/Spameri/ElasticQuery/blob/master/src/Query/MustNotCollection.php)
54+
55+
##### ShouldCollection Query
56+
- Class `\Spameri\ElasticQuery\Query\ShouldCollection`
57+
- [Implementation](https://github.com/Spameri/ElasticQuery/blob/master/src/Query/ShouldCollection.php)

doc/03-aggregation-objects.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Aggregation objects
2+
3+
Every object is as close to documentation as possible. Also reference provided in doc blocks where possible.
4+
[Like this](https://github.com/Spameri/ElasticQuery/blob/master/src/Aggregation/Term.php#L7)
5+
6+
Every aggregation object implements `\Spameri\ElasticQuery\Aggregation\LeafAggregationInterface` and is capable of converting to array.
7+
8+
AggregationCollection is also **LeafAggregationInterface**, so you can nest as you need.
9+
10+
## Implementations
11+
##### Term Aggregation
12+
- Class `\Spameri\ElasticQuery\Aggregation\Term`
13+
- [Documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html)
14+
- [Implementation](https://github.com/Spameri/ElasticQuery/blob/master/src/Aggregation/Term.php)
15+
16+
##### Histogram Aggregation
17+
- Class `\Spameri\ElasticQuery\Aggregation\Histogram`
18+
- [Documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-histogram-aggregation.html)
19+
- [Implementation](https://github.com/Spameri/ElasticQuery/blob/master/src/Aggregation/Histogram.php)
20+
21+
##### Range Aggregation
22+
- Class `\Spameri\ElasticQuery\Aggregation\Range`
23+
- [Documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-range-aggregation.html)
24+
- [Implementation](https://github.com/Spameri/ElasticQuery/blob/master/src/Aggregation/Range.php)
25+
26+
##### AggregationCollection Aggregation
27+
- Class `\Spameri\ElasticQuery\Aggregation\AggregationCollection`
28+
- [Implementation](https://github.com/Spameri/ElasticQuery/blob/master/src/Aggregation/AggregationCollection.php)
29+
- Top level aggregations.
30+
31+
##### LeafAggregationCollection Aggregation
32+
- Class `\Spameri\ElasticQuery\Aggregation\LeafAggregationCollection`
33+
- [Implementation](https://github.com/Spameri/ElasticQuery/blob/master/src/Aggregation/LeafAggregationCollection.php)
34+
- Nested aggregations collection.

doc/04-result-objects.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Result objects
2+
3+
- Every object is as close to array response as possible. But as typed object.
4+
- Every response object implements `\Spameri\ElasticQuery\Response\ResultInterface`
5+
6+
## Implementations
7+
##### Result Single
8+
- Class `\Spameri\ElasticQuery\Response\ResultSingle`
9+
- [Implementation](https://github.com/Spameri/ElasticQuery/blob/master/src/Response/ResultSingle.php)
10+
11+
##### Result Search
12+
- Class `\Spameri\ElasticQuery\Response\ResultSearch`
13+
- [Implementation](https://github.com/Spameri/ElasticQuery/blob/master/src/Response/ResultSearch.php)
14+
- [Sample usage](https://github.com/Spameri/ElasticQuery/blob/master/tests/SpameriTests/ElasticQuery/Response/Result.phpt#L76)
15+
16+
##### Result Bulk
17+
- Class `\Spameri\ElasticQuery\Response\ResultBulk`
18+
- [Implementation](https://github.com/Spameri/ElasticQuery/blob/master/src/Response/ResultBulk.php)

src/Aggregation/Range.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
/**
7-
* https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-range-aggregation.html
7+
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-range-aggregation.html
88
*/
99
class Range implements LeafAggregationInterface
1010
{

src/Response/ResultSearch.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
namespace Spameri\ElasticQuery\Response;
44

55

6+
/**
7+
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/getting-started-search-API.html
8+
*/
69
class ResultSearch implements ResultInterface
710
{
811

0 commit comments

Comments
 (0)