diff --git a/README.md b/README.md index fb87a96..99d04fa 100644 --- a/README.md +++ b/README.md @@ -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' => [ @@ -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'); @@ -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. @@ -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 @@ -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 diff --git a/src/Query/Compound/BoolQuery.php b/src/Query/Compound/BoolQuery.php index 4e72aae..e31672d 100644 --- a/src/Query/Compound/BoolQuery.php +++ b/src/Query/Compound/BoolQuery.php @@ -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[] */ @@ -46,6 +48,4 @@ public function formatForQuery(): array return ['bool' => $res]; } - - } diff --git a/src/Query/Compound/FunctionScore.php b/src/Query/Compound/FunctionScore.php index 7b6d5ce..6507f8a 100644 --- a/src/Query/Compound/FunctionScore.php +++ b/src/Query/Compound/FunctionScore.php @@ -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 { /** diff --git a/src/Query/FullText/MatchQuery.php b/src/Query/FullText/MatchQuery.php index beffc59..7e6aab7 100755 --- a/src/Query/FullText/MatchQuery.php +++ b/src/Query/FullText/MatchQuery.php @@ -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 */ diff --git a/src/Query/FullText/MultiMatchQuery.php b/src/Query/FullText/MultiMatchQuery.php index 0ea0f52..9cdf9ca 100644 --- a/src/Query/FullText/MultiMatchQuery.php +++ b/src/Query/FullText/MultiMatchQuery.php @@ -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; @@ -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) { diff --git a/src/Query/FullText/MultiMatchType.php b/src/Query/FullText/MultiMatchType.php new file mode 100644 index 0000000..1284fd1 --- /dev/null +++ b/src/Query/FullText/MultiMatchType.php @@ -0,0 +1,14 @@ +property = $property; $this->function = $function; $this->origin = $origin; diff --git a/src/Score/FunctionScoreOptions.php b/src/Score/FunctionScoreOptions.php index 625cd43..fb41ba6 100644 --- a/src/Score/FunctionScoreOptions.php +++ b/src/Score/FunctionScoreOptions.php @@ -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 */ diff --git a/src/Score/RandomScore.php b/src/Score/RandomScore.php index adead31..20ba71e 100644 --- a/src/Score/RandomScore.php +++ b/src/Score/RandomScore.php @@ -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 */ diff --git a/src/Score/ScoreMode.php b/src/Score/ScoreMode.php index 413f46e..774cdbd 100644 --- a/src/Score/ScoreMode.php +++ b/src/Score/ScoreMode.php @@ -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'; diff --git a/src/Score/ScriptScore.php b/src/Score/ScriptScore.php index 616e60b..275df93 100644 --- a/src/Score/ScriptScore.php +++ b/src/Score/ScriptScore.php @@ -4,11 +4,14 @@ namespace Novaway\ElasticsearchClient\Score; +use Novaway\ElasticsearchClient\Query\Compound\FunctionScore as FunctionScore; use Novaway\ElasticsearchClient\Script\ScriptingLanguage; use Novaway\ElasticsearchClient\Script\Traits\ScriptTrait; use Webmozart\Assert\Assert; -use Novaway\ElasticsearchClient\Query\Compound\FunctionScore as FunctionScore; +/** + * https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html + */ class ScriptScore implements FunctionScore { use ScriptTrait { diff --git a/src/Script/ScriptField.php b/src/Script/ScriptField.php index 213295e..e649695 100644 --- a/src/Script/ScriptField.php +++ b/src/Script/ScriptField.php @@ -7,6 +7,9 @@ use Novaway\ElasticsearchClient\Script\Traits\ScriptTrait; use Webmozart\Assert\Assert; +/** + * https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-script-fields.html + */ class ScriptField { use ScriptTrait;