Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 37 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ $ composer require novaway/elasticsearch-client

### Create an index

The first thing you'll need to do to use this library is to instatiate an index. This will be the keystone of the client.
The first thing you'll need to do to use this library is to instatiate a client and an index. They will be the keystones of the client.

```php
$index = new \Novaway\ElasticsearchClient\Index(
['127.0.0.1:9200'], # elasticsearch hosts
$client = \Elasticsearch\ClientBuilder::create()->setHosts(['127.0.0.1:9200'] # elasticsearch hosts);

$index = \Novaway\ElasticsearchClient\Index::createWithClient(
$client,
'main_index', # index name
[
'settings' => [
Expand Down Expand Up @@ -88,9 +90,10 @@ Use the `QueryBuilder` to build your query and execute it.

```php
use Novaway\ElasticsearchClient\Query\CombiningFactor;
use Novaway\ElasticsearchClient\Query\FullText\MatchQuery;

$queryBody = QueryBuilder::createNew()
->match('first_name', 'John', CombiningFactor::MUST)
->addQuery(new MatchQuery('first_name', 'John', CombiningFactor::MUST))
->getQueryBody()
;
$queryExecutor->execute($queryBody, 'my_type');
Expand All @@ -110,11 +113,28 @@ $queryBuilder = QueryBuilder::createNew(0, 10, 0.3);

This client provide several ways to improve querying :

- Filtering *(missing documentation)*
- [Aggregations](doc/aggregation.md)
- Result Formating *(missing documentation)*


- Supported Query DSL
* [Match All Query](src/Query/MatchAllQuery.php)
* Full Text Queries
* [Match Query](src/Query/FullText/MatchQuery.php)
* [Multi Match Query](src/Query/FullText/MultiMatchQuery.php)
* Term Level Queries
* [Term Query](src/Query/Term/TermQuery.php)
* [Range Query](src/Query/Term/RangeQuery.php)
* [Exist Query](src/Query/Term/ExistsQuery.php)
* [Prefix Query](src/Query/Term/PrefixQuery.php)
* [In Array Query](src/Query/Term/InArrayQuery.php)
* Compound Queries
* [Bool Query](src/Query/Compound/BoolQuery.php)
* [Function Score Query](src/Query/Compound/FunctionScore.php)
* Joining Queries
* [Nested Query](src/Query/Joining/NestedQuery.php)
* Geo Queries
* [GeoShape Query](src/Query/Geo/InlineGeoShapeQuery.php)
* [Geo Distance Query](src/Query/Geo/GeoDistanceQuery.php)

### Clear the index

You might want, for some reason, to purge an index. The `reload` method drops and recreates the index.
Expand All @@ -140,7 +160,7 @@ $index->hotswapToMain()

## Recommended usage with Symfony

If you are using this library in a symfony project, we recommend to use it as service.
If you are using this library in a symfony project, we recommend to use it as service, in comnbination with https://github.com/novaway/elasticsearch-bundle, which provides, among others, a ClientFactory.

```yml
# services.yml
Expand All @@ -160,12 +180,20 @@ parameters:
type: integer

services:
Novaway\ElasticsearchBundle\Elasticsearch\Client:
factory: Novaway\ElasticsearchBundle\Factory\ClientFactory:createClient
arguments:
- ['%elasticsearch_host%'] # define it in the parameter.yml file

myapp.search.index:
class: Novaway\ElasticsearchClient\Index
arguments:
- ['127.0.0.1:9200'] #define it in the parameter.yml file
- []
- 'myapp_myindex_%kernel.environment%'
- 'myapp.search.myindex.config'
- null
- null
- Novaway\ElasticsearchBundle\Elasticsearch\Client

myapp.search.object_indexer:
class: Novaway\ElasticsearchClient\ObjectIndexer
Expand Down
6 changes: 3 additions & 3 deletions src/Query/Compound/BoolQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
use Novaway\ElasticsearchClient\Query\Query;
use Webmozart\Assert\Assert;

/**
* https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html
*/
class BoolQuery implements Query
{

/** @var string */
private $combiningFactor;
/** @var Clause[] */
Expand Down Expand Up @@ -46,6 +48,4 @@ public function formatForQuery(): array

return ['bool' => $res];
}


}
3 changes: 3 additions & 0 deletions src/Query/Compound/FunctionScore.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Novaway\ElasticsearchClient\Query\Compound;

/**
* https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html
*/
interface FunctionScore
{
/**
Expand Down
5 changes: 3 additions & 2 deletions src/Query/FullText/MatchQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

namespace Novaway\ElasticsearchClient\Query\FullText;

//https://www.elastic.co/guide/en/elasticsearch/reference/5.6/query-dsl-match-query.html
use Novaway\ElasticsearchClient\Query\CombiningFactor;
use Novaway\ElasticsearchClient\Query\Query;
use Webmozart\Assert\Assert;


/**
* https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html
*/
class MatchQuery implements Query
{
/** @var string */
Expand Down
10 changes: 9 additions & 1 deletion src/Query/FullText/MultiMatchQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,20 @@
use Novaway\ElasticsearchClient\Query\Query;
use Webmozart\Assert\Assert;

/**
* https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html
*/
class MultiMatchQuery implements Query
{
/** @deprecated use Novaway\ElasticsearchClient\Query\FullText\MultiMatchType::BEST_FIELDS instead */
const BEST_FIELDS = 'best_fields';
/** @deprecated use Novaway\ElasticsearchClient\Query\FullText\MultiMatchType::MOST_FIELDS instead */
const MOST_FIELDS = 'most_fields';
/** @deprecated use Novaway\ElasticsearchClient\Query\FullText\MultiMatchType::CROSS_FIELDS instead */
const CROSS_FIELDS = 'cross_fields';
/** @deprecated use Novaway\ElasticsearchClient\Query\FullText\MultiMatchType::PHRASE instead */
const PHRASE = 'phrase';
/** @deprecated use Novaway\ElasticsearchClient\Query\FullText\MultiMatchType::PHRASE_PREFIX instead */
const PHRASE_PREFIX = 'phrase_prefix';
/** @var string */
private $value;
Expand All @@ -31,7 +39,7 @@ class MultiMatchQuery implements Query
* @param string $combiningFactor the combining factor
* @param array $options additional options
*/
public function __construct(string $value, array $fields, string $combiningFactor = CombiningFactor::SHOULD,array $options = [])
public function __construct(string $value, array $fields, string $combiningFactor = CombiningFactor::SHOULD, array $options = [])
{
Assert::oneOf($combiningFactor, CombiningFactor::toArray());
$fields = array_map(function($field) {
Expand Down
14 changes: 14 additions & 0 deletions src/Query/FullText/MultiMatchType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Novaway\ElasticsearchClient\Query\FullText;

use MyCLabs\Enum\Enum;

class MultiMatchType extends Enum
{
const BEST_FIELDS = 'best_fields';
const MOST_FIELDS = 'most_fields';
const CROSS_FIELDS = 'cross_fields';
const PHRASE = 'phrase';
const PHRASE_PREFIX = 'phrase_prefix';
}
3 changes: 3 additions & 0 deletions src/Query/Geo/DistanceUnits.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use MyCLabs\Enum\Enum;

/**
* https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#distance-units
*/
class DistanceUnits extends Enum
{
const MILES = 'miles';
Expand Down
7 changes: 5 additions & 2 deletions src/Query/Geo/GeoDistanceQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use Novaway\ElasticsearchClient\Query\Query;
use Webmozart\Assert\Assert;

/**
* https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-distance-query.html
*/
class GeoDistanceQuery implements Query, Filter
{
/** @var string */
Expand All @@ -31,8 +34,8 @@ class GeoDistanceQuery implements Query, Filter
* @param float $longitude
* @param float $distance
* @param string $combiningFactor
* @param string $unit Should be one of those https://www.elastic.co/guide/en/elasticsearch/reference/2.3/common-options.html#distance-units
* @param array $options Used to pass options from https://www.elastic.co/guide/en/elasticsearch/reference/2.3/query-dsl-geo-distance-query.html#_options_4
* @param string $unit Should be one of those https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#distance-units
* @param array $options Used to pass options from https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-distance-query.html#_options_4
*/
public function __construct(string $property, float $latitude, float $longitude, float $distance, string $combiningFactor = CombiningFactor::FILTER, string $unit = DistanceUnits::KM, array $options = [])
{
Expand Down
3 changes: 3 additions & 0 deletions src/Query/Joining/NestedQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
use Novaway\ElasticsearchClient\Query\Query;
use Webmozart\Assert\Assert;

/**
* https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html
*/
class NestedQuery implements Filter, Query
{
/** @var string */
Expand Down
4 changes: 3 additions & 1 deletion src/Query/MatchAllQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

namespace Novaway\ElasticsearchClient\Query;


/**
* https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-all-query.html
*/
class MatchAllQuery implements Query
{

Expand Down
3 changes: 3 additions & 0 deletions src/Query/Term/ExistsQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use Novaway\ElasticsearchClient\Query\Query;
use Webmozart\Assert\Assert;

/**
* https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-exists-query.html
*/
class ExistsQuery implements Filter, Query
{
/** @var string */
Expand Down
4 changes: 4 additions & 0 deletions src/Query/Term/InArrayQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
use Novaway\ElasticsearchClient\Query\Query;
use Webmozart\Assert\Assert;

/**
* Combines multiple TermQueries
* https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html
*/
class InArrayQuery implements Filter, Query
{
/** @var string */
Expand Down
3 changes: 3 additions & 0 deletions src/Query/Term/PrefixQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use Novaway\ElasticsearchClient\Query\Query;
use Webmozart\Assert\Assert;

/**
* https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-prefix-query.html
*/
class PrefixQuery implements Query
{
/** @var string */
Expand Down
13 changes: 13 additions & 0 deletions src/Query/Term/RangeOperator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Novaway\ElasticsearchClient\Query\Term;

use MyCLabs\Enum\Enum;

class RangeOperator extends Enum
{
const GREATER_THAN_OPERATOR = 'gt';
const GREATER_THAN_OR_EQUAL_OPERATOR = 'gte';
const LESS_THAN_OPERATOR = 'lt';
const LESS_THAN_OR_EQUAL_OPERATOR = 'lte';
}
7 changes: 7 additions & 0 deletions src/Query/Term/RangeQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,18 @@
use Novaway\ElasticsearchClient\Query\Query;
use Webmozart\Assert\Assert;

/**
* https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html
*/
class RangeQuery implements Filter, Query
{
/** @deprecated use Novaway\ElasticsearchClient\Query\Term\RangeOperator::GREATER_THAN_OPERATOR instead */
const GREATER_THAN_OPERATOR = 'gt';
/** @deprecated use Novaway\ElasticsearchClient\Query\Term\RangeOperator::GREATER_THAN_OR_EQUAL_OPERATOR instead */
const GREATER_THAN_OR_EQUAL_OPERATOR = 'gte';
/** @deprecated use Novaway\ElasticsearchClient\Query\Term\RangeOperator::LESS_THAN_OPERATOR instead */
const LESS_THAN_OPERATOR = 'lt';
/** @deprecated use Novaway\ElasticsearchClient\Query\Term\RangeOperator::LESS_THAN_OR_EQUAL_OPERATOR instead */
const LESS_THAN_OR_EQUAL_OPERATOR = 'lte';

/** @var string */
Expand Down
3 changes: 3 additions & 0 deletions src/Query/Term/TermQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use Novaway\ElasticsearchClient\Query\Query;
use Webmozart\Assert\Assert;

/**
* https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html
*/
class TermQuery implements Filter, Query
{
/** @var string */
Expand Down
3 changes: 3 additions & 0 deletions src/Score/BoostMode.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use MyCLabs\Enum\Enum;

/**
* https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#query-dsl-function-score-query
*/
class BoostMode extends Enum
{
const MULTIPLY = 'multiply';
Expand Down
15 changes: 15 additions & 0 deletions src/Score/DecayFunction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Novaway\ElasticsearchClient\Score;

use MyCLabs\Enum\Enum;

/**
* https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#_supported_decay_functions
*/
class DecayFunction extends Enum
{
const GAUSS = 'gauss';
const EXP = 'exp';
const LINEAR = 'linear';
}
9 changes: 8 additions & 1 deletion src/Score/DecayFunctionScore.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@
use Novaway\ElasticsearchClient\Query\Compound\FunctionScore as FunctionScore;
use Webmozart\Assert\Assert;

/**
* https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#function-decay
*/
class DecayFunctionScore implements FunctionScore
{
/** @deprecated use Novaway\ElasticsearchClient\Score\DecayFunction::GAUSS instead */
const GAUSS = 'gauss';
/** @deprecated use Novaway\ElasticsearchClient\Score\DecayFunction::EXP instead */
const EXP = 'exp';
/** @deprecated use Novaway\ElasticsearchClient\Score\DecayFunction::LINEAR instead */
const LINEAR = 'linear';

/** @deprecated use Novaway\ElasticsearchClient\Score\DecayFunction::toArray() instead */
public static $availableFunctions = [
self::GAUSS,
self::EXP,
Expand All @@ -36,7 +43,7 @@ class DecayFunctionScore implements FunctionScore

public function __construct(string $property, string $function, $origin, string $offset, string $scale, array $options = [], float $decay = self::DECAY)
{
Assert::oneOf($function, self::$availableFunctions);
Assert::oneOf($function, DecayFunction::toArray());
$this->property = $property;
$this->function = $function;
$this->origin = $origin;
Expand Down
3 changes: 3 additions & 0 deletions src/Score/FunctionScoreOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use Webmozart\Assert\Assert;

/**
* https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#query-dsl-function-score-query
*/
class FunctionScoreOptions
{
/** @var string */
Expand Down
3 changes: 3 additions & 0 deletions src/Score/RandomScore.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use Novaway\ElasticsearchClient\Query\Compound\FunctionScore as FunctionScore;

/**
* https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#function-random
*/
class RandomScore implements FunctionScore
{
/** @var string */
Expand Down
3 changes: 3 additions & 0 deletions src/Score/ScoreMode.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use MyCLabs\Enum\Enum;

/**
* https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#query-dsl-function-score-query
*/
class ScoreMode extends Enum
{
const MULTIPLY = 'multiply';
Expand Down
Loading