├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── LICENSE ├── README.md ├── composer.json ├── docs ├── Aggregation │ ├── Bucketing │ │ ├── Children.md │ │ ├── DateHistogram.md │ │ ├── DateRange.md │ │ ├── DiversifiedSampler.md │ │ ├── Filter.md │ │ ├── Filters.md │ │ ├── GeoDistance.md │ │ ├── GeoHashGrid.md │ │ ├── Global.md │ │ ├── Histogram.md │ │ ├── Ipv4Range.md │ │ ├── Missing.md │ │ ├── Nested.md │ │ ├── Range.md │ │ ├── ReverseNested.md │ │ ├── SamplerAgg.md │ │ ├── SignificantTerms.md │ │ └── Terms.md │ ├── Matrix │ │ └── MatrixStats.md │ ├── Metric │ │ ├── Avg.md │ │ ├── Cardinality.md │ │ ├── ExtendedStats.md │ │ ├── GeoBounds.md │ │ ├── GeoCentroid.md │ │ ├── Max.md │ │ ├── Min.md │ │ ├── PercentileRanks.md │ │ ├── Percentiles.md │ │ ├── ScriptedMetric.md │ │ ├── Stats.md │ │ ├── Sum.md │ │ ├── TopHits.md │ │ └── ValueCount.md │ ├── Pipeline │ │ ├── AvgBucket.md │ │ ├── BucketScript.md │ │ ├── BucketSelector.md │ │ ├── CumulativeSum.md │ │ ├── Derrivative.md │ │ ├── ExtendedStatsBucket.md │ │ ├── MaxBucket.md │ │ ├── MinBucket.md │ │ ├── PercentilesBucket.md │ │ ├── SerialDifferencing.md │ │ ├── StatsBucket.md │ │ └── SumBucket.md │ └── index.md ├── HowTo │ ├── CustomParameters.md │ └── HowToSearch.md ├── InnerHits │ ├── Nested.md │ └── Parent.md ├── Query │ ├── Compound │ │ ├── Bool.md │ │ ├── Boosting.md │ │ ├── ConstantScore.md │ │ ├── DisMax.md │ │ ├── FunctionScore.md │ │ └── Indices.md │ ├── FullText │ │ ├── CommonTerms.md │ │ ├── Match.md │ │ ├── MatchPhrase.md │ │ ├── MatchPhrasePrefix.md │ │ ├── MultiMatch.md │ │ ├── QueryString.md │ │ └── SimpleQueryString.md │ ├── Geo │ │ ├── GeoBoundingBox.md │ │ ├── GeoDistance.md │ │ ├── GeoDistanceRange.md │ │ ├── GeoPolygon.md │ │ └── GeoShape.md │ ├── Joining │ │ ├── HasChild.md │ │ ├── HasParent.md │ │ └── Nested.md │ ├── MatchAll.md │ ├── Span │ │ ├── SpanContaining.md │ │ ├── SpanFirst.md │ │ ├── SpanMultiTerm.md │ │ ├── SpanNear.md │ │ ├── SpanNot.md │ │ ├── SpanOr.md │ │ ├── SpanTerm.md │ │ └── SpanWithin.md │ ├── Specialized │ │ ├── MoreLikeThis.md │ │ ├── Script.md │ │ └── Template.md │ └── TermLevel │ │ ├── Exists.md │ │ ├── Fuzzy.md │ │ ├── Ids.md │ │ ├── Prefix.md │ │ ├── Range.md │ │ ├── Regexp.md │ │ ├── Term.md │ │ ├── Terms.md │ │ ├── Type.md │ │ └── Wildcard.md ├── Suggest │ └── index.md └── index.md ├── phpunit.xml.dist ├── src ├── Aggregation │ ├── AbstractAggregation.php │ ├── Bucketing │ │ ├── AdjacencyMatrixAggregation.php │ │ ├── AutoDateHistogramAggregation.php │ │ ├── ChildrenAggregation.php │ │ ├── CompositeAggregation.php │ │ ├── DateHistogramAggregation.php │ │ ├── DateRangeAggregation.php │ │ ├── DiversifiedSamplerAggregation.php │ │ ├── FilterAggregation.php │ │ ├── FiltersAggregation.php │ │ ├── GeoDistanceAggregation.php │ │ ├── GeoHashGridAggregation.php │ │ ├── GlobalAggregation.php │ │ ├── HistogramAggregation.php │ │ ├── Ipv4RangeAggregation.php │ │ ├── MissingAggregation.php │ │ ├── NestedAggregation.php │ │ ├── RangeAggregation.php │ │ ├── ReverseNestedAggregation.php │ │ ├── SamplerAggregation.php │ │ ├── SignificantTermsAggregation.php │ │ ├── SignificantTextAggregation.php │ │ └── TermsAggregation.php │ ├── Matrix │ │ └── MatrixStatsAggregation.php │ ├── Metric │ │ ├── AvgAggregation.php │ │ ├── CardinalityAggregation.php │ │ ├── ExtendedStatsAggregation.php │ │ ├── GeoBoundsAggregation.php │ │ ├── GeoCentroidAggregation.php │ │ ├── MaxAggregation.php │ │ ├── MinAggregation.php │ │ ├── PercentileRanksAggregation.php │ │ ├── PercentilesAggregation.php │ │ ├── ScriptedMetricAggregation.php │ │ ├── StatsAggregation.php │ │ ├── SumAggregation.php │ │ ├── TopHitsAggregation.php │ │ └── ValueCountAggregation.php │ ├── Pipeline │ │ ├── AbstractPipelineAggregation.php │ │ ├── AvgBucketAggregation.php │ │ ├── BucketScriptAggregation.php │ │ ├── BucketSelectorAggregation.php │ │ ├── BucketSortAggregation.php │ │ ├── CumulativeSumAggregation.php │ │ ├── DerivativeAggregation.php │ │ ├── ExtendedStatsBucketAggregation.php │ │ ├── MaxBucketAggregation.php │ │ ├── MinBucketAggregation.php │ │ ├── MovingAverageAggregation.php │ │ ├── MovingFunctionAggregation.php │ │ ├── PercentilesBucketAggregation.php │ │ ├── SerialDifferencingAggregation.php │ │ ├── StatsBucketAggregation.php │ │ └── SumBucketAggregation.php │ └── Type │ │ ├── BucketingTrait.php │ │ └── MetricTrait.php ├── BuilderBag.php ├── BuilderInterface.php ├── FieldAwareTrait.php ├── Highlight │ └── Highlight.php ├── InnerHit │ ├── NestedInnerHit.php │ └── ParentInnerHit.php ├── NameAwareTrait.php ├── NamedBuilderInterface.php ├── ParametersTrait.php ├── Query │ ├── Compound │ │ ├── BoolQuery.php │ │ ├── BoostingQuery.php │ │ ├── ConstantScoreQuery.php │ │ ├── DisMaxQuery.php │ │ └── FunctionScoreQuery.php │ ├── FullText │ │ ├── CommonTermsQuery.php │ │ ├── MatchPhrasePrefixQuery.php │ │ ├── MatchPhraseQuery.php │ │ ├── MatchQuery.php │ │ ├── MultiMatchQuery.php │ │ ├── QueryStringQuery.php │ │ └── SimpleQueryStringQuery.php │ ├── Geo │ │ ├── GeoBoundingBoxQuery.php │ │ ├── GeoDistanceQuery.php │ │ ├── GeoPolygonQuery.php │ │ └── GeoShapeQuery.php │ ├── Joining │ │ ├── HasChildQuery.php │ │ ├── HasParentQuery.php │ │ ├── NestedQuery.php │ │ └── ParentIdQuery.php │ ├── MatchAllQuery.php │ ├── Span │ │ ├── FieldMaskingSpanQuery.php │ │ ├── SpanContainingQuery.php │ │ ├── SpanFirstQuery.php │ │ ├── SpanMultiTermQuery.php │ │ ├── SpanNearQuery.php │ │ ├── SpanNotQuery.php │ │ ├── SpanOrQuery.php │ │ ├── SpanQueryInterface.php │ │ ├── SpanTermQuery.php │ │ └── SpanWithinQuery.php │ ├── Specialized │ │ ├── MoreLikeThisQuery.php │ │ ├── ScriptQuery.php │ │ └── TemplateQuery.php │ └── TermLevel │ │ ├── ExistsQuery.php │ │ ├── FuzzyQuery.php │ │ ├── IdsQuery.php │ │ ├── PrefixQuery.php │ │ ├── RangeQuery.php │ │ ├── RegexpQuery.php │ │ ├── TermQuery.php │ │ ├── TermsQuery.php │ │ ├── TermsSetQuery.php │ │ ├── TypeQuery.php │ │ └── WildcardQuery.php ├── ScriptAwareTrait.php ├── Search.php ├── SearchEndpoint │ ├── AbstractSearchEndpoint.php │ ├── AggregationsEndpoint.php │ ├── HighlightEndpoint.php │ ├── InnerHitsEndpoint.php │ ├── PostFilterEndpoint.php │ ├── QueryEndpoint.php │ ├── SearchEndpointFactory.php │ ├── SearchEndpointInterface.php │ ├── SortEndpoint.php │ └── SuggestEndpoint.php ├── Serializer │ ├── Normalizer │ │ ├── AbstractNormalizable.php │ │ ├── CustomReferencedNormalizer.php │ │ └── OrderedNormalizerInterface.php │ └── OrderedSerializer.php ├── Sort │ ├── FieldSort.php │ └── NestedSort.php └── Suggest │ └── Suggest.php └── tests ├── Functional ├── AbstractElasticsearchTestCase.php └── Query │ ├── FunctionScoreQueryTest.php │ └── MatchAllQueryTest.php └── Unit ├── Aggregation ├── Bucketing │ ├── AdjacencyMatrixAggregationTest.php │ ├── AudoDateHistogramAggregationTest.php │ ├── ChildrenAggregationTest.php │ ├── CompositeAggregationTest.php │ ├── DateHistogramAggregationTest.php │ ├── DateRangeAggregationTest.php │ ├── FilterAggregationTest.php │ ├── FiltersAggregationTest.php │ ├── GeoDistanceAggregationTest.php │ ├── GeoHashGridAggregationTest.php │ ├── GlobalAggregationTest.php │ ├── Ipv4RangeAggregationTest.php │ ├── MissingAggregationTest.php │ ├── NestedAggregationTest.php │ ├── RangeAggregationTest.php │ ├── ReverseNestedAggregationTest.php │ ├── SamplerAggregationTest.php │ ├── SignificantTermsAggregationTest.php │ ├── SignificantTextAggregationTest.php │ └── TermsAggregationTest.php ├── Metric │ ├── CardinalityAggregationTest.php │ ├── GeoBoundsAggregationTest.php │ ├── GeoCentroidAggregationTest.php │ ├── PercentileRanksAggregationTest.php │ ├── PercentilesAggregationTest.php │ ├── StatsAggregationTest.php │ └── TopHitsAggregationTest.php └── Pipeline │ ├── AvgBucketAggregationTest.php │ ├── BucketScriptAggregationTest.php │ ├── BucketSelectorAggregationTest.php │ ├── BucketSortAggregationTest.php │ ├── CumulativeSumAggregationTest.php │ ├── DerivativeAggregationTest.php │ ├── ExtendedStatsBucketAggregationTest.php │ ├── MaxBucketAggregationTest.php │ ├── MinBucketAggregationTest.php │ ├── MovingFunctionAggregationTest.php │ ├── PercentilesBucketAggregationTest.php │ ├── SerialDifferencingAggregationTest.php │ ├── StatsBucketAggregationTest.php │ └── SumBucketAggregationTest.php ├── BuilderBagTest.php ├── Highlight └── HighlightTest.php ├── InnerHit ├── NestedInnerHitTest.php └── ParentInnerHitTest.php ├── ParametersTraitTest.php ├── Query ├── Compound │ ├── BoolQueryTest.php │ ├── BoostingQueryTest.php │ ├── ConstantScoreQueryTest.php │ ├── DisMaxQueryTest.php │ └── FunctionScoreQueryTest.php ├── FullText │ ├── CommonTermsQueryTest.php │ ├── MatchPhrasePrefixQueryTest.php │ ├── MatchPhraseQueryTest.php │ ├── MatchQueryTest.php │ ├── MultiMatchQueryTest.php │ ├── QueryStringQueryTest.php │ └── SimpleQueryStringQueryTest.php ├── Geo │ ├── GeoBoundingBoxQueryTest.php │ ├── GeoDistanceQueryTest.php │ ├── GeoPolygonQueryTest.php │ └── GeoShapeQueryTest.php ├── Joining │ ├── HasChildQueryTest.php │ ├── HasParentQueryTest.php │ └── NestedQueryTest.php ├── MatchAllQueryTest.php ├── Span │ ├── FieldMaskingSpanQueryTest.php │ ├── SpanContainingQueryTest.php │ ├── SpanFirstQueryTest.php │ ├── SpanMultiTermQueryTest.php │ ├── SpanNearQueryTest.php │ ├── SpanNotQueryTest.php │ ├── SpanOrQueryTest.php │ ├── SpanTermQueryTest.php │ └── SpanWithinQueryTest.php ├── Specialized │ ├── MoreLikeThisQueryTest.php │ ├── ScriptQueryTest.php │ └── TemplateQueryTest.php └── TermLevel │ ├── ExistsQueryTest.php │ ├── FuzzyQueryTest.php │ ├── IdsQueryTest.php │ ├── PrefixQueryTest.php │ ├── RangeQueryTest.php │ ├── RegexpQueryTest.php │ ├── TermQueryTest.php │ ├── TermsQueryTest.php │ ├── TermsSetQueryTest.php │ ├── TypeQueryTest.php │ └── WildcardQueryTest.php ├── SearchEndpoint ├── AggregationsEndpointTest.php ├── HighlightEndpointTest.php ├── InnerHitsEndpointTest.php ├── PostFilterEndpointTest.php ├── QueryEndpointTest.php ├── SearchEndpointFactoryTest.php ├── SortEndpointTest.php └── SuggestEndpointTest.php ├── SearchTest.php ├── Sort ├── FieldSortTest.php └── NestedSortTest.php └── Suggest └── SuggestTest.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor/ 2 | /phpunit.xml 3 | /composer.lock 4 | coverage.xml -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: php 3 | php: 4 | - 7.1 5 | - 7.2 6 | - 7.3 7 | - 7.4snapshot 8 | matrix: 9 | allow_failures: 10 | - php: 7.4snapshot 11 | env: 12 | global: 13 | - ES_VERSION=7.4.0 ES_DOWNLOAD_URL=https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-${ES_VERSION}-linux-x86_64.tar.gz 14 | install: 15 | - wget ${ES_DOWNLOAD_URL} 16 | - tar -xzf elasticsearch-${ES_VERSION}-linux-x86_64.tar.gz 17 | - ./elasticsearch-${ES_VERSION}/bin/elasticsearch -d 18 | before_script: 19 | - if [ "$GITHUB_COMPOSER_AUTH" ]; then composer config -g github-oauth.github.com $GITHUB_COMPOSER_AUTH; fi 20 | - composer install --no-interaction --prefer-dist 21 | script: 22 | - wget -q --waitretry=1 --retry-connrefused -T 10 -O - http://127.0.0.1:9200 23 | - vendor/bin/phpunit --coverage-clover=coverage.xml 24 | - vendor/bin/phpcs -p --standard=PSR2 --ignore=vendor/ ./ 25 | after_script: 26 | - travis_retry bash <(curl -s https://codecov.io/bash) 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014-2016 NFQ Technologies UAB 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is furnished 8 | to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ongr/elasticsearch-dsl", 3 | "description": "Elasticsearch DSL library", 4 | "type": "library", 5 | "homepage": "http://ongr.io", 6 | "license": "MIT", 7 | "authors": [ 8 | { 9 | "name": "ONGR team", 10 | "homepage": "http://www.ongr.io" 11 | } 12 | ], 13 | "require": { 14 | "php": "^7.0", 15 | "symfony/serializer": "^3.0|^4.0", 16 | "paragonie/random_compat": "*" 17 | }, 18 | "require-dev": { 19 | "elasticsearch/elasticsearch": "^7.0", 20 | "phpunit/phpunit": "~6.0", 21 | "squizlabs/php_codesniffer": "^3.0" 22 | }, 23 | "suggest": { 24 | "elasticsearch/elasticsearch": "This library is for elasticsearch/elasticsearch client to enhance it with DSL functionality." 25 | }, 26 | "autoload": { 27 | "psr-4": { 28 | "ONGR\\ElasticsearchDSL\\": "src/" 29 | } 30 | }, 31 | "autoload-dev": { 32 | "psr-4": { 33 | "ONGR\\ElasticsearchDSL\\Tests\\": "tests/" 34 | } 35 | }, 36 | "minimum-stability": "dev", 37 | "extra": { 38 | "branch-alias": { 39 | "dev-master": "7.0-dev" 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /docs/Aggregation/Bucketing/Children.md: -------------------------------------------------------------------------------- 1 | # Children Aggregation 2 | 3 | > More info about children aggregation is in the [official elasticsearch docs][1] 4 | 5 | A special single bucket aggregation that enables aggregating from buckets on parent 6 | document types to buckets on child documents. 7 | 8 | ## Simple example 9 | 10 | ```JSON 11 | { 12 | "aggregations": { 13 | "author_count": { 14 | "children": { 15 | "type": "answer" 16 | }, 17 | "aggregations": { 18 | "top_names": { 19 | "terms": { 20 | "field": "owner.display_name" 21 | } 22 | } 23 | } 24 | } 25 | } 26 | } 27 | ``` 28 | 29 | And now the query via DSL: 30 | 31 | ```php 32 | $termsAggregation = new TermsAggregation('top_names', 'owner.display_name'); 33 | 34 | $childrenAggregation = new ChildrenAggregation('author_count', 'answer'); 35 | $childrenAggregation->addAggregation($termsAggregation); 36 | 37 | $search = new Search(); 38 | $search->addAggregation($childrenAggregation); 39 | 40 | $queryArray = $search->toArray(); 41 | ``` 42 | 43 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-children-aggregation.html 44 | -------------------------------------------------------------------------------- /docs/Aggregation/Bucketing/DateRange.md: -------------------------------------------------------------------------------- 1 | # Date Range Aggregation 2 | 3 | > More info about date range aggregation is in the [official elasticsearch docs][1] 4 | 5 | A range aggregation that is dedicated for date values. 6 | 7 | ## Simple example 8 | 9 | ```JSON 10 | { 11 | "aggregations": { 12 | "range": { 13 | "date_range": { 14 | "field": "date", 15 | "format": "MM-yyy", 16 | "ranges": [ 17 | { "to": "now-10M/M" }, 18 | { "from": "now-10M/M" } 19 | ] 20 | } 21 | } 22 | } 23 | } 24 | ``` 25 | 26 | And now the query via DSL: 27 | 28 | ```php 29 | $dateRangeAggregation = new DateRangeAggregation('range'); 30 | $dateRangeAggregation->setField('date'); 31 | $dateRangeAggregation->setFormat('MM-yyy'); 32 | $dateRangeAggregation->addRange(null, 'now-10M/M'); 33 | $dateRangeAggregation->addRange('now-10M/M', null); 34 | 35 | $search = new Search(); 36 | $search->addAggregation($dateRangeAggregation); 37 | 38 | $queryArray = $search->toArray(); 39 | ``` 40 | 41 | Or : 42 | 43 | ```php 44 | $dateRangeAggregation = new DateRangeAggregation( 45 | 'range', 46 | 'date', 47 | 'MM-yyy', 48 | [ 49 | ['to' => 'now-10M/M'], 50 | ['from' => 'now-10M/M'], 51 | ] 52 | ); 53 | 54 | $search = new Search(); 55 | $search->addAggregation($dateRangeAggregation); 56 | 57 | $queryArray = $search->toArray(); 58 | ``` 59 | 60 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-daterange-aggregation.html 61 | -------------------------------------------------------------------------------- /docs/Aggregation/Bucketing/DiversifiedSampler.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ongr-io/ElasticsearchDSL/b4c3c8982bca497661139a1c2967a81f8f9bb9b6/docs/Aggregation/Bucketing/DiversifiedSampler.md -------------------------------------------------------------------------------- /docs/Aggregation/Bucketing/Filter.md: -------------------------------------------------------------------------------- 1 | # Filter Aggregation 2 | 3 | > More info about filter aggregation is in the [official elasticsearch docs][1] 4 | 5 | Defines a single bucket of all the documents in the current document set context that 6 | match a specified filter. Often this will be used to narrow down the current aggregation 7 | context to a specific set of documents. 8 | 9 | ## Simple example 10 | 11 | ```JSON 12 | { 13 | "aggregations" : { 14 | "grades_stats" : { 15 | "filter" : { "term": { "color": "red" } }, 16 | "aggregations" : { 17 | "avg_price" : { "avg" : { "field" : "price" } } 18 | } 19 | } 20 | } 21 | } 22 | ``` 23 | 24 | And now the query via DSL: 25 | 26 | ```php 27 | $termFilter = new TermQuery('color', 'red'); 28 | $avgAggregation = new AvgAggregation('avg_price', 'price'); 29 | 30 | $filterAggregation = new FilterAggregation('grades_stats', $termFilter); 31 | $filterAggregation->addAggregation($avgAggregation); 32 | 33 | $search = new Search(); 34 | $search->addAggregation($filterAggregation); 35 | 36 | $queryArray = $search->toArray(); 37 | ``` 38 | 39 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filter-aggregation.html 40 | -------------------------------------------------------------------------------- /docs/Aggregation/Bucketing/GeoDistance.md: -------------------------------------------------------------------------------- 1 | # Geo Distance Aggregation 2 | 3 | > More info about geo distance aggregation is in the [official elasticsearch docs][1] 4 | 5 | A multi-bucket aggregation that works on geo_point fields 6 | and conceptually works very similar to the range aggregation. 7 | 8 | ## Simple example 9 | 10 | ```JSON 11 | { 12 | "aggregations" : { 13 | "rings_around_amsterdam" : { 14 | "geo_distance" : { 15 | "field" : "location", 16 | "origin" : "52.3760, 4.894", 17 | "ranges" : [ 18 | { "to" : 100 }, 19 | { "from" : 100, "to" : 300 }, 20 | { "from" : 300 } 21 | ] 22 | } 23 | } 24 | } 25 | } 26 | ``` 27 | 28 | And now the query via DSL: 29 | 30 | ```php 31 | $geoDistanceAggregation = new GeoDistanceAggregation( 32 | 'rings_around_amsterdam', 33 | 'location', 34 | '52.3760, 4.894', 35 | [ 36 | ['to' => 100], 37 | ['from' => 100, 'to' => 300], 38 | ['from' => 300], 39 | ] 40 | ); 41 | 42 | $search = new Search(); 43 | $search->addAggregation($geoDistanceAggregation); 44 | 45 | $queryArray = $search->toArray(); 46 | ``` 47 | 48 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-geodistance-aggregation.html 49 | -------------------------------------------------------------------------------- /docs/Aggregation/Bucketing/GeoHashGrid.md: -------------------------------------------------------------------------------- 1 | # Geo Hash Grid Aggregation 2 | 3 | > More info about geo hash grid aggregation is in the [official elasticsearch docs][1] 4 | 5 | A multi-bucket aggregation that works on geo_point fields and groups points into buckets 6 | that represent cells in a grid. 7 | 8 | ## Simple example 9 | 10 | ```JSON 11 | { 12 | "aggregations" : { 13 | "GrainGeoHashGrid" : { 14 | "geohash_grid" : { 15 | "field" : "location", 16 | "precision" : 3 17 | } 18 | } 19 | } 20 | } 21 | ``` 22 | 23 | And now the query via DSL: 24 | 25 | ```php 26 | $geoHashGridAggregation = new GeoHashGridAggregation( 27 | 'GrainGeoHashGrid', 28 | 'location', 29 | 3 30 | ); 31 | 32 | $search = new Search(); 33 | $search->addAggregation($geoHashGridAggregation); 34 | 35 | $queryArray = $search->toArray(); 36 | ``` 37 | 38 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-geohashgrid-aggregation.html 39 | -------------------------------------------------------------------------------- /docs/Aggregation/Bucketing/Global.md: -------------------------------------------------------------------------------- 1 | # Global Aggregation 2 | 3 | > More info about cardinality aggregation is in the [official elasticsearch docs][1] 4 | 5 | Defines a single bucket of all the documents within the search execution 6 | context. This context is defined by the indices and the document types 7 | you’re searching on, but is **not influenced** by the search query itself. 8 | 9 | ## Simple example 10 | 11 | ```JSON 12 | { 13 | "aggregations": { 14 | "all_products": { 15 | "global": {}, 16 | "aggregations": { 17 | "avg_price": { 18 | "avg": { 19 | "field": "price" 20 | } 21 | } 22 | } 23 | } 24 | } 25 | } 26 | ``` 27 | 28 | And now the query via DSL: 29 | 30 | ```php 31 | $avgAggregation = new AvgAggregation('avg_price', 'price'); 32 | $globalAggregation = new GlobalAggregation('all_products'); 33 | $globalAggregation->addAggregation($avgAggregation); 34 | 35 | $search = new Search(); 36 | $search->addAggregation($globalAggregation); 37 | 38 | $queryArray = $search->toArray(); 39 | ``` 40 | 41 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-global-aggregation.html 42 | -------------------------------------------------------------------------------- /docs/Aggregation/Bucketing/Histogram.md: -------------------------------------------------------------------------------- 1 | # Histogram Aggregation 2 | 3 | > More info about histogram aggregation is in the [official elasticsearch docs][1] 4 | 5 | A multi-bucket values source based aggregation that can be applied on numeric values extracted from 6 | the documents. It dynamically builds fixed size (a.k.a. interval) buckets over the values. 7 | 8 | ## Simple example 9 | 10 | ```JSON 11 | { 12 | "aggregations": { 13 | "prices": { 14 | "histogram": { 15 | "field": "price", 16 | "interval": 50 17 | } 18 | } 19 | } 20 | } 21 | ``` 22 | 23 | And now the query via DSL: 24 | 25 | ```php 26 | $histogramAggregation = new HistogramAggregation('prices', 'price', 50); 27 | 28 | $search = new Search(); 29 | $search->addAggregation($histogramAggregation); 30 | 31 | $queryArray = $search->toArray(); 32 | ``` 33 | 34 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-histogram-aggregation.html 35 | -------------------------------------------------------------------------------- /docs/Aggregation/Bucketing/Missing.md: -------------------------------------------------------------------------------- 1 | # Missing Aggregation 2 | 3 | > More info about missing aggregation is in the [official elasticsearch docs][1] 4 | 5 | A field data based single bucket aggregation, that creates a bucket of all documents 6 | in the current document set context that are missing a field value. 7 | 8 | ## Simple example 9 | 10 | ```JSON 11 | { 12 | "aggregations" : { 13 | "products_without_a_price" : { 14 | "missing" : { "field" : "price" } 15 | } 16 | } 17 | } 18 | ``` 19 | 20 | And now the query via DSL: 21 | 22 | ```php 23 | $missingAggregation = new MissingAggregation('products_without_a_price', 'price'); 24 | 25 | $search = new Search(); 26 | $search->addAggregation($missingAggregation); 27 | 28 | $queryArray = $search->toArray(); 29 | ``` 30 | 31 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-missing-aggregation.html 32 | -------------------------------------------------------------------------------- /docs/Aggregation/Bucketing/Nested.md: -------------------------------------------------------------------------------- 1 | # Nested Aggregation 2 | 3 | > More info about nested aggregation is in the [official elasticsearch docs][1] 4 | 5 | A special single bucket aggregation that enables aggregating nested documents. 6 | 7 | ## Simple example 8 | 9 | ```JSON 10 | { 11 | "aggregations" : { 12 | "resellers" : { 13 | "nested" : { 14 | "path" : "resellers" 15 | }, 16 | "aggregations" : { 17 | "min_price" : { "min" : { "field" : "resellers.price" } } 18 | } 19 | } 20 | } 21 | } 22 | ``` 23 | 24 | And now the query via DSL: 25 | 26 | ```php 27 | $minAggregation = new MinAggregation('min_price', 'resellers.price'); 28 | $nestedAggregation = new NestedAggregation('resellers', 'resellers'); 29 | $nestedAggregation->addAggregation($minAggregation); 30 | 31 | $search = new Search(); 32 | $search->addAggregation($nestedAggregation); 33 | 34 | $queryArray = $search->toArray(); 35 | ``` 36 | 37 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-nested-aggregation.html 38 | -------------------------------------------------------------------------------- /docs/Aggregation/Bucketing/SamplerAgg.md: -------------------------------------------------------------------------------- 1 | # Sampler Aggregation 2 | 3 | > More info about histogram aggregation is in the [official elasticsearch docs][1] 4 | 5 | A filtering aggregation used to limit any sub aggregations' processing to a sample of the top-scoring documents. Optionally, 6 | diversity settings can be used to limit the number of matches that share a common value such as an "author". 7 | 8 | ## Simple example 9 | 10 | ```JSON 11 | { 12 | "aggregations": { 13 | "sample": { 14 | "sampler": { 15 | "shard_size": 200, 16 | "field" : "user.id" 17 | }, 18 | "aggs": { 19 | "keywords": { 20 | "significant_terms": { 21 | "field": "text" 22 | } 23 | } 24 | } 25 | } 26 | } 27 | } 28 | ``` 29 | 30 | And now the query via DSL: 31 | 32 | ```php 33 | $samplerAggregation = new SamplerAggregation('sample', 'user.id', 200); 34 | $samplerAggregation->addAggregation( 35 | new SignificantTermsAggregation('keywords', 'text') 36 | ); 37 | 38 | $search = new Search(); 39 | $search->addAggregation($samplerAggregation); 40 | 41 | $queryArray = $search->toArray(); 42 | ``` 43 | 44 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-sampler-aggregation.html 45 | -------------------------------------------------------------------------------- /docs/Aggregation/Bucketing/Terms.md: -------------------------------------------------------------------------------- 1 | # Terms Aggregation 2 | 3 | > More info about terms aggregation is in the [official elasticsearch docs][1] 4 | 5 | A multi-bucket value source based aggregation where buckets are dynamically 6 | built - one per unique value. 7 | 8 | ## Simple example 9 | 10 | ```JSON 11 | { 12 | "aggregations" : { 13 | "genders" : { 14 | "terms" : { "field" : "gender" } 15 | } 16 | } 17 | } 18 | ``` 19 | 20 | And now the query via DSL: 21 | 22 | ```php 23 | $termsAggregation = new TermsAggregation('genders', 'gender'); 24 | 25 | $search = new Search(); 26 | $search->addAggregation($termsAggregation); 27 | 28 | $queryArray = $search->toArray(); 29 | ``` 30 | 31 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html 32 | -------------------------------------------------------------------------------- /docs/Aggregation/Matrix/MatrixStats.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ongr-io/ElasticsearchDSL/b4c3c8982bca497661139a1c2967a81f8f9bb9b6/docs/Aggregation/Matrix/MatrixStats.md -------------------------------------------------------------------------------- /docs/Aggregation/Metric/Avg.md: -------------------------------------------------------------------------------- 1 | # Avg Aggregation 2 | 3 | > More info about avg aggregation is in the [official elasticsearch docs][1] 4 | 5 | A single-value metrics aggregation that computes the average of numeric values that are extracted from the aggregated documents. 6 | 7 | 8 | ## Simple example 9 | 10 | ```JSON 11 | { 12 | "aggregations": { 13 | "avg_grade": { 14 | "avg": { 15 | "field": "grade" 16 | } 17 | } 18 | } 19 | } 20 | ``` 21 | 22 | And now the query via DSL: 23 | 24 | ```php 25 | $avgAggregation = new AvgAggregation('avg_grade'); 26 | $avgAggregation->setField('grade'); 27 | 28 | $search = new Search(); 29 | $search->addAggregation($avgAggregation); 30 | 31 | $queryArray = $search->toArray(); 32 | ``` 33 | 34 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-avg-aggregation.html 35 | -------------------------------------------------------------------------------- /docs/Aggregation/Metric/Cardinality.md: -------------------------------------------------------------------------------- 1 | # Cardinality Aggregation 2 | 3 | > More info about cardinality aggregation is in the [official elasticsearch docs][1] 4 | 5 | A single-value metrics aggregation that calculates an approximate count of distinct values. 6 | 7 | ## Simple example 8 | 9 | ```JSON 10 | { 11 | "aggregations" : { 12 | "author_count" : { 13 | "cardinality" : { 14 | "field" : "author" 15 | } 16 | } 17 | } 18 | } 19 | ``` 20 | 21 | And now the query via DSL: 22 | 23 | ```php 24 | $cardinalityAggregation = new CardinalityAggregation('author_count'); 25 | $cardinalityAggregation->setField('author'); 26 | 27 | $search = new Search(); 28 | $search->addAggregation($cardinalityAggregation); 29 | 30 | $queryArray = $search->toArray(); 31 | ``` 32 | 33 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-cardinality-aggregation.html 34 | -------------------------------------------------------------------------------- /docs/Aggregation/Metric/ExtendedStats.md: -------------------------------------------------------------------------------- 1 | # Extended Stats Aggregation 2 | 3 | > More info about extended stats aggregation is in the [official elasticsearch docs][1] 4 | 5 | A multi-value metrics aggregation that computes stats over numeric values extracted from the aggregated documents. 6 | 7 | ## Simple example 8 | 9 | ```JSON 10 | { 11 | "aggregations" : { 12 | "grades_stats" : { 13 | "extended_stats" : { "field" : "grade" } 14 | } 15 | } 16 | } 17 | ``` 18 | 19 | And now the query via DSL: 20 | 21 | ```php 22 | $extendedStatsAggregation = new ExtendedStatsAggregation('grades_stats', 'grade'); 23 | 24 | $search = new Search(); 25 | $search->addAggregation($extendedStatsAggregation); 26 | 27 | $queryArray = $search->toArray(); 28 | ``` 29 | 30 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-extendedstats-aggregation.html 31 | -------------------------------------------------------------------------------- /docs/Aggregation/Metric/GeoBounds.md: -------------------------------------------------------------------------------- 1 | # Geo Bounds Aggregation 2 | 3 | > More info about geo bounds aggregation is in the [official elasticsearch docs][1] 4 | 5 | A metric aggregation that computes the bounding box containing all geo_point values for a field. 6 | 7 | ## Simple example 8 | 9 | ```JSON 10 | { 11 | "aggregations" : { 12 | "viewport" : { 13 | "geo_bounds" : { 14 | "field" : "location", 15 | "wrap_longitude" : true 16 | } 17 | } 18 | } 19 | } 20 | ``` 21 | 22 | And now the query via DSL: 23 | 24 | ```php 25 | $geoBoundsAggregation = new GeoBoundsAggregation('viewport', 'location'); 26 | 27 | $search = new Search(); 28 | $search->addAggregation($geoBoundsAggregation); 29 | 30 | $queryArray = $search->toArray(); 31 | ``` 32 | 33 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-geobounds-aggregation.html 34 | -------------------------------------------------------------------------------- /docs/Aggregation/Metric/Max.md: -------------------------------------------------------------------------------- 1 | # Max Aggregation 2 | 3 | > More info about max aggregation is in the [official elasticsearch docs][1] 4 | 5 | A single-value metrics aggregation that keeps track and returns the 6 | maximum value among the numeric values extracted from the aggregated documents. 7 | 8 | ## Simple example 9 | 10 | ```JSON 11 | { 12 | "aggregations" : { 13 | "max_price" : { "max" : { "field" : "price" } } 14 | } 15 | } 16 | ``` 17 | 18 | And now the query via DSL: 19 | 20 | ```php 21 | $maxAggregation = new MaxAggregation('max_price', 'price'); 22 | 23 | $search = new Search(); 24 | $search->addAggregation($maxAggregation); 25 | 26 | $queryArray = $search->toArray(); 27 | ``` 28 | 29 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-max-aggregation.html 30 | -------------------------------------------------------------------------------- /docs/Aggregation/Metric/Min.md: -------------------------------------------------------------------------------- 1 | # Min Aggregation 2 | 3 | > More info about min aggregation is in the [official elasticsearch docs][1] 4 | 5 | A single-value metrics aggregation that keeps track and returns the minimum value among 6 | numeric values extracted from the aggregated documents. 7 | 8 | ## Simple example 9 | 10 | ```JSON 11 | { 12 | "aggregations" : { 13 | "min_price" : { "min" : { "field" : "price" } } 14 | } 15 | } 16 | ``` 17 | 18 | And now the query via DSL: 19 | 20 | ```php 21 | $minAggregation = new MinAggregation('min_price', 'price'); 22 | 23 | $search = new Search(); 24 | $search->addAggregation($minAggregation); 25 | 26 | $queryArray = $search->toArray(); 27 | ``` 28 | 29 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-min-aggregation.html 30 | -------------------------------------------------------------------------------- /docs/Aggregation/Metric/PercentileRanks.md: -------------------------------------------------------------------------------- 1 | # Percentile Ranks Aggregation 2 | 3 | > More info about percentile ranks aggregation is in the [official elasticsearch docs][1] 4 | 5 | A multi-value metrics aggregation that calculates one or more percentile 6 | ranks over numeric values extracted from the aggregated documents. 7 | 8 | ## Simple example 9 | 10 | ```JSON 11 | { 12 | "aggregations" : { 13 | "load_time_outlier" : { 14 | "percentile_ranks" : { 15 | "field" : "load_time", 16 | "values" : [15, 30] 17 | } 18 | } 19 | } 20 | } 21 | ``` 22 | 23 | And now the query via DSL: 24 | 25 | ```php 26 | $percentileRanksAggregation = new PercentileRanksAggregation('load_time_outlier', 'load_time', [15, 30]); 27 | 28 | $search = new Search(); 29 | $search->addAggregation($percentileRanksAggregation); 30 | 31 | $queryArray = $search->toArray(); 32 | ``` 33 | 34 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-percentile-rank-aggregation.html 35 | -------------------------------------------------------------------------------- /docs/Aggregation/Metric/Percentiles.md: -------------------------------------------------------------------------------- 1 | # Percentiles Aggregation 2 | 3 | > More info about percentiles aggregation is in the [official elasticsearch docs][1] 4 | 5 | A multi-value metrics aggregation that calculates one or more percentiles over 6 | numeric values extracted from the aggregated documents. 7 | 8 | ## Simple example 9 | 10 | ```JSON 11 | { 12 | "aggregations" : { 13 | "load_time_outlier" : { 14 | "percentiles" : { 15 | "field" : "load_time" 16 | } 17 | } 18 | } 19 | } 20 | ``` 21 | 22 | And now the query via DSL: 23 | 24 | ```php 25 | $percentilesAggregation = new PercentilesAggregation('load_time_outlier', 'load_time'); 26 | 27 | $search = new Search(); 28 | $search->addAggregation($percentilesAggregation); 29 | 30 | $queryArray = $search->toArray(); 31 | ``` 32 | 33 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-cardinality-aggregation.html 34 | -------------------------------------------------------------------------------- /docs/Aggregation/Metric/ScriptedMetric.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ongr-io/ElasticsearchDSL/b4c3c8982bca497661139a1c2967a81f8f9bb9b6/docs/Aggregation/Metric/ScriptedMetric.md -------------------------------------------------------------------------------- /docs/Aggregation/Metric/Stats.md: -------------------------------------------------------------------------------- 1 | # Stats Aggregation 2 | 3 | > More info about stats aggregation is in the [official elasticsearch docs][1] 4 | 5 | A multi-value metrics aggregation that computes stats over numeric 6 | values extracted from the aggregated documents. 7 | 8 | ## Simple example 9 | 10 | ```JSON 11 | { 12 | "aggregations" : { 13 | "grades_stats" : { "stats" : { "field" : "grade" } } 14 | } 15 | } 16 | ``` 17 | 18 | And now the query via DSL: 19 | 20 | ```php 21 | $statsAggregation = new StatsAggregation('grades_stats', 'grade'); 22 | 23 | $search = new Search(); 24 | $search->addAggregation($statsAggregation); 25 | 26 | $queryArray = $search->toArray(); 27 | ``` 28 | 29 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-stats-aggregation.html 30 | -------------------------------------------------------------------------------- /docs/Aggregation/Metric/Sum.md: -------------------------------------------------------------------------------- 1 | # Sum Aggregation 2 | 3 | > More info about sum aggregation is in the [official elasticsearch docs][1] 4 | 5 | A single-value metrics aggregation that sums up numeric values that are extracted from the aggregated documents. 6 | 7 | ## Simple example 8 | 9 | ```JSON 10 | { 11 | "aggregations" : { 12 | "intraday_return" : { "sum" : { "field" : "change" } } 13 | } 14 | } 15 | ``` 16 | 17 | And now the query via DSL: 18 | 19 | ```php 20 | $sumAggregation = new SumAggregation('intraday_return', 'change'); 21 | 22 | $search = new Search(); 23 | $search->addAggregation($sumAggregation); 24 | 25 | $queryArray = $search->toArray(); 26 | ``` 27 | 28 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-sum-aggregation.html 29 | -------------------------------------------------------------------------------- /docs/Aggregation/Metric/ValueCount.md: -------------------------------------------------------------------------------- 1 | # Value Count Aggregation 2 | 3 | > More info about value count aggregation is in the [official elasticsearch docs][1] 4 | 5 | A single-value metrics aggregation that counts the number of values that are extracted from the aggregated documents. 6 | 7 | ## Simple example 8 | 9 | ```JSON 10 | { 11 | "aggregations" : { 12 | "grades_count" : { "value_count" : { "field" : "grade" } } 13 | } 14 | } 15 | ``` 16 | 17 | And now the query via DSL: 18 | 19 | ```php 20 | $valueCountAggregation = new ValueCountAggregation('grades_count', 'grade'); 21 | 22 | $search = new Search(); 23 | $search->addAggregation($valueCountAggregation); 24 | 25 | $queryArray = $search->toArray(); 26 | ``` 27 | 28 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-valuecount-aggregation.html 29 | -------------------------------------------------------------------------------- /docs/Aggregation/Pipeline/AvgBucket.md: -------------------------------------------------------------------------------- 1 | # Avg Bucket Aggregation 2 | 3 | > More info about avg bucket aggregation is in the [official elasticsearch docs][1] 4 | 5 | A sibling pipeline aggregation which calculates the (mean) average value of a specified metric in a 6 | sibling aggregation. The specified metric must be numeric and the sibling aggregation must be a multi-bucket 7 | aggregation. 8 | 9 | ## Simple example 10 | 11 | ```JSON 12 | { 13 | "aggs" : { 14 | "sales_per_month" : { 15 | "date_histogram" : { 16 | "field" : "date", 17 | "interval" : "month" 18 | }, 19 | "aggs": { 20 | "sales": { 21 | "sum": { 22 | "field": "price" 23 | } 24 | } 25 | } 26 | }, 27 | "avg_monthly_sales": { 28 | "avg_bucket": { 29 | "buckets_path": "sales_per_month>sales" 30 | } 31 | } 32 | } 33 | } 34 | ``` 35 | 36 | And now the query via DSL: 37 | 38 | ```php 39 | $search = new Search(); 40 | 41 | $dateAggregation = new DateHistogramAggregation('sales_per_month', 'date', 'month'); 42 | $dateAggregation->addAggregation( 43 | new SumAggregation('sales', 'price') 44 | ); 45 | 46 | $search->addAggregation($dateAggregation); 47 | $search->addAggregation( 48 | new AvgBucketAggregation('avg_monthly_sales', 'sales_per_month>sales') 49 | ); 50 | 51 | $aggArray = $search->toArray(); 52 | ``` 53 | 54 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-avg-bucket-aggregation.html 55 | -------------------------------------------------------------------------------- /docs/Aggregation/Pipeline/CumulativeSum.md: -------------------------------------------------------------------------------- 1 | # Cumulative Sum Aggregation 2 | 3 | > More info about cumulative sum aggregation is in the [official elasticsearch docs][1] 4 | 5 | A parent pipeline aggregation which calculates the cumulative sum of a specified metric 6 | in a parent histogram (or date_histogram) aggregation. The specified metric must be numeric 7 | and the enclosing histogram must have min_doc_count set to 0 (default for histogram 8 | aggregations). 9 | 10 | ## Simple example 11 | 12 | ```JSON 13 | { 14 | "aggs" : { 15 | "sales_per_month" : { 16 | "date_histogram" : { 17 | "field" : "date", 18 | "interval" : "month" 19 | }, 20 | "aggs": { 21 | "sales": { 22 | "sum": { 23 | "field": "price" 24 | } 25 | }, 26 | "cumulative_sales": { 27 | "cumulative_sum": { 28 | "buckets_path": "sales" 29 | } 30 | } 31 | } 32 | } 33 | } 34 | } 35 | ``` 36 | 37 | And now the query via DSL: 38 | 39 | ```php 40 | $search = new Search(); 41 | 42 | $dateAggregation = new DateHistogramAggregation('sales_per_month', 'date', 'month'); 43 | $dateAggregation->addAggregation( 44 | new SumAggregation('sales', 'price') 45 | ); 46 | $dateAggregation->addAggregation( 47 | new CumulativeSumAggregation('cumulative_sales', 'sales') 48 | ); 49 | 50 | $search->addAggregation($dateAggregation); 51 | 52 | $aggArray = $search->toArray(); 53 | ``` 54 | 55 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-cumulative-sum-aggregation.html -------------------------------------------------------------------------------- /docs/Aggregation/Pipeline/ExtendedStatsBucket.md: -------------------------------------------------------------------------------- 1 | # Extended Stats Bucket Aggregation 2 | 3 | > More info about extended stats bucket aggregation is in the [official elasticsearch docs][1] 4 | 5 | This aggregation provides a few more statistics (sum of squares, standard deviation, etc) compared to the stats_bucket` aggregation. 6 | 7 | ## Simple example 8 | 9 | ```JSON 10 | { 11 | "aggs" : { 12 | "sales_per_month" : { 13 | "date_histogram" : { 14 | "field" : "date", 15 | "interval" : "month" 16 | }, 17 | "aggs": { 18 | "sales": { 19 | "sum": { 20 | "field": "price" 21 | } 22 | } 23 | } 24 | }, 25 | "stats_monthly_sales": { 26 | "extended_stats_bucket": { 27 | "buckets_paths": "sales_per_month>sales" 28 | } 29 | } 30 | } 31 | } 32 | ``` 33 | 34 | And now the query via DSL: 35 | 36 | ```php 37 | $search = new Search(); 38 | 39 | $dateAggregation = new DateHistogramAggregation('sales_per_month', 'date', 'month'); 40 | $dateAggregation->addAggregation( 41 | new SumAggregation('sales', 'price') 42 | ); 43 | 44 | $search->addAggregation($dateAggregation); 45 | $search->addAggregation( 46 | new ExtendedStatsBucketAggregation('stats_monthly_sales', 'sales_per_month>sales') 47 | ); 48 | 49 | $aggArray = $search->toArray(); 50 | ``` 51 | 52 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-extended-stats-bucket-aggregation.html -------------------------------------------------------------------------------- /docs/Aggregation/Pipeline/SerialDifferencing.md: -------------------------------------------------------------------------------- 1 | # Serial Differencing Aggregation 2 | 3 | > More info about serial differencing aggregation is in the [official elasticsearch docs][1] 4 | 5 | Serial differencing is a technique where values in a time series are subtracted from itself at 6 | different time lags or periods 7 | 8 | ## Simple example 9 | 10 | ```JSON 11 | { 12 | "aggs": { 13 | "my_date_histo": { 14 | "date_histogram": { 15 | "field": "timestamp", 16 | "interval": "day" 17 | }, 18 | "aggs": { 19 | "the_sum": { 20 | "sum": { 21 | "field": "lemmings" 22 | } 23 | }, 24 | "thirtieth_difference": { 25 | "serial_diff": { 26 | "buckets_path": "the_sum", 27 | "lag" : 30 28 | } 29 | } 30 | } 31 | } 32 | } 33 | } 34 | ``` 35 | 36 | And now the query via DSL: 37 | 38 | ```php 39 | $search = new Search(); 40 | 41 | $dateAggregation = new DateHistogramAggregation('my_date_histo', 'timestamp', 'day'); 42 | $dateAggregation->addAggregation( 43 | new SumAggregation('the_sum', 'lemmings') 44 | ); 45 | $diffAggregation = new SerialDifferencingAggregation('thirtieth_difference', 'the_sum'); 46 | $diffAggregation->addParameter('lag', 30); 47 | $dateAggregation->addAggregation($diffAggregation); 48 | 49 | $search->addAggregation($dateAggregation); 50 | 51 | $aggArray = $search->toArray(); 52 | ``` 53 | 54 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-serialdiff-aggregation.html -------------------------------------------------------------------------------- /docs/Aggregation/Pipeline/StatsBucket.md: -------------------------------------------------------------------------------- 1 | # Stats Aggregation 2 | 3 | > More info about stats bucket aggregation is in the [official elasticsearch docs][1] 4 | 5 | A sibling pipeline aggregation which calculates a variety of stats across all bucket of 6 | a specified metric in a sibling aggregation. The specified metric must be numeric and the 7 | sibling aggregation must be a multi-bucket aggregation. 8 | 9 | ## Simple example 10 | 11 | ```JSON 12 | { 13 | "aggs" : { 14 | "sales_per_month" : { 15 | "date_histogram" : { 16 | "field" : "date", 17 | "interval" : "month" 18 | }, 19 | "aggs": { 20 | "sales": { 21 | "sum": { 22 | "field": "price" 23 | } 24 | } 25 | } 26 | }, 27 | "stats_monthly_sales": { 28 | "stats_bucket": { 29 | "buckets_path": "sales_per_month>sales" 30 | } 31 | } 32 | } 33 | } 34 | ``` 35 | 36 | And now the query via DSL: 37 | 38 | ```php 39 | $search = new Search(); 40 | 41 | $dateAggregation = new DateHistogramAggregation('sales_per_month', 'date', 'month'); 42 | $dateAggregation->addAggregation( 43 | new SumAggregation('sales', 'price') 44 | ); 45 | 46 | $search->addAggregation($dateAggregation); 47 | $search->addAggregation( 48 | new StatsBucketAggregation('stats_monthly_sales', 'sales_per_month>sales') 49 | ); 50 | 51 | $aggArray = $search->toArray(); 52 | ``` 53 | 54 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-stats-bucket-aggregation.html -------------------------------------------------------------------------------- /docs/Aggregation/Pipeline/SumBucket.md: -------------------------------------------------------------------------------- 1 | # Sum Bucket Aggregation 2 | 3 | > More info about sum bucket aggregation is in the [official elasticsearch docs][1] 4 | 5 | A sibling pipeline aggregation which calculates the sum across all bucket of a 6 | specified metric in a sibling aggregation. The specified metric must be numeric and 7 | the sibling aggregation must be a multi-bucket aggregation. 8 | 9 | ## Simple example 10 | 11 | ```JSON 12 | { 13 | "aggs" : { 14 | "sales_per_month" : { 15 | "date_histogram" : { 16 | "field" : "date", 17 | "interval" : "month" 18 | }, 19 | "aggs": { 20 | "sales": { 21 | "sum": { 22 | "field": "price" 23 | } 24 | } 25 | } 26 | }, 27 | "sum_monthly_sales": { 28 | "sum_bucket": { 29 | "buckets_path": "sales_per_month>sales" 30 | } 31 | } 32 | } 33 | } 34 | ``` 35 | 36 | And now the query via DSL: 37 | 38 | ```php 39 | $dateAggregation = new DateHistogramAggregation('sales_per_month', 'date', 'month'); 40 | $dateAggregation->addAggregation(new SumAggregation('sales', 'price')); 41 | $sumBucketAggregation = new SumBucketAggregation( 42 | 'sum_monthly_sales', 43 | 'sales_per_month>sales' 44 | ) 45 | 46 | $search = new Search(); 47 | $search->addAggregation($dateAggregation); 48 | $search->addAggregation($sumBucketAggregation); 49 | 50 | $queryArray = $search->toArray(); 51 | ``` 52 | 53 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-sum-bucket-aggregation.html 54 | -------------------------------------------------------------------------------- /docs/InnerHits/Parent.md: -------------------------------------------------------------------------------- 1 | # Parent Inner Hits 2 | 3 | > More info about inner hits is in the [official elasticsearch docs][1] 4 | 5 | The `parent/child` inner_hits can be used to include parent or child. 6 | The usage of parent inner hits is very similar to that of nested [inner hits](Nested.md), the only 7 | difference is that in stead of passing the `path` to the nested object, the parent/child `type` 8 | needs to be passed to the `$path` variable. 9 | 10 | ## Simple example 11 | 12 | ```JSON 13 | { 14 | "inner_hits" : { 15 | "children" : { 16 | "type" : { 17 | "article" : { 18 | "query" : { 19 | "match" : {"title" : "[actual query]"} 20 | } 21 | } 22 | } 23 | } 24 | } 25 | } 26 | ``` 27 | 28 | And now the query via DSL: 29 | 30 | ```php 31 | $matchQuery = new MatchQuery('title', '[actual query]'); 32 | $innerHit = new ParentInnerHit('children', 'article', $matchQuery); 33 | 34 | $search = new Search(); 35 | $search->addInnerHit($innerHit); 36 | $search->toArray(); 37 | ``` 38 | 39 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-inner-hits.html 40 | -------------------------------------------------------------------------------- /docs/Query/Compound/Boosting.md: -------------------------------------------------------------------------------- 1 | # Boosting query 2 | 3 | > More info about Boosting query is in the [official elasticsearch docs][1] 4 | 5 | Lets take an example to write a query with Elasticsearch DSL. 6 | 7 | ```JSON 8 | { 9 | "boosting" : { 10 | "positive" : { 11 | "term" : { 12 | "field1" : "value1" 13 | } 14 | }, 15 | "negative" : { 16 | "term" : { 17 | "field2" : "value2" 18 | } 19 | }, 20 | "negative_boost" : 0.2 21 | } 22 | } 23 | ``` 24 | 25 | And now the query via DSL: 26 | 27 | ```php 28 | $termQuery1 = new TermQuery("field1", "value1"); 29 | $termQuery2 = new TermQuery("field2", "value2"); 30 | 31 | $boostingQuery = new BoostingQuery($termQuery1, $termQuery2, 0.2); 32 | 33 | $search = new Search(); 34 | $search->addQuery($boostingQuery); 35 | 36 | $queryArray = $search->toArray(); 37 | ``` 38 | 39 | 40 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-boosting-query.html 41 | -------------------------------------------------------------------------------- /docs/Query/Compound/ConstantScore.md: -------------------------------------------------------------------------------- 1 | # Constant score query 2 | 3 | > More info about Constant score query is in the [official elasticsearch docs][1] 4 | 5 | Inside constant score query you can insert filter or query. 6 | 7 | Lets take an example to write a constant score query with filter inside. 8 | 9 | ```JSON 10 | { 11 | "constant_score" : { 12 | "filter" : { 13 | "term" : { "user" : "kimchy"} 14 | }, 15 | "boost" : 1.2 16 | } 17 | } 18 | ``` 19 | 20 | And now the query via DSL: 21 | 22 | ```php 23 | $termFilter = new TermQuery("user", "kimchy"); 24 | $constantScoreQuery = new ConstantScoreQuery($termFilter, ["boost" => 1.2]); 25 | 26 | $search = new Search(); 27 | $search->addQuery($constantScoreQuery); 28 | 29 | $queryArray = $search->toArray(); 30 | ``` 31 | 32 | To form a query with query inside is very easy, just add a query in `ConstantScoreQuery` constructor instead of filter. 33 | 34 | ```JSON 35 | { 36 | "constant_score" : { 37 | "query" : { 38 | "term" : { "user" : "kimchy"} 39 | }, 40 | "boost" : 1.2 41 | } 42 | } 43 | ``` 44 | 45 | via DSL: 46 | 47 | ```php 48 | $termQuery = new TermQuery("user", "kimchy"); 49 | $constantScoreQuery = new ConstantScoreQuery($termQuery, ["boost" => 1.2]); 50 | 51 | $search = new Search(); 52 | $search->addQuery($constantScoreQuery); 53 | 54 | $queryArray = $search->toArray(); 55 | ``` 56 | 57 | 58 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-constant-score-query.html 59 | -------------------------------------------------------------------------------- /docs/Query/Compound/Indices.md: -------------------------------------------------------------------------------- 1 | # Indices Query 2 | 3 | > More info about indices query is in the [official elasticsearch docs][1] 4 | 5 | The indices query can be used when executed across multiple indices, allowing to have a query that executes 6 | only when executed on an index that matches a specific list of indices, and another query that executes 7 | when it is executed on an index that does not match the listed indices. 8 | 9 | ## Simple example 10 | 11 | ```JSON 12 | { 13 | "indices" : { 14 | "indices" : ["index1", "index2"], 15 | "query" : { 16 | "term" : { "tag" : "wow" } 17 | }, 18 | "no_match_query" : { 19 | "term" : { "tag" : "kow" } 20 | } 21 | } 22 | } 23 | ``` 24 | 25 | In DSL: 26 | 27 | ```php 28 | $matchTermQuery = new TermQuery('tag', 'wow'); 29 | $noMatchTermQuery = new TermQuery('tag', 'kow'); 30 | 31 | $indicesQuery = new IndicesQuery(['index1', 'index2'], $matchTermQuery, $noMatchTermQuery); 32 | 33 | $search = new Search(); 34 | $search->addQuery($indicesQuery); 35 | 36 | $queryArray = $search->toArray(); 37 | ``` 38 | 39 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-indices-query.html 40 | -------------------------------------------------------------------------------- /docs/Query/FullText/CommonTerms.md: -------------------------------------------------------------------------------- 1 | # Common terms query 2 | 3 | > More info about Common terms query is in the [official elasticsearch docs][1] 4 | 5 | There are so many use cases with `Common Terms` query. We highly recommend to take a look at the [official docs][1] before continuing. 6 | 7 | Lets take first example to write easy `Common query` with Elasticsearch DSL. 8 | 9 | ```JSON 10 | { 11 | "common": { 12 | "name": { 13 | "query": "this is bonsai cool", 14 | "cutoff_frequency": 0.001, 15 | "minimum_should_match": { 16 | "low_freq" : 2, 17 | "high_freq" : 3 18 | } 19 | } 20 | } 21 | } 22 | ``` 23 | 24 | And now the query via DSL: 25 | 26 | ```php 27 | $commonTermsQuery = new CommonTermsQuery( 28 | "field_name", 29 | "this is bonsai cool", 30 | [ 31 | "cutoff_frequency" => 0.001, 32 | "minimum_should_match" => [ 33 | "low_freq" => 2, 34 | "high_freq" => 3, 35 | ], 36 | ] 37 | ); 38 | 39 | $search = new Search(); 40 | $search->addQuery($commonTermsQuery); 41 | 42 | $queryArray = $search->toArray(); 43 | ``` 44 | 45 | 46 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-common-terms-query.html 47 | -------------------------------------------------------------------------------- /docs/Query/FullText/Match.md: -------------------------------------------------------------------------------- 1 | # Match Query 2 | 3 | > More info about match query is in the [official elasticsearch docs][1] 4 | 5 | A family of match queries that accept text/numerics/dates, analyzes it, and constructs a query out of it. 6 | 7 | ## Simple example 8 | 9 | ```JSON 10 | { 11 | "match" : { 12 | "message" : "this is a test" 13 | } 14 | } 15 | ``` 16 | 17 | In DSL: 18 | 19 | ```php 20 | $matchQuery = new MatchQuery('message', 'this is a test'); 21 | 22 | $search = new Search(); 23 | $search->addQuery($matchQuery); 24 | 25 | $queryArray = $search->toArray(); 26 | ``` 27 | 28 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html 29 | -------------------------------------------------------------------------------- /docs/Query/FullText/MatchPhrase.md: -------------------------------------------------------------------------------- 1 | # MatchPhrase Query 2 | 3 | > More info about match phrase query is in the [official elasticsearch docs][1] 4 | 5 | The match_phrase query analyzes the text and creates a phrase query out of the analyzed text. For example: 6 | 7 | ## Simple example 8 | 9 | ```JSON 10 | { 11 | "query": { 12 | "match_phrase" : { 13 | "message" : { 14 | "query" : "this is a test", 15 | "analyzer" : "my_analyzer" 16 | } 17 | } 18 | } 19 | } 20 | ``` 21 | 22 | In DSL: 23 | 24 | ```php 25 | $query = new MatchPhraseQuery('message', 'this is a test'); 26 | $query->addParameter('analyzer', 'my_analyzer'); 27 | 28 | $search = new Search(); 29 | $search->addQuery($query); 30 | 31 | $queryArray = $search->toArray(); 32 | ``` 33 | 34 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase.html 35 | -------------------------------------------------------------------------------- /docs/Query/FullText/MatchPhrasePrefix.md: -------------------------------------------------------------------------------- 1 | # Match Phrase Prefix Query 2 | 3 | > More info about match phrase prefix query is in the [official elasticsearch docs][1] 4 | 5 | The `match_phrase_prefix` is the same as `match_phrase`, except that it allows for prefix matches on the last term in the text. For example 6 | 7 | ## Simple example 8 | 9 | ```JSON 10 | { 11 | "query": { 12 | "match_phrase_prefix" : { 13 | "message" : { 14 | "query" : "quick brown f", 15 | "max_expansions" : 10 16 | } 17 | } 18 | } 19 | } 20 | ``` 21 | 22 | In DSL: 23 | 24 | ```php 25 | $query = new MatchPhrasePrefixQuery('message', 'quick brown f'); 26 | $query->addParameter('max_expansions', 10); 27 | 28 | $search = new Search(); 29 | $search->addQuery($query); 30 | 31 | $queryArray = $search->toArray(); 32 | ``` 33 | 34 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase-prefix.html 35 | -------------------------------------------------------------------------------- /docs/Query/FullText/MultiMatch.md: -------------------------------------------------------------------------------- 1 | # Multi Match Query 2 | 3 | > More info about multi match query is in the [official elasticsearch docs][1] 4 | 5 | The multi match query builds on the [match][2] query to allow multi-field queries: 6 | 7 | ## Simple example 8 | 9 | ```JSON 10 | { 11 | "multi_match" : { 12 | "query": "this is a test", 13 | "fields": [ "subject", "message" ] 14 | } 15 | } 16 | ``` 17 | 18 | In DSL: 19 | 20 | ```php 21 | $multiMatchQuery = new MultiMatchQuery( 22 | ['subject', 'message'], 23 | 'this is a test' 24 | ); 25 | 26 | $search = new Search(); 27 | $search->addQuery($multiMatchQuery); 28 | 29 | $queryArray = $search->toArray(); 30 | ``` 31 | 32 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html 33 | [2]: Match.md 34 | -------------------------------------------------------------------------------- /docs/Query/FullText/QueryString.md: -------------------------------------------------------------------------------- 1 | # Query String Query 2 | 3 | > More info about query string query is in the [official elasticsearch docs][1] 4 | 5 | A query that uses a query parser in order to parse its content. 6 | 7 | ## Simple example 8 | 9 | ```JSON 10 | { 11 | "query_string" : { 12 | "default_field" : "content", 13 | "query" : "this AND that OR thus" 14 | } 15 | } 16 | ``` 17 | 18 | In DSL: 19 | 20 | ```php 21 | $queryStringQuery = new QueryStringQuery('this AND that OR thus'); 22 | $queryStringQuery->addParameter('default_field', 'content'); 23 | 24 | $search = new Search(); 25 | $search->addQuery($queryStringQuery); 26 | 27 | $queryArray = $search->toArray(); 28 | ``` 29 | 30 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html 31 | -------------------------------------------------------------------------------- /docs/Query/FullText/SimpleQueryString.md: -------------------------------------------------------------------------------- 1 | # Simple Query String Query 2 | 3 | > More info about simple query string query is in the [official elasticsearch docs][1] 4 | 5 | A query that uses the SimpleQueryParser to parse its context 6 | 7 | ## Simple example 8 | 9 | ```JSON 10 | { 11 | "simple_query_string" : { 12 | "query": "\"fried eggs\" +(eggplant | potato) -frittata", 13 | "analyzer": "snowball", 14 | "fields": ["body^5","_all"], 15 | "default_operator": "and" 16 | } 17 | } 18 | ``` 19 | 20 | In DSL: 21 | 22 | ```php 23 | $simpleQueryStringQuery = new SimpleQueryStringQuery( 24 | '"fried eggs" +(eggplant | potato) -frittata', 25 | [ 26 | 'analyzer' => 'snowball', 27 | 'fields' => ['body^5', '_all'], 28 | 'default_operator' => 'and', 29 | ] 30 | ); 31 | 32 | $search = new Search(); 33 | $search->addQuery($simpleQueryStringQuery); 34 | 35 | $queryArray = $search->toArray(); 36 | ``` 37 | 38 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-simple-query-string-query.html 39 | -------------------------------------------------------------------------------- /docs/Query/Geo/GeoDistanceRange.md: -------------------------------------------------------------------------------- 1 | # Geo Distance Range Query 2 | 3 | > More info about geo distance range query is in the [official elasticsearch docs][1] 4 | 5 | > This query was deprecated as of elasticsearch 5.0. Distance aggregations or sorting should be used instead. 6 | 7 | Filters documents that exists within a range from a specific point: 8 | 9 | ## Simple example 10 | 11 | ```JSON 12 | { 13 | "query": { 14 | "bool" : { 15 | "must" : { 16 | "match_all" : {} 17 | }, 18 | "filter" : { 19 | "geo_distance_range" : { 20 | "from" : "200km", 21 | "to" : "400km", 22 | "pin.location" : { 23 | "lat" : 40, 24 | "lon" : -70 25 | } 26 | } 27 | } 28 | } 29 | } 30 | } 31 | ``` 32 | 33 | In DSL: 34 | 35 | ```php 36 | $search = new Search(); 37 | $boolQuery = new BoolQuery(); 38 | $boolQuery->add(new MatchAllQuery()); 39 | $geoQuery = new GeoDistanceQuery( 40 | 'pin.location', 41 | ['from' => '200km', 'to' => '400km'], 42 | ['lat' => 40, 'lon' => -70] 43 | ); 44 | $boolQuery->add($geoQuery, BoolQuery::FILTER); 45 | $search->addQuery($boolQuery); 46 | 47 | $queryArray = $search->toArray(); 48 | ``` 49 | 50 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-distance-range-query.html 51 | -------------------------------------------------------------------------------- /docs/Query/Geo/GeoPolygon.md: -------------------------------------------------------------------------------- 1 | # Geo Polygon Query 2 | 3 | > More info about geo polygon query is in the [official elasticsearch docs][1] 4 | 5 | A query allowing to include hits that only fall within a polygon of points. Here is an example: 6 | 7 | ## Simple example 8 | 9 | ```JSON 10 | { 11 | "query": { 12 | "bool" : { 13 | "must" : { 14 | "match_all" : {} 15 | }, 16 | "filter" : { 17 | "geo_polygon" : { 18 | "person.location" : { 19 | "points" : [ 20 | {"lat" : 40, "lon" : -70}, 21 | {"lat" : 30, "lon" : -80}, 22 | {"lat" : 20, "lon" : -90} 23 | ] 24 | } 25 | } 26 | } 27 | } 28 | } 29 | } 30 | ``` 31 | 32 | In DSL: 33 | 34 | ```php 35 | $points = [ 36 | ['lat' => 40, 'lon' => -70], 37 | ['lat' => 30, 'lon' => -80], 38 | ['lat' => 20, 'lon' => -90], 39 | ]; 40 | $search = new Search(); 41 | $boolQuery = new BoolQuery(); 42 | $boolQuery->add(new MatchAllQuery()); 43 | $geoQuery = new GeoPolygonQuery('person.location', $points); 44 | $boolQuery->add($geoQuery, BoolQuery::FILTER); 45 | $search->addQuery($boolQuery); 46 | 47 | $queryArray = $search->toArray(); 48 | ``` 49 | 50 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-polygon-query.html 51 | -------------------------------------------------------------------------------- /docs/Query/Geo/GeoShape.md: -------------------------------------------------------------------------------- 1 | Documentation in progress... -------------------------------------------------------------------------------- /docs/Query/Joining/HasChild.md: -------------------------------------------------------------------------------- 1 | # Has child Query 2 | 3 | > More info about has child query is in the [official elasticsearch docs][1] 4 | 5 | The has child query accepts a query and the child type to run against, and results in parent documents that have child 6 | docs matching the query. 7 | 8 | ## Simple example 9 | 10 | ```JSON 11 | { 12 | "has_child" : { 13 | "type" : "blog_tag", 14 | "query" : { 15 | "term" : { 16 | "tag" : "something" 17 | } 18 | } 19 | } 20 | } 21 | ``` 22 | 23 | In DSL: 24 | 25 | ```php 26 | $termQuery = new TermQuery('tag', 'something'); 27 | 28 | $hasChildQuery = new HasChildQuery('blog_tag', $termQuery); 29 | 30 | $search = new Search(); 31 | $search->addQuery($hasChildQuery); 32 | 33 | $queryArray = $search->toArray(); 34 | ``` 35 | 36 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-has-child-query.html 37 | -------------------------------------------------------------------------------- /docs/Query/Joining/HasParent.md: -------------------------------------------------------------------------------- 1 | # Has Parent Query 2 | 3 | > More info about has parent query is in the [official elasticsearch docs][1] 4 | 5 | The has parent query accepts a query and a parent type. The query is executed in the parent document space, which is 6 | specified by the parent type. This filter returns child documents which associated parents have matched. 7 | 8 | ## Simple example 9 | 10 | ```JSON 11 | { 12 | "has_parent" : { 13 | "parent_type" : "blog", 14 | "query" : { 15 | "term" : { 16 | "tag" : "something" 17 | } 18 | } 19 | } 20 | } 21 | ``` 22 | 23 | In DSL: 24 | 25 | ```php 26 | $termQuery = new TermQuery('tag', 'something'); 27 | 28 | $hasParentQuery = new HasParentQuery('blog', $termQuery); 29 | 30 | $search = new Search(); 31 | $search->addQuery($hasParentQuery); 32 | 33 | $queryArray = $search->toArray(); 34 | ``` 35 | 36 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-has-parent-query.html 37 | -------------------------------------------------------------------------------- /docs/Query/Joining/Nested.md: -------------------------------------------------------------------------------- 1 | # Nested Query 2 | 3 | > More info about nested query is in the [official elasticsearch docs][1] 4 | 5 | Nested query allows to query nested objects / documents (see [nested mapping][2]). The query is executed against the nested 6 | objects / documents as if they were indexed as separate documents (they are, internally) and resulting in the root 7 | parent document (or parent nested mapping). 8 | 9 | ## Simple example 10 | 11 | ```JSON 12 | { 13 | "nested" : { 14 | "path" : "obj1", 15 | "score_mode" : "avg", 16 | "query" : { 17 | "bool" : { 18 | "must" : [ 19 | { 20 | "match" : {"obj1.name" : "blue"} 21 | }, 22 | { 23 | "range" : {"obj1.count" : {"gt" : 5}} 24 | } 25 | ] 26 | } 27 | } 28 | } 29 | } 30 | ``` 31 | 32 | In DSL: 33 | 34 | ```php 35 | $matchQuery = new MatchQuery('obj1.name', 'blue'); 36 | $rangeQuery = new RangeQuery('obj1.count', ['gt' => 5]); 37 | $boolQuery = new BoolQuery(); 38 | $boolQuery->add($matchQuery); 39 | $boolQuery->add($rangeQuery); 40 | 41 | $nestedQuery = new NestedQuery( 42 | 'obj1', 43 | $boolQuery 44 | ); 45 | $nestedQuery->addParameter('score_mode', 'avg'); 46 | 47 | $search = new Search(); 48 | $search->addQuery($nestedQuery); 49 | 50 | $queryArray = $search->toArray(); 51 | ``` 52 | 53 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html 54 | [2]: https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-nested-type.html 55 | -------------------------------------------------------------------------------- /docs/Query/MatchAll.md: -------------------------------------------------------------------------------- 1 | # Match All Query 2 | 3 | > More info about match all query is in the [official elasticsearch docs][1] 4 | 5 | A query that matches all documents 6 | 7 | ## Simple example 8 | 9 | ```JSON 10 | { 11 | "match_all" : { } 12 | } 13 | ``` 14 | 15 | In DSL: 16 | 17 | ```php 18 | $matchAllQuery = new MatchAllQuery(); 19 | 20 | $search = new Search(); 21 | $search->addQuery($matchAllQuery); 22 | 23 | $queryArray = $search->toArray(); 24 | ``` 25 | 26 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-all-query.html 27 | -------------------------------------------------------------------------------- /docs/Query/Span/SpanContaining.md: -------------------------------------------------------------------------------- 1 | # Span Containing query 2 | 3 | > More info about Boosting query is in the [official elasticsearch docs][1] 4 | 5 | Returns matches which enclose another span query. 6 | 7 | ```JSON 8 | { 9 | "span_containing" : { 10 | "little" : { 11 | "span_term" : { "field1" : "foo" } 12 | }, 13 | "big" : { 14 | "span_near" : { 15 | "clauses" : [ 16 | { "span_term" : { "field1" : "bar" } }, 17 | { "span_term" : { "field1" : "baz" } } 18 | ], 19 | "slop" : 5, 20 | "in_order" : true 21 | } 22 | } 23 | } 24 | } 25 | ``` 26 | 27 | And now the query via DSL: 28 | 29 | ```php 30 | $spanTermQuery = new SpanTermQuery('field1', 'foo'); 31 | $spanNearQuery = new SpanNearQuery(); 32 | 33 | $spanNearQuery->setSlop(5); 34 | $spanNearQuery->addParameter('in_order', true); 35 | $spanNearQuery->addQuery(new SpanTermQuery('field1', 'bar')); 36 | $spanNearQuery->addQuery(new SpanTermQuery('field1', 'baz')); 37 | 38 | $spanContainingQuery = new SpanContainingQuery( 39 | $spanTermQuery, 40 | $spanNearQuery 41 | ); 42 | ``` 43 | 44 | 45 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-containing-query.html 46 | -------------------------------------------------------------------------------- /docs/Query/Span/SpanFirst.md: -------------------------------------------------------------------------------- 1 | # Span First Query 2 | 3 | > More info about Span First query is in the [official elasticsearch docs][1] 4 | 5 | Matches spans near the beginning of a field. The span first query maps to Lucene SpanFirstQuery. Here is an example: 6 | 7 | ```JSON 8 | { 9 | "query": { 10 | "span_first" : { 11 | "match" : { 12 | "span_term" : { "user" : "kimchy" } 13 | }, 14 | "end" : 3 15 | } 16 | } 17 | } 18 | ``` 19 | 20 | And now the query via DSL: 21 | 22 | ```php 23 | $search = new Search(); 24 | $query = new SpanFirstQuery(new SpanTermQuery('user', 'kimchy'), 3); 25 | $search->addQuery($query); 26 | 27 | $search->toArray(); 28 | ``` 29 | 30 | 31 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-first-query.html 32 | -------------------------------------------------------------------------------- /docs/Query/Span/SpanMultiTerm.md: -------------------------------------------------------------------------------- 1 | # Span Multi Term Query 2 | 3 | > More info about span multi term query is in the [official elasticsearch docs][1] 4 | 5 | The span_multi query allows you to wrap a multi term query (one of wildcard, fuzzy, prefix, range or regexp query) as a span query, so it can be nested. Example: 6 | 7 | ## Simple example 8 | 9 | ```JSON 10 | { 11 | "query": { 12 | "span_multi":{ 13 | "match":{ 14 | "prefix" : { "user" : { "value" : "ki" } } 15 | } 16 | } 17 | } 18 | } 19 | ``` 20 | 21 | In DSL: 22 | 23 | ```php 24 | $search = new Search(); 25 | $query = new PrefixQuery('user', 'ki'); 26 | $spanMultiTermQuery = new SpanMultiTermQuery($query); 27 | $search->addQuery($spanMultiTermQuery); 28 | 29 | $queryArray = $search->toArray(); 30 | ``` 31 | 32 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-multi-term-query.html 33 | -------------------------------------------------------------------------------- /docs/Query/Span/SpanNear.md: -------------------------------------------------------------------------------- 1 | # Span Near Query 2 | 3 | > More info about span near query is in the [official elasticsearch docs][1] 4 | 5 | Matches spans which are near one another. One can specify slop, the maximum number of intervening unmatched positions, as well as whether matches are required to be in-order. The span near query maps to Lucene SpanNearQuery. Here is an example: 6 | 7 | ## Simple example 8 | 9 | ```JSON 10 | { 11 | "query": { 12 | "span_near" : { 13 | "clauses" : [ 14 | { "span_term" : { "field" : "value1" } }, 15 | { "span_term" : { "field" : "value2" } }, 16 | { "span_term" : { "field" : "value3" } } 17 | ], 18 | "slop" : 12, 19 | "in_order" : false 20 | } 21 | } 22 | } 23 | ``` 24 | 25 | In DSL: 26 | 27 | ```php 28 | $search = new Search(); 29 | $spanNearQuery = new SpanNearQuery(); 30 | $spanNearQuery->addQuery(new SpanTermQuery('field', 'value1')); 31 | $spanNearQuery->addQuery(new SpanTermQuery('field', 'value2')); 32 | $spanNearQuery->addQuery(new SpanTermQuery('field', 'value3')); 33 | $spanNearQuery->addParameter('slop', 12); 34 | $spanNearQuery->addParameter('in_order', false); 35 | $search->addQuery($spanNearQuery); 36 | 37 | $queryArray = $search->toArray(); 38 | ``` 39 | 40 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-near-query.html 41 | -------------------------------------------------------------------------------- /docs/Query/Span/SpanNot.md: -------------------------------------------------------------------------------- 1 | Documentation in progress... -------------------------------------------------------------------------------- /docs/Query/Span/SpanOr.md: -------------------------------------------------------------------------------- 1 | Documentation in progress... -------------------------------------------------------------------------------- /docs/Query/Span/SpanTerm.md: -------------------------------------------------------------------------------- 1 | Documentation in progress... -------------------------------------------------------------------------------- /docs/Query/Span/SpanWithin.md: -------------------------------------------------------------------------------- 1 | # Span Within query 2 | 3 | > More info about Boosting query is in the [official elasticsearch docs][1] 4 | 5 | Returns matches which are enclosed inside another span query. 6 | 7 | ```JSON 8 | { 9 | "span_within" : { 10 | "little" : { 11 | "span_term" : { "field1" : "foo" } 12 | }, 13 | "big" : { 14 | "span_near" : { 15 | "clauses" : [ 16 | { "span_term" : { "field1" : "bar" } }, 17 | { "span_term" : { "field1" : "baz" } } 18 | ], 19 | "slop" : 5, 20 | "in_order" : true 21 | } 22 | } 23 | } 24 | } 25 | ``` 26 | 27 | And now the query via DSL: 28 | 29 | ```php 30 | $spanTermQuery = new SpanTermQuery('field1', 'foo'); 31 | $spanNearQuery = new SpanNearQuery(); 32 | 33 | $spanNearQuery->setSlop(5); 34 | $spanNearQuery->addParameter('in_order', true); 35 | $spanNearQuery->addQuery(new SpanTermQuery('field1', 'bar')); 36 | $spanNearQuery->addQuery(new SpanTermQuery('field1', 'baz')); 37 | 38 | $spanWithinQuery = new SpanWithinQuery( 39 | $spanTermQuery, 40 | $spanNearQuery 41 | ); 42 | 43 | $search = new Search(); 44 | $search->addQuery($spanWithinQuery); 45 | 46 | $queryArray = $search->toArray(); 47 | ``` 48 | 49 | 50 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-within-query.html 51 | -------------------------------------------------------------------------------- /docs/Query/Specialized/MoreLikeThis.md: -------------------------------------------------------------------------------- 1 | # More Like This Query 2 | 3 | > More info about more like this query is in the [official elasticsearch docs][1] 4 | 5 | The More Like This Query (MLT Query) finds documents that are "like" a given set of documents. 6 | 7 | ## Simple example 8 | 9 | ```JSON 10 | { 11 | "more_like_this" : { 12 | "fields" : ["title", "description"], 13 | "like_text" : "Once upon a time", 14 | "min_term_freq" : 1, 15 | "max_query_terms" : 12 16 | } 17 | } 18 | ``` 19 | 20 | In DSL: 21 | 22 | ```php 23 | $moreLikeThisQuery = new MoreLikeThisQuery( 24 | 'Once upon a time', 25 | [ 26 | 'fields' => ['title', 'description'], 27 | 'min_term_freq' => 1, 28 | 'max_query_terms' => 12, 29 | ] 30 | ); 31 | 32 | $search = new Search(); 33 | $search->addQuery($moreLikeThisQuery); 34 | 35 | $queryArray = $search->toArray(); 36 | ``` 37 | 38 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-mlt-query.html 39 | -------------------------------------------------------------------------------- /docs/Query/Specialized/Script.md: -------------------------------------------------------------------------------- 1 | Documentation in progress... -------------------------------------------------------------------------------- /docs/Query/TermLevel/Exists.md: -------------------------------------------------------------------------------- 1 | Documentation in progress... -------------------------------------------------------------------------------- /docs/Query/TermLevel/Fuzzy.md: -------------------------------------------------------------------------------- 1 | # Fuzzy Query 2 | 3 | > More info about fuzzy query is in the [official elasticsearch docs][1] 4 | 5 | The fuzzy query uses similarity based on Levenshtein edit distance for string fields, and a +/- margin on numeric and 6 | date fields. 7 | 8 | ## Simple example 9 | 10 | ```JSON 11 | { 12 | "fuzzy" : { "user" : "ki" } 13 | } 14 | ``` 15 | 16 | In DSL: 17 | 18 | ```php 19 | $fuzzyQuery = new FuzzyQuery('user', 'ki'); 20 | 21 | $search = new Search(); 22 | $search->addQuery($fuzzyQuery); 23 | 24 | $queryArray = $search->toArray(); 25 | ``` 26 | 27 | ## With more advanced settings 28 | 29 | ```JSON 30 | { 31 | "fuzzy" : { 32 | "user" : { 33 | "value" : "ki", 34 | "boost" : 1.0, 35 | "fuzziness" : 2, 36 | "prefix_length" : 0, 37 | "max_expansions": 100 38 | } 39 | } 40 | } 41 | ``` 42 | 43 | In DSL 44 | ```php 45 | $fuzzyQuery = new FuzzyQuery( 46 | 'user', 47 | 'ki', 48 | [ 49 | 'boost' => 1, 50 | 'fuzziness' => 2, 51 | 'prefix_length' => 0, 52 | 'max_expansions' => 100, 53 | ] 54 | ); 55 | 56 | 57 | $search = new Search(); 58 | $search->addQuery($fuzzyQuery); 59 | 60 | $queryArray = $search->toArray(); 61 | ``` 62 | 63 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-fuzzy-query.html 64 | -------------------------------------------------------------------------------- /docs/Query/TermLevel/Ids.md: -------------------------------------------------------------------------------- 1 | # Ids Query 2 | 3 | > More info about ids query is in the [official elasticsearch docs][1] 4 | 5 | Filters documents that only have the provided ids. 6 | 7 | ## Simple example 8 | 9 | ```JSON 10 | { 11 | "ids" : { 12 | "type" : "my_type", 13 | "values" : ["1", "4", "100"] 14 | } 15 | } 16 | ``` 17 | 18 | In DSL: 19 | 20 | ```php 21 | $idsQuery = new IdsQuery(['1', '4', '100'], ['type' => 'my_type']); 22 | 23 | $search = new Search(); 24 | $search->addQuery($idsQuery); 25 | 26 | $queryArray = $search->toArray(); 27 | ``` 28 | 29 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-ids-query.html 30 | -------------------------------------------------------------------------------- /docs/Query/TermLevel/Prefix.md: -------------------------------------------------------------------------------- 1 | # Prefix Query 2 | 3 | > More info about prefix query is in the [official elasticsearch docs][1] 4 | 5 | Matches documents that have fields containing terms with a specified prefix. 6 | 7 | ## Simple example 8 | 9 | ```JSON 10 | { 11 | "prefix" : { "user" : "ki" } 12 | } 13 | ``` 14 | 15 | In DSL: 16 | 17 | ```php 18 | $prefixQuery = new PrefixQuery('user', 'ki'); 19 | 20 | $search = new Search(); 21 | $search->addQuery($prefixQuery); 22 | 23 | $queryArray = $search->toArray(); 24 | ``` 25 | 26 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-prefix-query.html 27 | -------------------------------------------------------------------------------- /docs/Query/TermLevel/Range.md: -------------------------------------------------------------------------------- 1 | # Range Query 2 | 3 | > More info about range query is in the [official elasticsearch docs][1] 4 | 5 | Matches documents with fields that have terms within a certain range. 6 | 7 | ## Simple example 8 | 9 | ```JSON 10 | { 11 | { 12 | "range" : { 13 | "age" : { 14 | "gte" : 10, 15 | "lte" : 20, 16 | "boost" : 2.0 17 | } 18 | } 19 | } 20 | } 21 | ``` 22 | 23 | In DSL: 24 | 25 | ```php 26 | $rangeQuery = new RangeQuery( 27 | 'age', 28 | [ 29 | 'gte' => 10, 30 | 'lte' => 20, 31 | 'boost' => 2.0, 32 | ] 33 | ); 34 | 35 | $search = new Search(); 36 | $search->addQuery($rangeQuery); 37 | 38 | $queryArray = $search->toArray(); 39 | ``` 40 | 41 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html 42 | -------------------------------------------------------------------------------- /docs/Query/TermLevel/Regexp.md: -------------------------------------------------------------------------------- 1 | # Regexp Query 2 | 3 | > More info about regexp query is in the [official elasticsearch docs][1] 4 | 5 | The regexp query allows you to use regular expression term queries. 6 | 7 | ## Simple example 8 | 9 | ```JSON 10 | { 11 | "query": { 12 | "regexp":{ 13 | "name.first" : "s.*y" 14 | } 15 | } 16 | } 17 | ``` 18 | 19 | And now the query via DSL: 20 | 21 | ```php 22 | $regexpQuery = new RegexpQuery('name.first', 's.*y'); 23 | 24 | $search = new Search(); 25 | $search->addQuery($regexpQuery); 26 | 27 | $queryArray = $search->toArray(); 28 | ``` 29 | 30 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html 31 | -------------------------------------------------------------------------------- /docs/Query/TermLevel/Term.md: -------------------------------------------------------------------------------- 1 | # Term Query 2 | 3 | > More info about term query is in the [official elasticsearch docs][1] 4 | 5 | The term query finds documents that contain the exact term specified in the inverted index. 6 | 7 | 8 | ## Simple example 9 | 10 | ```JSON 11 | { 12 | "term" : { "user" : "Kimchy" } 13 | } 14 | ``` 15 | 16 | In DSL: 17 | 18 | ```php 19 | $termQuery = new TermQuery('user', 'Kimchy'); 20 | 21 | $search = new Search(); 22 | $search->addQuery($termQuery); 23 | 24 | $queryArray = $search->toArray(); 25 | ``` 26 | 27 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html 28 | -------------------------------------------------------------------------------- /docs/Query/TermLevel/Terms.md: -------------------------------------------------------------------------------- 1 | # Terms Query 2 | 3 | > More info about terms query is in the [official elasticsearch docs][1] 4 | 5 | A query that match on any of the provided terms. 6 | 7 | ## Simple example 8 | 9 | ```JSON 10 | { 11 | "terms" : { 12 | "tags" : [ "blue", "pill" ], 13 | "minimum_should_match" : 1 14 | } 15 | } 16 | ``` 17 | 18 | In DSL: 19 | 20 | ```php 21 | $termsQuery = new TermsQuery( 22 | 'tags', 23 | ['blue', 'pill'], 24 | ['minimum_should_match' => 1] 25 | ); 26 | 27 | $search = new Search(); 28 | $search->addQuery($termsQuery); 29 | 30 | $queryArray = $search->toArray(); 31 | ``` 32 | 33 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html 34 | -------------------------------------------------------------------------------- /docs/Query/TermLevel/Type.md: -------------------------------------------------------------------------------- 1 | Documentation in progress... -------------------------------------------------------------------------------- /docs/Query/TermLevel/Wildcard.md: -------------------------------------------------------------------------------- 1 | # Wildcard query 2 | 3 | > More info about Wildcard query is in the [official elasticsearch docs][1] 4 | 5 | Matches documents that have fields matching a wildcard expression (not analyzed). 6 | 7 | Lets take an example to write a wildcard query with Elasticsearch DSL. 8 | 9 | ```JSON 10 | { 11 | "wildcard" : { 12 | "user" : { 13 | "value" : "ki*y" 14 | }, 15 | "boost" : 2.0 16 | } 17 | } 18 | ``` 19 | 20 | And now the query via DSL: 21 | 22 | ```php 23 | 24 | $search = new Search(); 25 | $wildcard= new WildcardQuery('user', 'ki*y', ["boost" => 2.0]); 26 | $search->addQuery($wildcard); 27 | $queryArray = $search->toArray(); 28 | 29 | ``` 30 | 31 | 32 | [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-wildcard-query.html 33 | -------------------------------------------------------------------------------- /docs/index.md: -------------------------------------------------------------------------------- 1 | # Elasticsearch DSL 2 | 3 | Welcome to Elasticsearch DSL library. The main purpose of this library is to provide objective query builder for [elasticsearch-php][1] client. 4 | 5 | Everything starts from the `Search` object. We recommend first to take a look at the [Search](HowTo/HowToSearch.md) chapter. 6 | 7 | ### Topics: 8 | - [Build Queries](Query/index.md) 9 | - [Build Aggregations](Aggregation/index.md) 10 | - [Build Suggest](Suggest/index.md) 11 | 12 | ### How to 13 | - [How to Search](HowTo/HowToSearch.md) 14 | - [How to set custom parameters to Search](HowTo/CustomParameters.md) 15 | - more coming soon.. 16 | 17 | [1]: https://github.com/elastic/elasticsearch-php 18 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | 16 | ./tests/Unit/ 17 | 18 | 19 | ./tests/Functional/ 20 | 21 | 22 | ./tests/ 23 | 24 | 25 | 26 | 27 | 28 | ./ 29 | 30 | ./tests 31 | ./vendor 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /src/Aggregation/Bucketing/GlobalAggregation.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Aggregation\Bucketing; 13 | 14 | use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; 15 | use ONGR\ElasticsearchDSL\Aggregation\Type\BucketingTrait; 16 | 17 | /** 18 | * Class representing GlobalAggregation. 19 | * 20 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-global-aggregation.html 21 | */ 22 | class GlobalAggregation extends AbstractAggregation 23 | { 24 | use BucketingTrait; 25 | 26 | /** 27 | * {@inheritdoc} 28 | */ 29 | public function setField($field) 30 | { 31 | throw new \LogicException("Global aggregation, doesn't support `field` parameter"); 32 | } 33 | 34 | /** 35 | * {@inheritdoc} 36 | */ 37 | public function getType() 38 | { 39 | return 'global'; 40 | } 41 | 42 | /** 43 | * {@inheritdoc} 44 | */ 45 | public function getArray() 46 | { 47 | return new \stdClass(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/Aggregation/Bucketing/MissingAggregation.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Aggregation\Bucketing; 13 | 14 | use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; 15 | use ONGR\ElasticsearchDSL\Aggregation\Type\BucketingTrait; 16 | 17 | /** 18 | * Class representing missing aggregation. 19 | * 20 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-missing-aggregation.html 21 | */ 22 | class MissingAggregation extends AbstractAggregation 23 | { 24 | use BucketingTrait; 25 | 26 | /** 27 | * Inner aggregations container init. 28 | * 29 | * @param string $name 30 | * @param string $field 31 | */ 32 | public function __construct($name, $field = null) 33 | { 34 | parent::__construct($name); 35 | 36 | $this->setField($field); 37 | } 38 | 39 | /** 40 | * {@inheritdoc} 41 | */ 42 | public function getArray() 43 | { 44 | if ($this->getField()) { 45 | return ['field' => $this->getField()]; 46 | } 47 | throw new \LogicException('Missing aggregation must have a field set.'); 48 | } 49 | 50 | /** 51 | * {@inheritdoc} 52 | */ 53 | public function getType() 54 | { 55 | return 'missing'; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/Aggregation/Bucketing/SignificantTermsAggregation.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Aggregation\Bucketing; 13 | 14 | /** 15 | * Class representing TermsAggregation. 16 | * 17 | * @link https://goo.gl/xI7zoa 18 | */ 19 | class SignificantTermsAggregation extends TermsAggregation 20 | { 21 | /** 22 | * {@inheritdoc} 23 | */ 24 | public function getType() 25 | { 26 | return 'significant_terms'; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Aggregation/Bucketing/SignificantTextAggregation.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Aggregation\Bucketing; 13 | 14 | /** 15 | * Class representing TermsAggregation. 16 | * 17 | * @link https://goo.gl/xI7zoa 18 | */ 19 | class SignificantTextAggregation extends TermsAggregation 20 | { 21 | /** 22 | * {@inheritdoc} 23 | */ 24 | public function getType() 25 | { 26 | return 'significant_text'; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Aggregation/Metric/AvgAggregation.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Aggregation\Metric; 13 | 14 | /** 15 | * Class representing Avg Aggregation. 16 | * 17 | * @link http://goo.gl/7KOIwo 18 | */ 19 | class AvgAggregation extends StatsAggregation 20 | { 21 | /** 22 | * {@inheritdoc} 23 | */ 24 | public function getType() 25 | { 26 | return 'avg'; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Aggregation/Metric/GeoCentroidAggregation.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Aggregation\Metric; 13 | 14 | use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; 15 | use ONGR\ElasticsearchDSL\Aggregation\Type\MetricTrait; 16 | 17 | /** 18 | * Class representing geo centroid aggregation. 19 | * 20 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-geocentroid-aggregation.html 21 | */ 22 | class GeoCentroidAggregation extends AbstractAggregation 23 | { 24 | use MetricTrait; 25 | 26 | /** 27 | * Inner aggregations container init. 28 | * 29 | * @param string $name 30 | * @param string $field 31 | */ 32 | public function __construct($name, $field = null) 33 | { 34 | parent::__construct($name); 35 | 36 | $this->setField($field); 37 | } 38 | 39 | /** 40 | * {@inheritdoc} 41 | */ 42 | public function getArray() 43 | { 44 | $data = []; 45 | if ($this->getField()) { 46 | $data['field'] = $this->getField(); 47 | } else { 48 | throw new \LogicException('Geo centroid aggregation must have a field set.'); 49 | } 50 | 51 | return $data; 52 | } 53 | 54 | /** 55 | * {@inheritdoc} 56 | */ 57 | public function getType() 58 | { 59 | return 'geo_centroid'; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/Aggregation/Metric/MaxAggregation.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Aggregation\Metric; 13 | 14 | /** 15 | * Class representing Max Aggregation. 16 | * 17 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-max-aggregation.html 18 | */ 19 | class MaxAggregation extends StatsAggregation 20 | { 21 | /** 22 | * {@inheritdoc} 23 | */ 24 | public function getType() 25 | { 26 | return 'max'; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Aggregation/Metric/MinAggregation.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Aggregation\Metric; 13 | 14 | /** 15 | * Class representing Min Aggregation. 16 | * 17 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-min-aggregation.html 18 | */ 19 | class MinAggregation extends StatsAggregation 20 | { 21 | /** 22 | * {@inheritdoc} 23 | */ 24 | public function getType() 25 | { 26 | return 'min'; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Aggregation/Metric/SumAggregation.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Aggregation\Metric; 13 | 14 | /** 15 | * Class representing Sum Aggregation. 16 | * 17 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-sum-aggregation.html 18 | */ 19 | class SumAggregation extends StatsAggregation 20 | { 21 | /** 22 | * {@inheritdoc} 23 | */ 24 | public function getType() 25 | { 26 | return 'sum'; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Aggregation/Metric/ValueCountAggregation.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Aggregation\Metric; 13 | 14 | /** 15 | * Class representing Value Count Aggregation. 16 | * 17 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-valuecount-aggregation.html 18 | */ 19 | class ValueCountAggregation extends StatsAggregation 20 | { 21 | /** 22 | * {@inheritdoc} 23 | */ 24 | public function getType() 25 | { 26 | return 'value_count'; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Aggregation/Pipeline/AbstractPipelineAggregation.php: -------------------------------------------------------------------------------- 1 | setBucketsPath($bucketsPath); 25 | } 26 | 27 | /** 28 | * @return string 29 | */ 30 | public function getBucketsPath() 31 | { 32 | return $this->bucketsPath; 33 | } 34 | 35 | /** 36 | * @param string $bucketsPath 37 | * 38 | * @return $this 39 | */ 40 | public function setBucketsPath($bucketsPath) 41 | { 42 | $this->bucketsPath = $bucketsPath; 43 | 44 | return $this; 45 | } 46 | 47 | /** 48 | * {@inheritdoc} 49 | */ 50 | public function getArray() 51 | { 52 | return ['buckets_path' => $this->getBucketsPath()]; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/Aggregation/Pipeline/AvgBucketAggregation.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Aggregation\Pipeline; 13 | 14 | /** 15 | * Class representing Avg Bucket Pipeline Aggregation. 16 | * 17 | * @link https://goo.gl/SWK2nP 18 | */ 19 | class AvgBucketAggregation extends AbstractPipelineAggregation 20 | { 21 | /** 22 | * {@inheritdoc} 23 | */ 24 | public function getType() 25 | { 26 | return 'avg_bucket'; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Aggregation/Pipeline/BucketSelectorAggregation.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Aggregation\Pipeline; 13 | 14 | /** 15 | * Class representing Bucket Selector Pipeline Aggregation. 16 | * 17 | * @link https://goo.gl/IQbyyM 18 | */ 19 | class BucketSelectorAggregation extends BucketScriptAggregation 20 | { 21 | /** 22 | * {@inheritdoc} 23 | */ 24 | public function getType() 25 | { 26 | return 'bucket_selector'; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Aggregation/Pipeline/CumulativeSumAggregation.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Aggregation\Pipeline; 13 | 14 | /** 15 | * Class representing Cumulative Sum Pipeline Aggregation. 16 | * 17 | * @link https://goo.gl/EUzda6 18 | */ 19 | class CumulativeSumAggregation extends AbstractPipelineAggregation 20 | { 21 | /** 22 | * {@inheritdoc} 23 | */ 24 | public function getType() 25 | { 26 | return 'cumulative_sum'; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Aggregation/Pipeline/DerivativeAggregation.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Aggregation\Pipeline; 13 | 14 | /** 15 | * Class representing Derivative Pipeline Aggregation. 16 | * 17 | * @link https://goo.gl/Tt2MIR 18 | */ 19 | class DerivativeAggregation extends AbstractPipelineAggregation 20 | { 21 | /** 22 | * {@inheritdoc} 23 | */ 24 | public function getType() 25 | { 26 | return 'derivative'; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Aggregation/Pipeline/ExtendedStatsBucketAggregation.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Aggregation\Pipeline; 13 | 14 | /** 15 | * Class representing Extended Stats Bucket Pipeline Aggregation. 16 | * 17 | * @link https://goo.gl/rn8vtA 18 | */ 19 | class ExtendedStatsBucketAggregation extends AbstractPipelineAggregation 20 | { 21 | /** 22 | * {@inheritdoc} 23 | */ 24 | public function getType() 25 | { 26 | return 'extended_stats_bucket'; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Aggregation/Pipeline/MaxBucketAggregation.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Aggregation\Pipeline; 13 | 14 | /** 15 | * Class representing Max Bucket Pipeline Aggregation. 16 | * 17 | * @link https://goo.gl/FQQWIv 18 | */ 19 | class MaxBucketAggregation extends AbstractPipelineAggregation 20 | { 21 | /** 22 | * {@inheritdoc} 23 | */ 24 | public function getType() 25 | { 26 | return 'max_bucket'; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Aggregation/Pipeline/MinBucketAggregation.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Aggregation\Pipeline; 13 | 14 | /** 15 | * Class representing Min Bucket Pipeline Aggregation. 16 | * 17 | * @link https://goo.gl/5oo4XH 18 | */ 19 | class MinBucketAggregation extends AbstractPipelineAggregation 20 | { 21 | /** 22 | * {@inheritdoc} 23 | */ 24 | public function getType() 25 | { 26 | return 'min_bucket'; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Aggregation/Pipeline/MovingAverageAggregation.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Aggregation\Pipeline; 13 | 14 | /** 15 | * Class representing Max Bucket Pipeline Aggregation. 16 | * 17 | * @link https://goo.gl/8gIfok 18 | */ 19 | class MovingAverageAggregation extends AbstractPipelineAggregation 20 | { 21 | /** 22 | * {@inheritdoc} 23 | */ 24 | public function getType() 25 | { 26 | return 'moving_avg'; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Aggregation/Pipeline/MovingFunctionAggregation.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Aggregation\Pipeline; 13 | 14 | /** 15 | * Class representing Max Bucket Pipeline Aggregation. 16 | * 17 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-movfn-aggregation.html 18 | */ 19 | class MovingFunctionAggregation extends AbstractPipelineAggregation 20 | { 21 | /** 22 | * {@inheritdoc} 23 | */ 24 | public function getType() 25 | { 26 | return 'moving_fn'; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Aggregation/Pipeline/PercentilesBucketAggregation.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Aggregation\Pipeline; 13 | 14 | /** 15 | * Class representing Percentiles Bucket Pipeline Aggregation. 16 | * 17 | * @link https://goo.gl/bqi7m5 18 | */ 19 | class PercentilesBucketAggregation extends AbstractPipelineAggregation 20 | { 21 | /** 22 | * @var array 23 | */ 24 | private $percents; 25 | 26 | /** 27 | * {@inheritdoc} 28 | */ 29 | public function getType() 30 | { 31 | return 'percentiles_bucket'; 32 | } 33 | 34 | /** 35 | * @return array 36 | */ 37 | public function getPercents() 38 | { 39 | return $this->percents; 40 | } 41 | 42 | /** 43 | * @param array $percents 44 | * 45 | * @return $this 46 | */ 47 | public function setPercents(array $percents) 48 | { 49 | $this->percents = $percents; 50 | 51 | return $this; 52 | } 53 | 54 | /** 55 | * {@inheritdoc} 56 | */ 57 | public function getArray() 58 | { 59 | $data = ['buckets_path' => $this->getBucketsPath()]; 60 | 61 | if ($this->getPercents()) { 62 | $data['percents'] = $this->getPercents(); 63 | } 64 | 65 | return $data; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/Aggregation/Pipeline/SerialDifferencingAggregation.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Aggregation\Pipeline; 13 | 14 | /** 15 | * Class representing Serial Differencing Pipeline Aggregation. 16 | * 17 | * @link https://goo.gl/46ZR4v 18 | */ 19 | class SerialDifferencingAggregation extends AbstractPipelineAggregation 20 | { 21 | /** 22 | * {@inheritdoc} 23 | */ 24 | public function getType() 25 | { 26 | return 'serial_diff'; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Aggregation/Pipeline/StatsBucketAggregation.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Aggregation\Pipeline; 13 | 14 | /** 15 | * Class representing Stats Bucket Pipeline Aggregation. 16 | * 17 | * @link https://goo.gl/apQXaQ 18 | */ 19 | class StatsBucketAggregation extends AbstractPipelineAggregation 20 | { 21 | /** 22 | * {@inheritdoc} 23 | */ 24 | public function getType() 25 | { 26 | return 'stats_bucket'; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Aggregation/Pipeline/SumBucketAggregation.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Aggregation\Pipeline; 13 | 14 | /** 15 | * Class representing Max Bucket Pipeline Aggregation. 16 | * 17 | * @link https://goo.gl/8gIfok 18 | */ 19 | class SumBucketAggregation extends AbstractPipelineAggregation 20 | { 21 | /** 22 | * {@inheritdoc} 23 | */ 24 | public function getType() 25 | { 26 | return 'sum_bucket'; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Aggregation/Type/BucketingTrait.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Aggregation\Type; 13 | 14 | /** 15 | * Trait used by Aggregations which supports nesting. 16 | */ 17 | trait BucketingTrait 18 | { 19 | /** 20 | * Bucketing aggregations supports nesting. 21 | * 22 | * @return bool 23 | */ 24 | protected function supportsNesting() 25 | { 26 | return true; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Aggregation/Type/MetricTrait.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Aggregation\Type; 13 | 14 | /** 15 | * Trait used by Aggregations which do not support nesting. 16 | */ 17 | trait MetricTrait 18 | { 19 | /** 20 | * Metric aggregations does not support nesting. 21 | * 22 | * @return bool 23 | */ 24 | protected function supportsNesting() 25 | { 26 | return false; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/BuilderInterface.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL; 13 | 14 | /** 15 | * Interface BuilderInterface. 16 | */ 17 | interface BuilderInterface 18 | { 19 | /** 20 | * Generates array which will be passed to elasticsearch-php client. 21 | * 22 | * @return array 23 | */ 24 | public function toArray(); 25 | 26 | /** 27 | * Returns element type. 28 | * 29 | * @return string 30 | */ 31 | public function getType(); 32 | } 33 | -------------------------------------------------------------------------------- /src/FieldAwareTrait.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL; 13 | 14 | trait FieldAwareTrait 15 | { 16 | /** 17 | * @var string 18 | */ 19 | private $field; 20 | 21 | /** 22 | * @return mixed 23 | */ 24 | public function getField() 25 | { 26 | return $this->field; 27 | } 28 | 29 | /** 30 | * @param string $field 31 | * 32 | * @return $this 33 | */ 34 | public function setField($field) 35 | { 36 | $this->field = $field; 37 | 38 | return $this; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/InnerHit/ParentInnerHit.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL; 13 | 14 | trait NameAwareTrait 15 | { 16 | /** 17 | * @var string 18 | */ 19 | private $name; 20 | 21 | /** 22 | * @return mixed 23 | */ 24 | public function getName() 25 | { 26 | return $this->name; 27 | } 28 | 29 | /** 30 | * @param string $name 31 | * 32 | * @return $this 33 | */ 34 | public function setName($name) 35 | { 36 | $this->name = $name; 37 | 38 | return $this; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/NamedBuilderInterface.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL; 13 | 14 | /** 15 | * Interface NamedBuilderInterface. 16 | */ 17 | interface NamedBuilderInterface extends BuilderInterface 18 | { 19 | /** 20 | * Returns element name. 21 | * 22 | * @return string 23 | */ 24 | public function getName(); 25 | } 26 | -------------------------------------------------------------------------------- /src/Query/Compound/ConstantScoreQuery.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Query\Compound; 13 | 14 | use ONGR\ElasticsearchDSL\BuilderInterface; 15 | use ONGR\ElasticsearchDSL\ParametersTrait; 16 | 17 | /** 18 | * Represents Elasticsearch "constant_score" query. 19 | * 20 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-constant-score-query.html 21 | */ 22 | class ConstantScoreQuery implements BuilderInterface 23 | { 24 | use ParametersTrait; 25 | 26 | /** 27 | * @var BuilderInterface 28 | */ 29 | private $query; 30 | 31 | /** 32 | * @param BuilderInterface $query 33 | * @param array $parameters 34 | */ 35 | public function __construct(BuilderInterface $query, array $parameters = []) 36 | { 37 | $this->query = $query; 38 | $this->setParameters($parameters); 39 | } 40 | 41 | /** 42 | * {@inheritdoc} 43 | */ 44 | public function getType() 45 | { 46 | return 'constant_score'; 47 | } 48 | 49 | /** 50 | * {@inheritdoc} 51 | */ 52 | public function toArray() 53 | { 54 | $query = [ 55 | 'filter' => $this->query->toArray(), 56 | ]; 57 | 58 | $output = $this->processArray($query); 59 | 60 | return [$this->getType() => $output]; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/Query/FullText/CommonTermsQuery.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Query\FullText; 13 | 14 | use ONGR\ElasticsearchDSL\BuilderInterface; 15 | use ONGR\ElasticsearchDSL\ParametersTrait; 16 | 17 | /** 18 | * Represents Elasticsearch "common" query. 19 | * 20 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-common-terms-query.html 21 | */ 22 | class CommonTermsQuery implements BuilderInterface 23 | { 24 | use ParametersTrait; 25 | 26 | /** 27 | * @var string 28 | */ 29 | private $field; 30 | 31 | /** 32 | * @var string 33 | */ 34 | private $query; 35 | 36 | /** 37 | * @param string $field 38 | * @param string $query 39 | * @param array $parameters 40 | */ 41 | public function __construct($field, $query, array $parameters = []) 42 | { 43 | $this->field = $field; 44 | $this->query = $query; 45 | $this->setParameters($parameters); 46 | } 47 | 48 | /** 49 | * {@inheritdoc} 50 | */ 51 | public function getType() 52 | { 53 | return 'common'; 54 | } 55 | 56 | /** 57 | * {@inheritdoc} 58 | */ 59 | public function toArray() 60 | { 61 | $query = [ 62 | 'query' => $this->query, 63 | ]; 64 | 65 | $output = [ 66 | $this->field => $this->processArray($query), 67 | ]; 68 | 69 | return [$this->getType() => $output]; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/Query/FullText/MatchPhrasePrefixQuery.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Query\FullText; 13 | 14 | /** 15 | * Represents Elasticsearch "match_phrase_prefix" query. 16 | * 17 | * @author Ron Rademaker 18 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html 19 | */ 20 | class MatchPhrasePrefixQuery extends MatchQuery 21 | { 22 | /** 23 | * {@inheritdoc} 24 | */ 25 | public function getType() 26 | { 27 | return 'match_phrase_prefix'; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Query/FullText/MatchPhraseQuery.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Query\FullText; 13 | 14 | /** 15 | * Represents Elasticsearch "match_phrase" query. 16 | * 17 | * @author Ron Rademaker 18 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html 19 | */ 20 | class MatchPhraseQuery extends MatchQuery 21 | { 22 | /** 23 | * {@inheritdoc} 24 | */ 25 | public function getType() 26 | { 27 | return 'match_phrase'; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Query/FullText/MatchQuery.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Query\FullText; 13 | 14 | use ONGR\ElasticsearchDSL\BuilderInterface; 15 | use ONGR\ElasticsearchDSL\ParametersTrait; 16 | 17 | /** 18 | * Represents Elasticsearch "match" query. 19 | * 20 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html 21 | */ 22 | class MatchQuery implements BuilderInterface 23 | { 24 | use ParametersTrait; 25 | 26 | /** 27 | * @var string 28 | */ 29 | private $field; 30 | 31 | /** 32 | * @var string 33 | */ 34 | private $query; 35 | 36 | /** 37 | * @param string $field 38 | * @param string $query 39 | * @param array $parameters 40 | */ 41 | public function __construct($field, $query, array $parameters = []) 42 | { 43 | $this->field = $field; 44 | $this->query = $query; 45 | $this->setParameters($parameters); 46 | } 47 | 48 | /** 49 | * {@inheritdoc} 50 | */ 51 | public function getType() 52 | { 53 | return 'match'; 54 | } 55 | 56 | /** 57 | * {@inheritdoc} 58 | */ 59 | public function toArray() 60 | { 61 | $query = [ 62 | 'query' => $this->query, 63 | ]; 64 | 65 | $output = [ 66 | $this->field => $this->processArray($query), 67 | ]; 68 | 69 | return [$this->getType() => $output]; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/Query/FullText/QueryStringQuery.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Query\FullText; 13 | 14 | use ONGR\ElasticsearchDSL\BuilderInterface; 15 | use ONGR\ElasticsearchDSL\ParametersTrait; 16 | 17 | /** 18 | * Represents Elasticsearch "query_string" query. 19 | * 20 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html 21 | */ 22 | class QueryStringQuery implements BuilderInterface 23 | { 24 | use ParametersTrait; 25 | 26 | /** 27 | * @var string The actual query to be parsed. 28 | */ 29 | private $query; 30 | 31 | /** 32 | * @param string $query 33 | * @param array $parameters 34 | */ 35 | public function __construct($query, array $parameters = []) 36 | { 37 | $this->query = $query; 38 | $this->setParameters($parameters); 39 | } 40 | 41 | /** 42 | * {@inheritdoc} 43 | */ 44 | public function getType() 45 | { 46 | return 'query_string'; 47 | } 48 | 49 | /** 50 | * {@inheritdoc} 51 | */ 52 | public function toArray() 53 | { 54 | $query = [ 55 | 'query' => $this->query, 56 | ]; 57 | 58 | $output = $this->processArray($query); 59 | 60 | return [$this->getType() => $output]; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/Query/FullText/SimpleQueryStringQuery.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Query\FullText; 13 | 14 | use ONGR\ElasticsearchDSL\BuilderInterface; 15 | use ONGR\ElasticsearchDSL\ParametersTrait; 16 | 17 | /** 18 | * Represents Elasticsearch "simple_query_string" query. 19 | * 20 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-simple-query-string-query.html 21 | */ 22 | class SimpleQueryStringQuery implements BuilderInterface 23 | { 24 | use ParametersTrait; 25 | 26 | /** 27 | * @var string The actual query to be parsed. 28 | */ 29 | private $query; 30 | 31 | /** 32 | * @param string $query 33 | * @param array $parameters 34 | */ 35 | public function __construct($query, array $parameters = []) 36 | { 37 | $this->query = $query; 38 | $this->setParameters($parameters); 39 | } 40 | 41 | /** 42 | * {@inheritdoc} 43 | */ 44 | public function getType() 45 | { 46 | return 'simple_query_string'; 47 | } 48 | 49 | /** 50 | * {@inheritdoc} 51 | */ 52 | public function toArray() 53 | { 54 | $query = [ 55 | 'query' => $this->query, 56 | ]; 57 | 58 | $output = $this->processArray($query); 59 | 60 | return [$this->getType() => $output]; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/Query/Geo/GeoPolygonQuery.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Query\Geo; 13 | 14 | use ONGR\ElasticsearchDSL\BuilderInterface; 15 | use ONGR\ElasticsearchDSL\ParametersTrait; 16 | 17 | /** 18 | * Represents Elasticsearch "geo_polygon" query. 19 | * 20 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-polygon-query.html 21 | */ 22 | class GeoPolygonQuery implements BuilderInterface 23 | { 24 | use ParametersTrait; 25 | 26 | /** 27 | * @var string 28 | */ 29 | private $field; 30 | 31 | /** 32 | * @var array 33 | */ 34 | private $points; 35 | 36 | /** 37 | * @param string $field 38 | * @param array $points 39 | * @param array $parameters 40 | */ 41 | public function __construct($field, array $points = [], array $parameters = []) 42 | { 43 | $this->field = $field; 44 | $this->points = $points; 45 | $this->setParameters($parameters); 46 | } 47 | 48 | /** 49 | * {@inheritdoc} 50 | */ 51 | public function getType() 52 | { 53 | return 'geo_polygon'; 54 | } 55 | 56 | /** 57 | * {@inheritdoc} 58 | */ 59 | public function toArray() 60 | { 61 | $query = [$this->field => ['points' => $this->points]]; 62 | $output = $this->processArray($query); 63 | 64 | return [$this->getType() => $output]; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/Query/Joining/ParentIdQuery.php: -------------------------------------------------------------------------------- 1 | childType = $childType; 33 | $this->parentId = $parentId; 34 | $this->setParameters($parameters); 35 | } 36 | 37 | /** 38 | * {@inheritdoc} 39 | */ 40 | public function getType() 41 | { 42 | return 'parent_id'; 43 | } 44 | 45 | /** 46 | * {@inheritdoc} 47 | */ 48 | public function toArray() 49 | { 50 | $query = [ 51 | 'id' => $this->parentId, 52 | 'type' => $this->childType, 53 | ]; 54 | $output = $this->processArray($query); 55 | 56 | return [$this->getType() => $output]; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/Query/MatchAllQuery.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Query; 13 | 14 | use ONGR\ElasticsearchDSL\BuilderInterface; 15 | use ONGR\ElasticsearchDSL\ParametersTrait; 16 | 17 | /** 18 | * Represents Elasticsearch "match_all" query. 19 | * 20 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-all-query.html 21 | */ 22 | class MatchAllQuery implements BuilderInterface 23 | { 24 | use ParametersTrait; 25 | 26 | /** 27 | * @param array $parameters Additional parameters. 28 | */ 29 | public function __construct(array $parameters = []) 30 | { 31 | $this->setParameters($parameters); 32 | } 33 | 34 | /** 35 | * {@inheritdoc} 36 | */ 37 | public function getType() 38 | { 39 | return 'match_all'; 40 | } 41 | 42 | /** 43 | * {@inheritdoc} 44 | */ 45 | public function toArray() 46 | { 47 | $params = $this->getParameters(); 48 | return [$this->getType() => !empty($params) ? $params : new \stdClass()]; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/Query/Span/SpanMultiTermQuery.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Query\Span; 13 | 14 | use ONGR\ElasticsearchDSL\BuilderInterface; 15 | use ONGR\ElasticsearchDSL\ParametersTrait; 16 | 17 | /** 18 | * Elasticsearch span multi term query. 19 | * 20 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-multi-term-query.html 21 | */ 22 | class SpanMultiTermQuery implements SpanQueryInterface 23 | { 24 | use ParametersTrait; 25 | 26 | /** 27 | * @var BuilderInterface 28 | */ 29 | private $query; 30 | 31 | /** 32 | * Accepts one of fuzzy, prefix, term range, wildcard, regexp query. 33 | * 34 | * @param BuilderInterface $query 35 | * @param array $parameters 36 | */ 37 | public function __construct(BuilderInterface $query, array $parameters = []) 38 | { 39 | $this->query = $query; 40 | $this->setParameters($parameters); 41 | } 42 | 43 | /** 44 | * {@inheritdoc} 45 | */ 46 | public function getType() 47 | { 48 | return 'span_multi'; 49 | } 50 | 51 | /** 52 | * {@inheritdoc} 53 | * 54 | * @throws \InvalidArgumentException 55 | */ 56 | public function toArray() 57 | { 58 | $query = []; 59 | $query['match'] = $this->query->toArray(); 60 | $output = $this->processArray($query); 61 | 62 | return [$this->getType() => $output]; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/Query/Span/SpanNearQuery.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Query\Span; 13 | 14 | /** 15 | * Elasticsearch span near query. 16 | * 17 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-near-query.html 18 | */ 19 | class SpanNearQuery extends SpanOrQuery implements SpanQueryInterface 20 | { 21 | /** 22 | * @var int 23 | */ 24 | private $slop; 25 | 26 | /** 27 | * @return int 28 | */ 29 | public function getSlop() 30 | { 31 | return $this->slop; 32 | } 33 | 34 | /** 35 | * @param int $slop 36 | * 37 | * @return $this 38 | */ 39 | public function setSlop($slop) 40 | { 41 | $this->slop = $slop; 42 | 43 | return $this; 44 | } 45 | 46 | /** 47 | * {@inheritdoc} 48 | */ 49 | public function getType() 50 | { 51 | return 'span_near'; 52 | } 53 | 54 | /** 55 | * {@inheritdoc} 56 | */ 57 | public function toArray() 58 | { 59 | $query = []; 60 | foreach ($this->getQueries() as $type) { 61 | $query['clauses'][] = $type->toArray(); 62 | } 63 | $query['slop'] = $this->getSlop(); 64 | $output = $this->processArray($query); 65 | 66 | return [$this->getType() => $output]; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/Query/Span/SpanQueryInterface.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Query\Span; 13 | 14 | use ONGR\ElasticsearchDSL\BuilderInterface; 15 | 16 | /** 17 | * Interface SpanQueryInterface to recognise span queries. 18 | */ 19 | interface SpanQueryInterface extends BuilderInterface 20 | { 21 | } 22 | -------------------------------------------------------------------------------- /src/Query/Span/SpanTermQuery.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Query\Span; 13 | 14 | use ONGR\ElasticsearchDSL\Query\TermLevel\TermQuery; 15 | 16 | /** 17 | * Elasticsearch span_term query class. 18 | * 19 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-term-query.html 20 | */ 21 | class SpanTermQuery extends TermQuery implements SpanQueryInterface 22 | { 23 | /** 24 | * {@inheritdoc} 25 | */ 26 | public function getType() 27 | { 28 | return 'span_term'; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/Query/Span/SpanWithinQuery.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Query\Span; 13 | 14 | /** 15 | * Elasticsearch span within query. 16 | * 17 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-within-query.html 18 | */ 19 | class SpanWithinQuery extends SpanContainingQuery 20 | { 21 | /** 22 | * {@inheritdoc} 23 | */ 24 | public function getType() 25 | { 26 | return 'span_within'; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Query/Specialized/MoreLikeThisQuery.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Query\Specialized; 13 | 14 | use ONGR\ElasticsearchDSL\BuilderInterface; 15 | use ONGR\ElasticsearchDSL\ParametersTrait; 16 | 17 | /** 18 | * Represents Elasticsearch "more_like_this" query. 19 | * 20 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-mlt-query.html 21 | */ 22 | class MoreLikeThisQuery implements BuilderInterface 23 | { 24 | use ParametersTrait; 25 | 26 | /** 27 | * @var string The text to find documents like it, required if ids or docs are not specified. 28 | */ 29 | private $like; 30 | 31 | /** 32 | * @param string $like 33 | * @param array $parameters 34 | */ 35 | public function __construct($like, array $parameters = []) 36 | { 37 | $this->like = $like; 38 | $this->setParameters($parameters); 39 | } 40 | 41 | /** 42 | * {@inheritdoc} 43 | */ 44 | public function getType() 45 | { 46 | return 'more_like_this'; 47 | } 48 | 49 | /** 50 | * {@inheritdoc} 51 | */ 52 | public function toArray() 53 | { 54 | $query = []; 55 | 56 | if (($this->hasParameter('ids') === false) || ($this->hasParameter('docs') === false)) { 57 | $query['like'] = $this->like; 58 | } 59 | 60 | $output = $this->processArray($query); 61 | 62 | return [$this->getType() => $output]; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/Query/Specialized/ScriptQuery.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Query\Specialized; 13 | 14 | use ONGR\ElasticsearchDSL\BuilderInterface; 15 | use ONGR\ElasticsearchDSL\ParametersTrait; 16 | 17 | /** 18 | * Represents Elasticsearch "script" query. 19 | * 20 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-script-query.html 21 | */ 22 | class ScriptQuery implements BuilderInterface 23 | { 24 | use ParametersTrait; 25 | 26 | /** 27 | * @var string 28 | */ 29 | private $script; 30 | 31 | /** 32 | * @param string $script Script 33 | * @param array $parameters Optional parameters 34 | */ 35 | public function __construct($script, array $parameters = []) 36 | { 37 | $this->script = $script; 38 | $this->setParameters($parameters); 39 | } 40 | 41 | /** 42 | * {@inheritdoc} 43 | */ 44 | public function getType() 45 | { 46 | return 'script'; 47 | } 48 | 49 | /** 50 | * {@inheritdoc} 51 | */ 52 | public function toArray() 53 | { 54 | $query = ['inline' => $this->script]; 55 | $output = $this->processArray($query); 56 | 57 | return [$this->getType() => ['script' => $output]]; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/Query/TermLevel/ExistsQuery.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Query\TermLevel; 13 | 14 | use ONGR\ElasticsearchDSL\BuilderInterface; 15 | 16 | /** 17 | * Represents Elasticsearch "exists" query. 18 | * 19 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-exists-query.html 20 | */ 21 | class ExistsQuery implements BuilderInterface 22 | { 23 | /** 24 | * @var string 25 | */ 26 | private $field; 27 | 28 | /** 29 | * Constructor. 30 | * 31 | * @param string $field Field value 32 | */ 33 | public function __construct($field) 34 | { 35 | $this->field = $field; 36 | } 37 | 38 | /** 39 | * {@inheritdoc} 40 | */ 41 | public function getType() 42 | { 43 | return 'exists'; 44 | } 45 | 46 | /** 47 | * {@inheritdoc} 48 | */ 49 | public function toArray() 50 | { 51 | return [ 52 | $this->getType() => [ 53 | 'field' => $this->field, 54 | ], 55 | ]; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/Query/TermLevel/FuzzyQuery.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Query\TermLevel; 13 | 14 | use ONGR\ElasticsearchDSL\BuilderInterface; 15 | use ONGR\ElasticsearchDSL\ParametersTrait; 16 | 17 | /** 18 | * Represents Elasticsearch "fuzzy" query. 19 | * 20 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-fuzzy-query.html 21 | */ 22 | class FuzzyQuery implements BuilderInterface 23 | { 24 | use ParametersTrait; 25 | 26 | /** 27 | * @var string 28 | */ 29 | private $field; 30 | 31 | /** 32 | * @var string 33 | */ 34 | private $value; 35 | 36 | /** 37 | * @param string $field 38 | * @param string $value 39 | * @param array $parameters 40 | */ 41 | public function __construct($field, $value, array $parameters = []) 42 | { 43 | $this->field = $field; 44 | $this->value = $value; 45 | $this->setParameters($parameters); 46 | } 47 | 48 | /** 49 | * {@inheritdoc} 50 | */ 51 | public function getType() 52 | { 53 | return 'fuzzy'; 54 | } 55 | 56 | /** 57 | * {@inheritdoc} 58 | */ 59 | public function toArray() 60 | { 61 | $query = [ 62 | 'value' => $this->value, 63 | ]; 64 | 65 | $output = [ 66 | $this->field => $this->processArray($query), 67 | ]; 68 | 69 | return [$this->getType() => $output]; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/Query/TermLevel/IdsQuery.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Query\TermLevel; 13 | 14 | use ONGR\ElasticsearchDSL\BuilderInterface; 15 | use ONGR\ElasticsearchDSL\ParametersTrait; 16 | 17 | /** 18 | * Represents Elasticsearch "ids" query. 19 | * 20 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-ids-query.html 21 | */ 22 | class IdsQuery implements BuilderInterface 23 | { 24 | use ParametersTrait; 25 | 26 | /** 27 | * @var array 28 | */ 29 | private $values; 30 | 31 | /** 32 | * @param array $values 33 | * @param array $parameters 34 | */ 35 | public function __construct(array $values, array $parameters = []) 36 | { 37 | $this->values = $values; 38 | $this->setParameters($parameters); 39 | } 40 | 41 | /** 42 | * {@inheritdoc} 43 | */ 44 | public function getType() 45 | { 46 | return 'ids'; 47 | } 48 | 49 | /** 50 | * {@inheritdoc} 51 | */ 52 | public function toArray() 53 | { 54 | $query = [ 55 | 'values' => $this->values, 56 | ]; 57 | 58 | $output = $this->processArray($query); 59 | 60 | return [$this->getType() => $output]; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/Query/TermLevel/TypeQuery.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Query\TermLevel; 13 | 14 | use ONGR\ElasticsearchDSL\BuilderInterface; 15 | 16 | /** 17 | * Represents Elasticsearch "type" query. 18 | * 19 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-type-query.html 20 | */ 21 | class TypeQuery implements BuilderInterface 22 | { 23 | /** 24 | * @var string 25 | */ 26 | private $type; 27 | 28 | /** 29 | * Constructor. 30 | * 31 | * @param string $type Type name 32 | */ 33 | public function __construct($type) 34 | { 35 | $this->type = $type; 36 | } 37 | 38 | /** 39 | * {@inheritdoc} 40 | */ 41 | public function getType() 42 | { 43 | return 'type'; 44 | } 45 | 46 | /** 47 | * {@inheritdoc} 48 | */ 49 | public function toArray() 50 | { 51 | return [ 52 | $this->getType() => [ 53 | 'value' => $this->type, 54 | ], 55 | ]; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/Query/TermLevel/WildcardQuery.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Query\TermLevel; 13 | 14 | use ONGR\ElasticsearchDSL\BuilderInterface; 15 | use ONGR\ElasticsearchDSL\ParametersTrait; 16 | 17 | /** 18 | * Represents Elasticsearch "wildcard" query. 19 | * 20 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-wildcard-query.html 21 | */ 22 | class WildcardQuery implements BuilderInterface 23 | { 24 | use ParametersTrait; 25 | 26 | /** 27 | * @var string 28 | */ 29 | private $field; 30 | 31 | /** 32 | * @var string 33 | */ 34 | private $value; 35 | 36 | /** 37 | * @param string $field 38 | * @param string $value 39 | * @param array $parameters 40 | */ 41 | public function __construct($field, $value, array $parameters = []) 42 | { 43 | $this->field = $field; 44 | $this->value = $value; 45 | $this->setParameters($parameters); 46 | } 47 | 48 | /** 49 | * {@inheritdoc} 50 | */ 51 | public function getType() 52 | { 53 | return 'wildcard'; 54 | } 55 | 56 | /** 57 | * {@inheritdoc} 58 | */ 59 | public function toArray() 60 | { 61 | $query = [ 62 | 'value' => $this->value, 63 | ]; 64 | 65 | $output = [ 66 | $this->field => $this->processArray($query), 67 | ]; 68 | 69 | return [$this->getType() => $output]; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/ScriptAwareTrait.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL; 13 | 14 | /** 15 | * A trait which handles elasticsearch aggregation script. 16 | */ 17 | trait ScriptAwareTrait 18 | { 19 | /** 20 | * @var string 21 | */ 22 | private $script; 23 | 24 | /** 25 | * @return string 26 | */ 27 | public function getScript() 28 | { 29 | return $this->script; 30 | } 31 | 32 | /** 33 | * @param string $script 34 | * 35 | * @return $this 36 | */ 37 | public function setScript($script) 38 | { 39 | $this->script = $script; 40 | 41 | return $this; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/SearchEndpoint/AggregationsEndpoint.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\SearchEndpoint; 13 | 14 | use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; 15 | use Symfony\Component\Serializer\Normalizer\NormalizerInterface; 16 | 17 | /** 18 | * Search aggregations dsl endpoint. 19 | */ 20 | class AggregationsEndpoint extends AbstractSearchEndpoint 21 | { 22 | /** 23 | * Endpoint name 24 | */ 25 | const NAME = 'aggregations'; 26 | 27 | /** 28 | * {@inheritdoc} 29 | */ 30 | public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []) 31 | { 32 | $output = []; 33 | if (count($this->getAll()) > 0) { 34 | /** @var AbstractAggregation $aggregation */ 35 | foreach ($this->getAll() as $aggregation) { 36 | $output[$aggregation->getName()] = $aggregation->toArray(); 37 | } 38 | } 39 | 40 | return $output; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/SearchEndpoint/InnerHitsEndpoint.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\SearchEndpoint; 13 | 14 | use ONGR\ElasticsearchDSL\InnerHit\NestedInnerHit; 15 | use Symfony\Component\Serializer\Normalizer\NormalizerInterface; 16 | 17 | /** 18 | * Search inner hits dsl endpoint. 19 | */ 20 | class InnerHitsEndpoint extends AbstractSearchEndpoint 21 | { 22 | /** 23 | * Endpoint name 24 | */ 25 | const NAME = 'inner_hits'; 26 | 27 | /** 28 | * {@inheritdoc} 29 | */ 30 | public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []) 31 | { 32 | $output = []; 33 | if (count($this->getAll()) > 0) { 34 | /** @var NestedInnerHit $innerHit */ 35 | foreach ($this->getAll() as $innerHit) { 36 | $output[$innerHit->getName()] = $innerHit->toArray(); 37 | } 38 | } 39 | 40 | return $output; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/SearchEndpoint/PostFilterEndpoint.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\SearchEndpoint; 13 | 14 | use Symfony\Component\Serializer\Normalizer\NormalizerInterface; 15 | 16 | /** 17 | * Search post filter dsl endpoint. 18 | */ 19 | class PostFilterEndpoint extends QueryEndpoint 20 | { 21 | /** 22 | * Endpoint name 23 | */ 24 | const NAME = 'post_filter'; 25 | 26 | /** 27 | * {@inheritdoc} 28 | */ 29 | public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []) 30 | { 31 | if (!$this->getBool()) { 32 | return null; 33 | } 34 | 35 | return $this->getBool()->toArray(); 36 | } 37 | 38 | /** 39 | * {@inheritdoc} 40 | */ 41 | public function getOrder() 42 | { 43 | return 1; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/SearchEndpoint/SearchEndpointFactory.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\SearchEndpoint; 13 | 14 | /** 15 | * Factory for search endpoints. 16 | */ 17 | class SearchEndpointFactory 18 | { 19 | /** 20 | * @var array Holds namespaces for endpoints. 21 | */ 22 | private static $endpoints = [ 23 | 'query' => 'ONGR\ElasticsearchDSL\SearchEndpoint\QueryEndpoint', 24 | 'post_filter' => 'ONGR\ElasticsearchDSL\SearchEndpoint\PostFilterEndpoint', 25 | 'sort' => 'ONGR\ElasticsearchDSL\SearchEndpoint\SortEndpoint', 26 | 'highlight' => 'ONGR\ElasticsearchDSL\SearchEndpoint\HighlightEndpoint', 27 | 'aggregations' => 'ONGR\ElasticsearchDSL\SearchEndpoint\AggregationsEndpoint', 28 | 'suggest' => 'ONGR\ElasticsearchDSL\SearchEndpoint\SuggestEndpoint', 29 | 'inner_hits' => 'ONGR\ElasticsearchDSL\SearchEndpoint\InnerHitsEndpoint', 30 | ]; 31 | 32 | /** 33 | * Returns a search endpoint instance. 34 | * 35 | * @param string $type Type of endpoint. 36 | * 37 | * @return SearchEndpointInterface 38 | * 39 | * @throws \RuntimeException Endpoint does not exist. 40 | */ 41 | public static function get($type) 42 | { 43 | if (!array_key_exists($type, self::$endpoints)) { 44 | throw new \RuntimeException('Endpoint does not exist.'); 45 | } 46 | 47 | return new self::$endpoints[$type](); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/SearchEndpoint/SortEndpoint.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\SearchEndpoint; 13 | 14 | use Symfony\Component\Serializer\Normalizer\NormalizerInterface; 15 | 16 | /** 17 | * Search sort dsl endpoint. 18 | */ 19 | class SortEndpoint extends AbstractSearchEndpoint 20 | { 21 | /** 22 | * Endpoint name 23 | */ 24 | const NAME = 'sort'; 25 | 26 | /** 27 | * {@inheritdoc} 28 | */ 29 | public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []) 30 | { 31 | $output = []; 32 | 33 | foreach ($this->getAll() as $sort) { 34 | $output[] = $sort->toArray(); 35 | } 36 | 37 | return $output; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/SearchEndpoint/SuggestEndpoint.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\SearchEndpoint; 13 | 14 | use ONGR\ElasticsearchDSL\Suggest\TermSuggest; 15 | use Symfony\Component\Serializer\Normalizer\NormalizerInterface; 16 | 17 | /** 18 | * Search suggest dsl endpoint. 19 | */ 20 | class SuggestEndpoint extends AbstractSearchEndpoint 21 | { 22 | /** 23 | * Endpoint name 24 | */ 25 | const NAME = 'suggest'; 26 | 27 | /** 28 | * {@inheritdoc} 29 | */ 30 | public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []) 31 | { 32 | $output = []; 33 | if (count($this->getAll()) > 0) { 34 | /** @var TermSuggest $suggest */ 35 | foreach ($this->getAll() as $suggest) { 36 | $output = array_merge($output, $suggest->toArray()); 37 | } 38 | } 39 | 40 | return $output; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/Serializer/Normalizer/AbstractNormalizable.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Serializer\Normalizer; 13 | 14 | use ONGR\ElasticsearchDSL\ParametersTrait; 15 | use Symfony\Component\Serializer\Normalizer\NormalizableInterface; 16 | 17 | /** 18 | * Custom abstract normalizer which can save references for other objects. 19 | */ 20 | abstract class AbstractNormalizable implements NormalizableInterface 21 | { 22 | use ParametersTrait { 23 | ParametersTrait::hasParameter as hasReference; 24 | ParametersTrait::getParameter as getReference; 25 | ParametersTrait::getParameters as getReferences; 26 | ParametersTrait::addParameter as addReference; 27 | ParametersTrait::removeParameter as removeReference; 28 | ParametersTrait::setParameters as setReferences; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/Serializer/Normalizer/CustomReferencedNormalizer.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Serializer\Normalizer; 13 | 14 | use Symfony\Component\Serializer\Normalizer\CustomNormalizer; 15 | 16 | /** 17 | * Normalizer used with referenced normalized objects. 18 | */ 19 | class CustomReferencedNormalizer extends CustomNormalizer 20 | { 21 | /** 22 | * @var array 23 | */ 24 | private $references = []; 25 | 26 | /** 27 | * {@inheritdoc} 28 | */ 29 | public function normalize($object, $format = null, array $context = []) 30 | { 31 | $object->setReferences($this->references); 32 | $data = parent::normalize($object, $format, $context); 33 | $this->references = array_merge($this->references, $object->getReferences()); 34 | 35 | return $data; 36 | } 37 | 38 | /** 39 | * {@inheritdoc} 40 | */ 41 | public function supportsNormalization($data, $format = null) 42 | { 43 | return $data instanceof AbstractNormalizable; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/Serializer/Normalizer/OrderedNormalizerInterface.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Serializer\Normalizer; 13 | 14 | /** 15 | * This should be implemented by normalizable object that required to be processed in specific order. 16 | */ 17 | interface OrderedNormalizerInterface 18 | { 19 | /** 20 | * Returns normalization priority. 21 | * 22 | * @return int 23 | */ 24 | public function getOrder(); 25 | } 26 | -------------------------------------------------------------------------------- /tests/Functional/Query/MatchAllQueryTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Functional\Query; 13 | 14 | use ONGR\ElasticsearchDSL\Query\MatchAllQuery; 15 | use ONGR\ElasticsearchDSL\Search; 16 | use ONGR\ElasticsearchDSL\Tests\Functional\AbstractElasticsearchTestCase; 17 | 18 | class MatchAllQueryTest extends AbstractElasticsearchTestCase 19 | { 20 | /** 21 | * {@inheritdoc} 22 | */ 23 | protected function getDataArray() 24 | { 25 | return [ 26 | 'product' => [ 27 | [ 28 | 'title' => 'acme', 29 | ], 30 | [ 31 | 'title' => 'foo', 32 | ], 33 | ] 34 | ]; 35 | } 36 | 37 | /** 38 | * Match all test 39 | */ 40 | public function testMatchAll() 41 | { 42 | $search = new Search(); 43 | $matchAll = new MatchAllQuery(); 44 | 45 | $search->addQuery($matchAll); 46 | $q = $search->getQueries(); 47 | $results = $this->executeSearch($search); 48 | 49 | $this->assertEquals($this->getDataArray()['product'], $results); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /tests/Unit/Aggregation/Bucketing/MissingAggregationTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Bucketing\Aggregation; 13 | 14 | use ONGR\ElasticsearchDSL\Aggregation\Bucketing\MissingAggregation; 15 | 16 | class MissingAggregationTest extends \PHPUnit\Framework\TestCase 17 | { 18 | /** 19 | * Test if exception is thrown when field is not set. 20 | * 21 | * @expectedException \LogicException 22 | * @expectedExceptionMessage Missing aggregation must have a field set. 23 | */ 24 | public function testIfExceptionIsThrown() 25 | { 26 | $agg = new MissingAggregation('test_agg'); 27 | $agg->getArray(); 28 | } 29 | 30 | /** 31 | * Test getArray method. 32 | */ 33 | public function testMissingAggregationGetArray() 34 | { 35 | $aggregation = new MissingAggregation('foo'); 36 | $aggregation->setField('bar'); 37 | $result = $aggregation->getArray(); 38 | $this->assertEquals('bar', $result['field']); 39 | } 40 | 41 | /** 42 | * Test getType method. 43 | */ 44 | public function testMissingAggregationGetType() 45 | { 46 | $aggregation = new MissingAggregation('bar'); 47 | $result = $aggregation->getType(); 48 | $this->assertEquals('missing', $result); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /tests/Unit/Aggregation/Bucketing/NestedAggregationTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Bucketing\Aggregation; 13 | 14 | use ONGR\ElasticsearchDSL\Aggregation\Bucketing\NestedAggregation; 15 | use ONGR\ElasticsearchDSL\Aggregation\Bucketing\TermsAggregation; 16 | 17 | class NestedAggregationTest extends \PHPUnit\Framework\TestCase 18 | { 19 | /** 20 | * Test for nested aggregation toArray() method exception. 21 | */ 22 | public function testToArray() 23 | { 24 | $termAggregation = new TermsAggregation('acme'); 25 | 26 | $aggregation = new NestedAggregation('test_nested_agg'); 27 | $aggregation->setPath('test_path'); 28 | $aggregation->addAggregation($termAggregation); 29 | 30 | $expectedResult = [ 31 | 'nested' => ['path' => 'test_path'], 32 | 'aggregations' => [ 33 | $termAggregation->getName() => $termAggregation->toArray(), 34 | ], 35 | ]; 36 | 37 | $this->assertEquals($expectedResult, $aggregation->toArray()); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /tests/Unit/Aggregation/Bucketing/SignificantTermsAggregationTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Bucketing\Aggregation; 13 | 14 | use ONGR\ElasticsearchDSL\Aggregation\Bucketing\SignificantTermsAggregation; 15 | 16 | /** 17 | * Unit test for children aggregation. 18 | */ 19 | class SignificantTermsAggregationTest extends \PHPUnit\Framework\TestCase 20 | { 21 | /** 22 | * Tests getType method. 23 | */ 24 | public function testSignificantTermsAggregationGetType() 25 | { 26 | $aggregation = new SignificantTermsAggregation('foo'); 27 | $result = $aggregation->getType(); 28 | $this->assertEquals('significant_terms', $result); 29 | } 30 | 31 | /** 32 | * Tests getArray method. 33 | */ 34 | public function testSignificantTermsAggregationGetArray() 35 | { 36 | $mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation') 37 | ->disableOriginalConstructor() 38 | ->getMockForAbstractClass(); 39 | $aggregation = new SignificantTermsAggregation('foo', 'title'); 40 | $aggregation->addAggregation($mock); 41 | $result = $aggregation->getArray(); 42 | $expected = ['field' => 'title']; 43 | $this->assertEquals($expected, $result); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /tests/Unit/Aggregation/Bucketing/SignificantTextAggregationTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Bucketing\Aggregation; 13 | 14 | use ONGR\ElasticsearchDSL\Aggregation\Bucketing\SignificantTextAggregation; 15 | 16 | /** 17 | * Unit test for children aggregation. 18 | */ 19 | class SignificantTextAggregationTest extends \PHPUnit\Framework\TestCase 20 | { 21 | /** 22 | * Tests getType method. 23 | */ 24 | public function testSignificantTextAggregationGetType() 25 | { 26 | $aggregation = new SignificantTextAggregation('foo'); 27 | $result = $aggregation->getType(); 28 | $this->assertEquals('significant_text', $result); 29 | } 30 | 31 | /** 32 | * Tests getArray method. 33 | */ 34 | public function testSignificantTermsAggregationGetArray() 35 | { 36 | $mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation') 37 | ->disableOriginalConstructor() 38 | ->getMockForAbstractClass(); 39 | $aggregation = new SignificantTextAggregation('foo', 'title'); 40 | $aggregation->addAggregation($mock); 41 | $result = $aggregation->getArray(); 42 | $expected = ['field' => 'title']; 43 | $this->assertEquals($expected, $result); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /tests/Unit/Aggregation/Metric/GeoCentroidAggregationTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Metric\Aggregation; 13 | 14 | use ONGR\ElasticsearchDSL\Aggregation\Metric\GeoCentroidAggregation; 15 | 16 | /** 17 | * Unit test for children aggregation. 18 | */ 19 | class GeoCentroidAggregationTest extends \PHPUnit\Framework\TestCase 20 | { 21 | /** 22 | * Test if exception is thrown when field is not provided 23 | * 24 | * @expectedException \LogicException 25 | */ 26 | public function testGetArrayException() 27 | { 28 | $aggregation = new GeoCentroidAggregation('foo'); 29 | $aggregation->getArray(); 30 | } 31 | 32 | /** 33 | * Tests getType method. 34 | */ 35 | public function testGeoCentroidAggregationGetType() 36 | { 37 | $aggregation = new GeoCentroidAggregation('foo'); 38 | $this->assertEquals('geo_centroid', $aggregation->getType()); 39 | } 40 | 41 | /** 42 | * Tests getArray method. 43 | */ 44 | public function testGeoCentroidAggregationGetArray() 45 | { 46 | $aggregation = new GeoCentroidAggregation('foo', 'location'); 47 | $this->assertEquals(['field' => 'location'], $aggregation->getArray()); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /tests/Unit/Aggregation/Metric/PercentilesAggregationTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Metric\Aggregation; 13 | 14 | use ONGR\ElasticsearchDSL\Aggregation\Metric\PercentilesAggregation; 15 | 16 | class PercentilesAggregationTest extends \PHPUnit\Framework\TestCase 17 | { 18 | /** 19 | * Tests if PercentilesAggregation#getArray throws exception when expected. 20 | * 21 | * @expectedException \LogicException 22 | * @expectedExceptionMessage Percentiles aggregation must have field or script set. 23 | */ 24 | public function testPercentilesAggregationGetArrayException() 25 | { 26 | $aggregation = new PercentilesAggregation('bar'); 27 | $aggregation->getArray(); 28 | } 29 | 30 | /** 31 | * Test getType method. 32 | */ 33 | public function testGetType() 34 | { 35 | $aggregation = new PercentilesAggregation('bar'); 36 | $this->assertEquals('percentiles', $aggregation->getType()); 37 | } 38 | 39 | /** 40 | * Test getArray method. 41 | */ 42 | public function testGetArray() 43 | { 44 | $aggregation = new PercentilesAggregation('bar', 'fieldValue', ['percentsValue']); 45 | $this->assertSame( 46 | [ 47 | 'percents' => ['percentsValue'], 48 | 'field' => 'fieldValue', 49 | ], 50 | $aggregation->getArray() 51 | ); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /tests/Unit/Aggregation/Metric/StatsAggregationTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Metric\Aggregation; 13 | 14 | use ONGR\ElasticsearchDSL\Aggregation\Metric\StatsAggregation; 15 | 16 | class StatsAggregationTest extends \PHPUnit\Framework\TestCase 17 | { 18 | /** 19 | * Test for stats aggregation toArray() method. 20 | */ 21 | public function testToArray() 22 | { 23 | $aggregation = new StatsAggregation('test_agg'); 24 | $aggregation->setField('test_field'); 25 | 26 | $expectedResult = [ 27 | 'stats' => ['field' => 'test_field'], 28 | ]; 29 | 30 | $this->assertEquals($expectedResult, $aggregation->toArray()); 31 | } 32 | 33 | /** 34 | * Tests if parameter can be passed to constructor. 35 | */ 36 | public function testConstructor() 37 | { 38 | $aggregation = new StatsAggregation('foo', 'fieldValue', 'scriptValue'); 39 | $this->assertSame( 40 | [ 41 | 'stats' => [ 42 | 'field' => 'fieldValue', 43 | 'script' => 'scriptValue', 44 | ], 45 | ], 46 | $aggregation->toArray() 47 | ); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /tests/Unit/Aggregation/Pipeline/AvgBucketAggregationTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Aggregation\Pipeline; 13 | 14 | use ONGR\ElasticsearchDSL\Aggregation\Pipeline\AvgBucketAggregation; 15 | 16 | /** 17 | * Unit test for avg_bucket aggregation. 18 | */ 19 | class AvgBucketAggregationTest extends \PHPUnit\Framework\TestCase 20 | { 21 | /** 22 | * Tests getArray method. 23 | */ 24 | public function testGetArray() 25 | { 26 | $aggregation = new AvgBucketAggregation('foo', 'foo>bar'); 27 | 28 | $this->assertEquals(['buckets_path' => 'foo>bar'], $aggregation->getArray()); 29 | } 30 | 31 | /** 32 | * Tests getType method. 33 | */ 34 | public function testAvgBucketAggregationGetType() 35 | { 36 | $aggregation = new AvgBucketAggregation('foo', 'foo>bar'); 37 | $this->assertEquals('avg_bucket', $aggregation->getType()); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /tests/Unit/Aggregation/Pipeline/CumulativeSumAggregationTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Aggregation\Pipeline; 13 | 14 | use ONGR\ElasticsearchDSL\Aggregation\Pipeline\CumulativeSumAggregation; 15 | 16 | /** 17 | * Unit test for cumulative sum aggregation. 18 | */ 19 | class CumulativeSumAggregationTest extends \PHPUnit\Framework\TestCase 20 | { 21 | /** 22 | * Tests toArray method. 23 | */ 24 | public function testToArray() 25 | { 26 | $aggregation = new CumulativeSumAggregation('acme', 'test'); 27 | 28 | $expected = [ 29 | 'cumulative_sum' => [ 30 | 'buckets_path' => 'test', 31 | ], 32 | ]; 33 | 34 | $this->assertEquals($expected, $aggregation->toArray()); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /tests/Unit/Aggregation/Pipeline/DerivativeAggregationTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Aggregation\Pipeline; 13 | 14 | use ONGR\ElasticsearchDSL\Aggregation\Pipeline\DerivativeAggregation; 15 | 16 | /** 17 | * Unit test for derivative aggregation. 18 | */ 19 | class DerivativeAggregationTest extends \PHPUnit\Framework\TestCase 20 | { 21 | /** 22 | * Tests toArray method. 23 | */ 24 | public function testToArray() 25 | { 26 | $aggregation = new DerivativeAggregation('foo', 'foo>bar'); 27 | $aggregation->addParameter('gap_policy', 'skip'); 28 | 29 | $expected = [ 30 | 'derivative' => [ 31 | 'buckets_path' => 'foo>bar', 32 | 'gap_policy' => 'skip' 33 | ] 34 | ]; 35 | 36 | $this->assertEquals($expected, $aggregation->toArray()); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /tests/Unit/Aggregation/Pipeline/ExtendedStatsBucketAggregationTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Aggregation\Pipeline; 13 | 14 | use ONGR\ElasticsearchDSL\Aggregation\Pipeline\ExtendedStatsBucketAggregation; 15 | 16 | /** 17 | * Unit test for stats bucket aggregation. 18 | */ 19 | class ExtendedStatsBucketAggregationTest extends \PHPUnit\Framework\TestCase 20 | { 21 | /** 22 | * Tests toArray method. 23 | */ 24 | public function testToArray() 25 | { 26 | $aggregation = new ExtendedStatsBucketAggregation('acme', 'test'); 27 | 28 | $expected = [ 29 | 'extended_stats_bucket' => [ 30 | 'buckets_path' => 'test', 31 | ], 32 | ]; 33 | 34 | $this->assertEquals($expected, $aggregation->toArray()); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /tests/Unit/Aggregation/Pipeline/MaxBucketAggregationTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Aggregation\Pipeline; 13 | 14 | use ONGR\ElasticsearchDSL\Aggregation\Pipeline\MaxBucketAggregation; 15 | 16 | /** 17 | * Unit test for max bucket aggregation. 18 | */ 19 | class MaxBucketAggregationTest extends \PHPUnit\Framework\TestCase 20 | { 21 | /** 22 | * Tests toArray method. 23 | */ 24 | public function testToArray() 25 | { 26 | $aggregation = new MaxBucketAggregation('acme', 'test'); 27 | 28 | $expected = [ 29 | 'max_bucket' => [ 30 | 'buckets_path' => 'test', 31 | ], 32 | ]; 33 | 34 | $this->assertEquals($expected, $aggregation->toArray()); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /tests/Unit/Aggregation/Pipeline/MinBucketAggregationTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Aggregation\Pipeline; 13 | 14 | use ONGR\ElasticsearchDSL\Aggregation\Pipeline\MinBucketAggregation; 15 | 16 | /** 17 | * Unit test for min bucket aggregation. 18 | */ 19 | class MinBucketAggregationTest extends \PHPUnit\Framework\TestCase 20 | { 21 | /** 22 | * Tests toArray method. 23 | */ 24 | public function testToArray() 25 | { 26 | $aggregation = new MinBucketAggregation('acme', 'test'); 27 | 28 | $expected = [ 29 | 'min_bucket' => [ 30 | 'buckets_path' => 'test', 31 | ], 32 | ]; 33 | 34 | $this->assertEquals($expected, $aggregation->toArray()); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /tests/Unit/Aggregation/Pipeline/MovingFunctionAggregationTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Aggregation\Pipeline; 13 | 14 | use ONGR\ElasticsearchDSL\Aggregation\Pipeline\MovingFunctionAggregation; 15 | 16 | /** 17 | * Unit test for sum bucket aggregation. 18 | */ 19 | class MovingFunctionAggregationTest extends \PHPUnit\Framework\TestCase 20 | { 21 | /** 22 | * Tests toArray method. 23 | */ 24 | public function testToArray() 25 | { 26 | $aggregation = new MovingFunctionAggregation('acme', 'test'); 27 | 28 | $expected = [ 29 | 'moving_fn' => [ 30 | 'buckets_path' => 'test', 31 | ], 32 | ]; 33 | 34 | $this->assertEquals($expected, $aggregation->toArray()); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /tests/Unit/Aggregation/Pipeline/PercentilesBucketAggregationTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Aggregation\Pipeline; 13 | 14 | use ONGR\ElasticsearchDSL\Aggregation\Pipeline\PercentilesBucketAggregation; 15 | 16 | /** 17 | * Unit test for percentiles bucket aggregation. 18 | */ 19 | class PercentilesBucketAggregationTest extends \PHPUnit\Framework\TestCase 20 | { 21 | /** 22 | * Tests toArray method. 23 | */ 24 | public function testToArray() 25 | { 26 | $aggregation = new PercentilesBucketAggregation('acme', 'test'); 27 | $aggregation->setPercents([25.0, 50.0, 75.0]); 28 | 29 | $expected = [ 30 | 'percentiles_bucket' => [ 31 | 'buckets_path' => 'test', 32 | 'percents' => [25.0, 50.0, 75.0], 33 | ], 34 | ]; 35 | 36 | $this->assertEquals($expected, $aggregation->toArray()); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /tests/Unit/Aggregation/Pipeline/SerialDifferencingAggregationTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Aggregation\Pipeline; 13 | 14 | use ONGR\ElasticsearchDSL\Aggregation\Pipeline\SerialDifferencingAggregation; 15 | 16 | /** 17 | * Unit test for serial differencing aggregation. 18 | */ 19 | class SerialDifferencingAggregationTest extends \PHPUnit\Framework\TestCase 20 | { 21 | /** 22 | * Tests toArray method. 23 | */ 24 | public function testToArray() 25 | { 26 | $aggregation = new SerialDifferencingAggregation('acme', 'test'); 27 | $aggregation->addParameter('lag', '7'); 28 | 29 | $expected = [ 30 | 'serial_diff' => [ 31 | 'buckets_path' => 'test', 32 | 'lag' => '7' 33 | ], 34 | ]; 35 | 36 | $this->assertEquals($expected, $aggregation->toArray()); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /tests/Unit/Aggregation/Pipeline/StatsBucketAggregationTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Aggregation\Pipeline; 13 | 14 | use ONGR\ElasticsearchDSL\Aggregation\Pipeline\StatsBucketAggregation; 15 | 16 | /** 17 | * Unit test for stats bucket aggregation. 18 | */ 19 | class StatsBucketAggregationTest extends \PHPUnit\Framework\TestCase 20 | { 21 | /** 22 | * Tests toArray method. 23 | */ 24 | public function testToArray() 25 | { 26 | $aggregation = new StatsBucketAggregation('acme', 'test'); 27 | 28 | $expected = [ 29 | 'stats_bucket' => [ 30 | 'buckets_path' => 'test', 31 | ], 32 | ]; 33 | 34 | $this->assertEquals($expected, $aggregation->toArray()); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /tests/Unit/Aggregation/Pipeline/SumBucketAggregationTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Aggregation\Pipeline; 13 | 14 | use ONGR\ElasticsearchDSL\Aggregation\Pipeline\SumBucketAggregation; 15 | 16 | /** 17 | * Unit test for sum bucket aggregation. 18 | */ 19 | class SumBucketAggregationTest extends \PHPUnit\Framework\TestCase 20 | { 21 | /** 22 | * Tests toArray method. 23 | */ 24 | public function testToArray() 25 | { 26 | $aggregation = new SumBucketAggregation('acme', 'test'); 27 | 28 | $expected = [ 29 | 'sum_bucket' => [ 30 | 'buckets_path' => 'test', 31 | ], 32 | ]; 33 | 34 | $this->assertEquals($expected, $aggregation->toArray()); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /tests/Unit/InnerHit/ParentInnerHitTest.php: -------------------------------------------------------------------------------- 1 | addQuery($query); 16 | 17 | 18 | $hit = new ParentInnerHit('test', 'acme', $search); 19 | $expected = [ 20 | 'type' => [ 21 | 'acme' => [ 22 | 'query' => $query->toArray(), 23 | ], 24 | ], 25 | ]; 26 | $this->assertEquals($expected, $hit->toArray()); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /tests/Unit/ParametersTraitTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit; 13 | 14 | use ONGR\ElasticsearchDSL\ParametersTrait; 15 | 16 | /** 17 | * Test for ParametersTrait. 18 | */ 19 | class ParametersTraitTest extends \PHPUnit\Framework\TestCase 20 | { 21 | /** 22 | * @var ParametersTrait 23 | */ 24 | private $parametersTraitMock; 25 | 26 | /** 27 | * {@inheritdoc} 28 | */ 29 | public function setUp() 30 | { 31 | $this->parametersTraitMock = $this->getMockForTrait('ONGR\ElasticsearchDSL\ParametersTrait'); 32 | } 33 | 34 | /** 35 | * Tests addParameter method. 36 | */ 37 | public function testGetAndAddParameter() 38 | { 39 | $this->parametersTraitMock->addParameter('acme', 123); 40 | $this->assertEquals(123, $this->parametersTraitMock->getParameter('acme')); 41 | $this->parametersTraitMock->addParameter('bar', 321); 42 | $this->assertEquals(321, $this->parametersTraitMock->getParameter('bar')); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /tests/Unit/Query/Compound/BoostingQueryTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\Compound; 13 | 14 | use ONGR\ElasticsearchDSL\Query\Compound\BoostingQuery; 15 | 16 | class BoostingQueryTest extends \PHPUnit\Framework\TestCase 17 | { 18 | /** 19 | * Tests toArray(). 20 | */ 21 | public function testToArray() 22 | { 23 | $mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\BuilderInterface')->getMock(); 24 | $mock 25 | ->expects($this->any()) 26 | ->method('toArray') 27 | ->willReturn(['term' => ['foo' => 'bar']]); 28 | 29 | $query = new BoostingQuery($mock, $mock, 0.2); 30 | $expected = [ 31 | 'boosting' => [ 32 | 'positive' => ['term' => ['foo' => 'bar']], 33 | 'negative' => ['term' => ['foo' => 'bar']], 34 | 'negative_boost' => 0.2, 35 | ], 36 | ]; 37 | 38 | $this->assertEquals($expected, $query->toArray()); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /tests/Unit/Query/Compound/ConstantScoreQueryTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\Compound; 13 | 14 | use ONGR\ElasticsearchDSL\Query\Compound\ConstantScoreQuery; 15 | 16 | class ConstantScoreQueryTest extends \PHPUnit\Framework\TestCase 17 | { 18 | /** 19 | * Tests toArray(). 20 | */ 21 | public function testToArray() 22 | { 23 | $mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\BuilderInterface')->getMock(); 24 | $mock 25 | ->expects($this->any()) 26 | ->method('toArray') 27 | ->willReturn(['term' => ['foo' => 'bar']]); 28 | 29 | $query = new ConstantScoreQuery($mock, ['boost' => 1.2]); 30 | $expected = [ 31 | 'constant_score' => [ 32 | 'filter' => [ 33 | 'term' => ['foo' => 'bar'] 34 | ], 35 | 'boost' => 1.2, 36 | ], 37 | ]; 38 | 39 | $this->assertEquals($expected, $query->toArray()); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /tests/Unit/Query/Compound/DisMaxQueryTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\Compound; 13 | 14 | use ONGR\ElasticsearchDSL\Query\Compound\DisMaxQuery; 15 | 16 | class DisMaxQueryTest extends \PHPUnit\Framework\TestCase 17 | { 18 | /** 19 | * Tests toArray(). 20 | */ 21 | public function testToArray() 22 | { 23 | $mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\BuilderInterface')->getMock(); 24 | $mock 25 | ->expects($this->any()) 26 | ->method('toArray') 27 | ->willReturn(['term' => ['foo' => 'bar']]); 28 | 29 | $query = new DisMaxQuery(['boost' => 1.2]); 30 | $query->addQuery($mock); 31 | $query->addQuery($mock); 32 | $expected = [ 33 | 'dis_max' => [ 34 | 'queries' => [ 35 | ['term' => ['foo' => 'bar']], 36 | ['term' => ['foo' => 'bar']], 37 | ], 38 | 'boost' => 1.2, 39 | ], 40 | ]; 41 | 42 | $this->assertEquals($expected, $query->toArray()); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /tests/Unit/Query/FullText/CommonTermsQueryTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\FullText; 13 | 14 | use ONGR\ElasticsearchDSL\Query\FullText\CommonTermsQuery; 15 | 16 | class CommonTermsQueryTest extends \PHPUnit\Framework\TestCase 17 | { 18 | /** 19 | * Tests toArray(). 20 | */ 21 | public function testToArray() 22 | { 23 | $query = new CommonTermsQuery('body', 'this is bonsai cool', ['cutoff_frequency' => 0.01]); 24 | $expected = [ 25 | 'common' => [ 26 | 'body' => [ 27 | 'query' => 'this is bonsai cool', 28 | 'cutoff_frequency' => 0.01, 29 | ], 30 | ], 31 | ]; 32 | 33 | $this->assertEquals($expected, $query->toArray()); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /tests/Unit/Query/FullText/MatchPhrasePrefixQueryTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\FullText; 13 | 14 | use ONGR\ElasticsearchDSL\Query\FullText\MatchPhrasePrefixQuery; 15 | use PHPUnit\Framework\TestCase; 16 | 17 | class MatchPhrasePrefixQueryTest extends \PHPUnit\Framework\TestCase 18 | { 19 | /** 20 | * Tests toArray(). 21 | */ 22 | public function testToArray() 23 | { 24 | $query = new MatchPhrasePrefixQuery('message', 'this is a test'); 25 | $expected = [ 26 | 'match_phrase_prefix' => [ 27 | 'message' => [ 28 | 'query' => 'this is a test', 29 | ], 30 | ], 31 | ]; 32 | 33 | $this->assertEquals($expected, $query->toArray()); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /tests/Unit/Query/FullText/MatchPhraseQueryTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\FullText; 13 | 14 | use ONGR\ElasticsearchDSL\Query\FullText\MatchPhraseQuery; 15 | use PHPUnit\Framework\TestCase; 16 | 17 | class MatchPhraseQueryTest extends \PHPUnit\Framework\TestCase 18 | { 19 | /** 20 | * Tests toArray(). 21 | */ 22 | public function testToArray() 23 | { 24 | $query = new MatchPhraseQuery('message', 'this is a test'); 25 | $expected = [ 26 | 'match_phrase' => [ 27 | 'message' => [ 28 | 'query' => 'this is a test', 29 | ], 30 | ], 31 | ]; 32 | 33 | $this->assertEquals($expected, $query->toArray()); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /tests/Unit/Query/FullText/MatchQueryTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\FullText; 13 | 14 | use ONGR\ElasticsearchDSL\Query\FullText\MatchQuery; 15 | 16 | class MatchQueryTest extends \PHPUnit\Framework\TestCase 17 | { 18 | /** 19 | * Tests toArray(). 20 | */ 21 | public function testToArray() 22 | { 23 | $query = new MatchQuery('message', 'this is a test'); 24 | $expected = [ 25 | 'match' => [ 26 | 'message' => [ 27 | 'query' => 'this is a test', 28 | ], 29 | ], 30 | ]; 31 | 32 | $this->assertEquals($expected, $query->toArray()); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /tests/Unit/Query/FullText/MultiMatchQueryTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\FullText; 13 | 14 | use ONGR\ElasticsearchDSL\Query\FullText\MultiMatchQuery; 15 | 16 | class MultiMatchQueryTest extends \PHPUnit\Framework\TestCase 17 | { 18 | /** 19 | * Tests toArray(). 20 | */ 21 | public function testToArray() 22 | { 23 | $query = new MultiMatchQuery(['message', 'title'], 'this is a test'); 24 | $expected = [ 25 | 'multi_match' => [ 26 | 'query' => 'this is a test', 27 | 'fields' => ['message', 'title'], 28 | ], 29 | ]; 30 | 31 | $this->assertEquals($expected, $query->toArray()); 32 | } 33 | 34 | /** 35 | * Tests multi-match query with no fields. 36 | */ 37 | public function testToArrayWithNoFields() 38 | { 39 | $query = new MultiMatchQuery([], 'this is a test'); 40 | $expected = [ 41 | 'multi_match' => [ 42 | 'query' => 'this is a test', 43 | ], 44 | ]; 45 | 46 | $this->assertEquals($expected, $query->toArray()); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /tests/Unit/Query/FullText/QueryStringQueryTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\FullText; 13 | 14 | use ONGR\ElasticsearchDSL\Query\FullText\QueryStringQuery; 15 | 16 | class QueryStringQueryTest extends \PHPUnit\Framework\TestCase 17 | { 18 | /** 19 | * Tests toArray(). 20 | */ 21 | public function testToArray() 22 | { 23 | $query = new QueryStringQuery('this AND that OR thus'); 24 | $expected = [ 25 | 'query_string' => [ 26 | 'query' => 'this AND that OR thus', 27 | ] 28 | ]; 29 | 30 | $this->assertEquals($expected, $query->toArray()); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /tests/Unit/Query/FullText/SimpleQueryStringQueryTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\FullText; 13 | 14 | use ONGR\ElasticsearchDSL\Query\FullText\SimpleQueryStringQuery; 15 | 16 | class SimpleQueryStringQueryTest extends \PHPUnit\Framework\TestCase 17 | { 18 | /** 19 | * Tests toArray(). 20 | */ 21 | public function testToArray() 22 | { 23 | $query = new SimpleQueryStringQuery('"fried eggs" +(eggplant | potato) -frittata'); 24 | $expected = [ 25 | 'simple_query_string' => [ 26 | 'query' => '"fried eggs" +(eggplant | potato) -frittata', 27 | ], 28 | ]; 29 | 30 | $this->assertEquals($expected, $query->toArray()); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /tests/Unit/Query/Joining/HasChildQueryTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\Joining; 13 | 14 | use ONGR\ElasticsearchDSL\Query\Joining\HasChildQuery; 15 | 16 | class HasChildQueryTest extends \PHPUnit\Framework\TestCase 17 | { 18 | /** 19 | * Tests whether __constructor calls setParameters method. 20 | */ 21 | public function testConstructor() 22 | { 23 | $parentQuery = $this->getMockBuilder('ONGR\ElasticsearchDSL\BuilderInterface')->getMock(); 24 | $query = new HasChildQuery('test_type', $parentQuery, ['test_parameter1']); 25 | $this->assertEquals(['test_parameter1'], $query->getParameters()); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /tests/Unit/Query/Joining/HasParentQueryTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\Joining; 13 | 14 | use ONGR\ElasticsearchDSL\Query\Joining\HasParentQuery; 15 | 16 | class HasParentQueryTest extends \PHPUnit\Framework\TestCase 17 | { 18 | /** 19 | * Tests whether __constructor calls setParameters method. 20 | */ 21 | public function testConstructor() 22 | { 23 | $parentQuery = $this->getMockBuilder('ONGR\ElasticsearchDSL\BuilderInterface')->getMock(); 24 | $query = new HasParentQuery('test_type', $parentQuery, ['test_parameter1']); 25 | $this->assertEquals(['test_parameter1'], $query->getParameters()); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /tests/Unit/Query/MatchAllQueryTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Query; 13 | 14 | use ONGR\ElasticsearchDSL\Query\MatchAllQuery; 15 | 16 | class MatchAllQueryTest extends \PHPUnit\Framework\TestCase 17 | { 18 | /** 19 | * Tests toArray(). 20 | */ 21 | public function testToArrayWhenThereAreNoParams() 22 | { 23 | $query = new MatchAllQuery(); 24 | $this->assertEquals(['match_all' => new \stdClass()], $query->toArray()); 25 | } 26 | 27 | /** 28 | * Tests toArray(). 29 | */ 30 | public function testToArrayWithParams() 31 | { 32 | $params = ['boost' => 5]; 33 | $query = new MatchAllQuery($params); 34 | $this->assertEquals(['match_all' => $params], $query->toArray()); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /tests/Unit/Query/Span/SpanFirstQueryTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\Span; 13 | 14 | use ONGR\ElasticsearchDSL\Query\Span\SpanFirstQuery; 15 | 16 | /** 17 | * Unit test for SpanFirstQuery. 18 | */ 19 | class SpanFirstQueryTest extends \PHPUnit\Framework\TestCase 20 | { 21 | /** 22 | * Tests for toArray(). 23 | */ 24 | public function testToArray() 25 | { 26 | $mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface')->getMock(); 27 | $mock 28 | ->expects($this->once()) 29 | ->method('toArray') 30 | ->willReturn(['span_term' => ['user' => 'bob']]); 31 | 32 | $query = new SpanFirstQuery($mock, 5); 33 | $result = [ 34 | 'span_first' => [ 35 | 'match' => [ 36 | 'span_term' => ['user' => 'bob'], 37 | ], 38 | 'end' => 5, 39 | ], 40 | ]; 41 | $this->assertEquals($result, $query->toArray()); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /tests/Unit/Query/Span/SpanMultiTermQueryTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\Span; 13 | 14 | use ONGR\ElasticsearchDSL\Query\Span\SpanMultiTermQuery; 15 | 16 | /** 17 | * Unit test for SpanMultiTermQuery. 18 | */ 19 | class SpanMultiTermQueryTest extends \PHPUnit\Framework\TestCase 20 | { 21 | /** 22 | * Test for toArray(). 23 | */ 24 | public function testToArray() 25 | { 26 | $mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\BuilderInterface')->getMock(); 27 | $mock 28 | ->expects($this->once()) 29 | ->method('toArray') 30 | ->willReturn(['prefix' => ['user' => ['value' => 'ki']]]); 31 | 32 | $query = new SpanMultiTermQuery($mock); 33 | $expected = [ 34 | 'span_multi' => [ 35 | 'match' => [ 36 | 'prefix' => ['user' => ['value' => 'ki']], 37 | ], 38 | ], 39 | ]; 40 | 41 | $this->assertEquals($expected, $query->toArray()); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /tests/Unit/Query/Span/SpanNearQueryTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\Span; 13 | 14 | use ONGR\ElasticsearchDSL\Query\Span\SpanNearQuery; 15 | 16 | /** 17 | * Unit test for SpanNearQuery. 18 | */ 19 | class SpanNearQueryTest extends \PHPUnit\Framework\TestCase 20 | { 21 | /** 22 | * Tests for toArray(). 23 | */ 24 | public function testToArray() 25 | { 26 | $mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface')->getMock(); 27 | $mock 28 | ->expects($this->once()) 29 | ->method('toArray') 30 | ->willReturn(['span_term' => ['key' => 'value']]); 31 | 32 | $query = new SpanNearQuery(['in_order' => false]); 33 | $query->setSlop(5); 34 | $query->addQuery($mock); 35 | $result = [ 36 | 'span_near' => [ 37 | 'clauses' => [ 38 | 0 => [ 39 | 'span_term' => [ 40 | 'key' => 'value', 41 | ], 42 | ], 43 | ], 44 | 'slop' => 5, 45 | 'in_order' => false, 46 | ], 47 | ]; 48 | $this->assertEquals($result, $query->toArray()); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /tests/Unit/Query/Span/SpanNotQueryTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\Span; 13 | 14 | use ONGR\ElasticsearchDSL\Query\Span\SpanNotQuery; 15 | 16 | /** 17 | * Unit test for SpanNotQuery. 18 | */ 19 | class SpanNotQueryTest extends \PHPUnit\Framework\TestCase 20 | { 21 | /** 22 | * Tests for toArray(). 23 | */ 24 | public function testSpanNotQueryToArray() 25 | { 26 | $mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface')->getMock(); 27 | $mock 28 | ->expects($this->exactly(2)) 29 | ->method('toArray') 30 | ->willReturn(['span_term' => ['key' => 'value']]); 31 | 32 | $query = new SpanNotQuery($mock, $mock); 33 | $result = [ 34 | 'span_not' => [ 35 | 'include' => [ 36 | 'span_term' => ['key' => 'value'], 37 | ], 38 | 'exclude' => [ 39 | 'span_term' => ['key' => 'value'], 40 | ], 41 | ], 42 | ]; 43 | $this->assertEquals($result, $query->toArray()); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /tests/Unit/Query/Span/SpanOrQueryTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\Span; 13 | 14 | use ONGR\ElasticsearchDSL\Query\Span\SpanOrQuery; 15 | 16 | /** 17 | * Unit test for SpanOrQuery. 18 | */ 19 | class SpanOrQueryTest extends \PHPUnit\Framework\TestCase 20 | { 21 | /** 22 | * Tests for toArray(). 23 | */ 24 | public function testToArray() 25 | { 26 | $mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface')->getMock(); 27 | $mock 28 | ->expects($this->once()) 29 | ->method('toArray') 30 | ->willReturn(['span_term' => ['key' => 'value']]); 31 | 32 | $query = new SpanOrQuery(); 33 | $query->addQuery($mock); 34 | $result = [ 35 | 'span_or' => [ 36 | 'clauses' => [ 37 | 0 => [ 38 | 'span_term' => ['key' => 'value'], 39 | ], 40 | ], 41 | ], 42 | ]; 43 | $this->assertEquals($result, $query->toArray()); 44 | 45 | $result = $query->getQueries(); 46 | $this->assertInternalType('array', $result); 47 | $this->assertEquals(1, count($result)); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /tests/Unit/Query/Span/SpanTermQueryTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\Span; 13 | 14 | use ONGR\ElasticsearchDSL\Query\Span\SpanTermQuery; 15 | 16 | /** 17 | * Unit test for SpanTermQuery. 18 | */ 19 | class SpanTermQueryTest extends \PHPUnit\Framework\TestCase 20 | { 21 | /** 22 | * Tests for toArray(). 23 | */ 24 | public function testToArray() 25 | { 26 | $query = new SpanTermQuery('user', 'bob'); 27 | $expected = [ 28 | 'span_term' => ['user' => 'bob'], 29 | ]; 30 | 31 | $this->assertEquals($expected, $query->toArray()); 32 | } 33 | 34 | /** 35 | * Tests for toArray() with parameters. 36 | */ 37 | public function testToArrayWithParameters() 38 | { 39 | $query = new SpanTermQuery('user', 'bob', ['boost' => 2]); 40 | $expected = [ 41 | 'span_term' => ['user' => ['value' => 'bob', 'boost' => 2]], 42 | ]; 43 | 44 | $this->assertEquals($expected, $query->toArray()); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /tests/Unit/Query/Span/SpanWithinQueryTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\Span; 13 | 14 | use ONGR\ElasticsearchDSL\Query\Span\SpanWithinQuery; 15 | 16 | /** 17 | * Unit test for SpanWithinQuery. 18 | */ 19 | class SpanWithinQueryTest extends \PHPUnit\Framework\TestCase 20 | { 21 | /** 22 | * Tests for toArray(). 23 | */ 24 | public function testToArray() 25 | { 26 | $query = new SpanWithinQuery( 27 | $this->getSpanQueryMock('foo'), 28 | $this->getSpanQueryMock('bar') 29 | ); 30 | $result = [ 31 | 'span_within' => [ 32 | 'little' => [ 33 | 'span_term' => ['user' => 'foo'], 34 | ], 35 | 'big' => [ 36 | 'span_term' => ['user' => 'bar'], 37 | ], 38 | ], 39 | ]; 40 | $this->assertEquals($result, $query->toArray()); 41 | } 42 | 43 | /** 44 | * @param string $value 45 | * 46 | * @returns \PHPUnit_Framework_MockObject_MockObject 47 | */ 48 | private function getSpanQueryMock($value) 49 | { 50 | $mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface')->getMock(); 51 | $mock 52 | ->expects($this->once()) 53 | ->method('toArray') 54 | ->willReturn(['span_term' => ['user' => $value]]); 55 | return $mock; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /tests/Unit/Query/Specialized/MoreLikeThisQueryTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\Specialized; 13 | 14 | use ONGR\ElasticsearchDSL\Query\Specialized\MoreLikeThisQuery; 15 | 16 | class MoreLikeThisQueryTest extends \PHPUnit\Framework\TestCase 17 | { 18 | /** 19 | * Tests toArray(). 20 | */ 21 | public function testToArray() 22 | { 23 | $query = new MoreLikeThisQuery('this is a test', ['fields' => ['title', 'description']]); 24 | $expected = [ 25 | 'more_like_this' => [ 26 | 'fields' => ['title', 'description'], 27 | 'like' => 'this is a test', 28 | ], 29 | ]; 30 | 31 | $this->assertEquals($expected, $query->toArray()); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /tests/Unit/Query/TermLevel/ExistsQueryTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\TermLevel; 13 | 14 | use ONGR\ElasticsearchDSL\Query\TermLevel\ExistsQuery; 15 | 16 | /** 17 | * Unit test for ExistsQuery. 18 | */ 19 | class ExistsQueryTest extends \PHPUnit\Framework\TestCase 20 | { 21 | /** 22 | * Tests toArray() method. 23 | */ 24 | public function testToArray() 25 | { 26 | $query = new ExistsQuery('bar'); 27 | $this->assertEquals(['exists' => ['field' => 'bar']], $query->toArray()); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /tests/Unit/Query/TermLevel/FuzzyQueryTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\TermLevel; 13 | 14 | use ONGR\ElasticsearchDSL\Query\TermLevel\FuzzyQuery; 15 | 16 | class FuzzyQueryTest extends \PHPUnit\Framework\TestCase 17 | { 18 | /** 19 | * Tests toArray(). 20 | */ 21 | public function testToArray() 22 | { 23 | $query = new FuzzyQuery('user', 'ki', ['boost' => 1.2]); 24 | $expected = [ 25 | 'fuzzy' => [ 26 | 'user' => [ 27 | 'value' => 'ki', 28 | 'boost' => 1.2, 29 | ], 30 | ], 31 | ]; 32 | 33 | $this->assertEquals($expected, $query->toArray()); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /tests/Unit/Query/TermLevel/IdsQueryTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\TermLevel; 13 | 14 | use ONGR\ElasticsearchDSL\Query\TermLevel\IdsQuery; 15 | 16 | class IdsQueryTest extends \PHPUnit\Framework\TestCase 17 | { 18 | /** 19 | * Tests toArray(). 20 | */ 21 | public function testToArray() 22 | { 23 | $query = new IdsQuery(['foo', 'bar']); 24 | $expected = [ 25 | 'ids' => [ 26 | 'values' => ['foo', 'bar'], 27 | ], 28 | ]; 29 | 30 | $this->assertEquals($expected, $query->toArray()); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /tests/Unit/Query/TermLevel/PrefixQueryTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\TermLevel; 13 | 14 | use ONGR\ElasticsearchDSL\Query\TermLevel\PrefixQuery; 15 | 16 | class PrefixQueryTest extends \PHPUnit\Framework\TestCase 17 | { 18 | /** 19 | * Tests toArray(). 20 | */ 21 | public function testToArray() 22 | { 23 | $query = new PrefixQuery('user', 'ki'); 24 | $expected = [ 25 | 'prefix' => [ 26 | 'user' => [ 27 | 'value' => 'ki', 28 | ], 29 | ], 30 | ]; 31 | 32 | $this->assertEquals($expected, $query->toArray()); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /tests/Unit/Query/TermLevel/RangeQueryTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\TermLevel; 13 | 14 | use ONGR\ElasticsearchDSL\Query\TermLevel\RangeQuery; 15 | 16 | class RangeQueryTest extends \PHPUnit\Framework\TestCase 17 | { 18 | /** 19 | * Tests toArray(). 20 | */ 21 | public function testToArray() 22 | { 23 | $query = new RangeQuery('age', ['gte' => 10, 'lte' => 20]); 24 | $expected = [ 25 | 'range' => [ 26 | 'age' => [ 27 | 'gte' => 10, 28 | 'lte' => 20, 29 | ], 30 | ] 31 | ]; 32 | 33 | $this->assertEquals($expected, $query->toArray()); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /tests/Unit/Query/TermLevel/RegexpQueryTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\TermLevel; 13 | 14 | use ONGR\ElasticsearchDSL\Query\TermLevel\RegexpQuery; 15 | 16 | class RegexpQueryTest extends \PHPUnit\Framework\TestCase 17 | { 18 | /** 19 | * Tests toArray(). 20 | */ 21 | public function testToArray() 22 | { 23 | $query = new RegexpQuery('user', 's.*y'); 24 | $expected = [ 25 | 'regexp' => [ 26 | 'user' => [ 27 | 'value' => 's.*y', 28 | ], 29 | ], 30 | ]; 31 | 32 | $this->assertEquals($expected, $query->toArray()); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /tests/Unit/Query/TermLevel/TermQueryTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\TermLevel; 13 | 14 | use ONGR\ElasticsearchDSL\Query\TermLevel\TermQuery; 15 | 16 | class TermQueryTest extends \PHPUnit\Framework\TestCase 17 | { 18 | /** 19 | * Tests toArray(). 20 | */ 21 | public function testToArray() 22 | { 23 | $query = new TermQuery('user', 'bob'); 24 | $expected = [ 25 | 'term' => [ 26 | 'user' => 'bob', 27 | ], 28 | ]; 29 | 30 | $this->assertEquals($expected, $query->toArray()); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /tests/Unit/Query/TermLevel/TermsQueryTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\TermLevel; 13 | 14 | use ONGR\ElasticsearchDSL\Query\TermLevel\TermsQuery; 15 | 16 | class TermsQueryTest extends \PHPUnit\Framework\TestCase 17 | { 18 | /** 19 | * Tests toArray(). 20 | */ 21 | public function testToArray() 22 | { 23 | $query = new TermsQuery('user', ['bob', 'elasticsearch']); 24 | $expected = [ 25 | 'terms' => [ 26 | 'user' => ['bob', 'elasticsearch'], 27 | ], 28 | ]; 29 | 30 | $this->assertEquals($expected, $query->toArray()); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /tests/Unit/Query/TermLevel/TermsSetQueryTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\TermLevel; 13 | 14 | use ONGR\ElasticsearchDSL\Query\TermLevel\TermsSetQuery; 15 | 16 | class TermsSetQueryTest extends \PHPUnit\Framework\TestCase 17 | { 18 | /** 19 | * Tests toArray(). 20 | */ 21 | public function testToArray() 22 | { 23 | $terms = ['php', 'c++', 'java']; 24 | $parameters = ['minimum_should_match_field' => 'required_matches']; 25 | $query = new TermsSetQuery('programming_languages', $terms, $parameters); 26 | $expected = [ 27 | 'terms_set' => [ 28 | 'programming_languages' => [ 29 | 'terms' => ['php', 'c++', 'java'], 30 | 'minimum_should_match_field' => 'required_matches', 31 | ] 32 | ], 33 | ]; 34 | 35 | $this->assertEquals($expected, $query->toArray()); 36 | } 37 | 38 | public function testItThrowsAaExceptionWhenMinimumShouldMatchFieldOrMinimumShouldMatchScriptIsNotGiven() 39 | { 40 | $message = "Either minimum_should_match_field or minimum_should_match_script must be set."; 41 | $this->expectException(\InvalidArgumentException::class); 42 | $this->expectExceptionMessage($message); 43 | 44 | $terms = ['php', 'c++', 'java']; 45 | new TermsSetQuery('programming_languages', $terms, []); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /tests/Unit/Query/TermLevel/TypeQueryTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\TermLevel; 13 | 14 | use ONGR\ElasticsearchDSL\Query\TermLevel\TypeQuery; 15 | 16 | class TypeQueryTest extends \PHPUnit\Framework\TestCase 17 | { 18 | /** 19 | * Test for query toArray() method. 20 | */ 21 | public function testToArray() 22 | { 23 | $query = new TypeQuery('foo'); 24 | $expectedResult = [ 25 | 'type' => ['value' => 'foo'] 26 | ]; 27 | 28 | $this->assertEquals($expectedResult, $query->toArray()); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /tests/Unit/Query/TermLevel/WildcardQueryTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\TermLevel; 13 | 14 | use ONGR\ElasticsearchDSL\Query\TermLevel\WildcardQuery; 15 | 16 | class WildcardQueryTest extends \PHPUnit\Framework\TestCase 17 | { 18 | /** 19 | * Test for query toArray() method. 20 | */ 21 | public function testToArray() 22 | { 23 | $query = new WildcardQuery('user', 'ki*y'); 24 | $expectedResult = [ 25 | 'wildcard' => [ 26 | 'user' => [ 27 | 'value' => 'ki*y', 28 | ], 29 | ], 30 | ]; 31 | 32 | $this->assertEquals($expectedResult, $query->toArray()); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /tests/Unit/SearchEndpoint/AggregationsEndpointTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Unit\SearchEndpoint; 13 | 14 | use ONGR\ElasticsearchDSL\Aggregation\Bucketing\MissingAggregation; 15 | use ONGR\ElasticsearchDSL\SearchEndpoint\AggregationsEndpoint; 16 | 17 | /** 18 | * Class AggregationsEndpointTest. 19 | */ 20 | class AggregationsEndpointTest extends \PHPUnit\Framework\TestCase 21 | { 22 | /** 23 | * Tests constructor. 24 | */ 25 | public function testItCanBeInstantiated() 26 | { 27 | $this->assertInstanceOf( 28 | 'ONGR\ElasticsearchDSL\SearchEndpoint\AggregationsEndpoint', 29 | new AggregationsEndpoint() 30 | ); 31 | } 32 | 33 | /** 34 | * Tests if endpoint returns builders. 35 | */ 36 | public function testEndpointGetter() 37 | { 38 | $aggName = 'acme_agg'; 39 | $agg = new MissingAggregation('acme'); 40 | $endpoint = new AggregationsEndpoint(); 41 | $endpoint->add($agg, $aggName); 42 | $builders = $endpoint->getAll(); 43 | 44 | $this->assertCount(1, $builders); 45 | $this->assertSame($agg, $builders[$aggName]); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /tests/Unit/SearchEndpoint/SearchEndpointFactoryTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Unit\SearchEndpoint; 13 | 14 | use ONGR\ElasticsearchDSL\SearchEndpoint\AggregationsEndpoint; 15 | use ONGR\ElasticsearchDSL\SearchEndpoint\SearchEndpointFactory; 16 | 17 | /** 18 | * Unit test class for search endpoint factory. 19 | */ 20 | class SearchEndpointFactoryTest extends \PHPUnit\Framework\TestCase 21 | { 22 | /** 23 | * Tests get method exception. 24 | * 25 | * @expectedException \RuntimeException 26 | */ 27 | public function testGet() 28 | { 29 | SearchEndpointFactory::get('foo'); 30 | } 31 | 32 | /** 33 | * Tests if factory can create endpoint. 34 | */ 35 | public function testFactory() 36 | { 37 | SearchEndpointFactory::get(AggregationsEndpoint::NAME); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /tests/Unit/SearchTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit; 13 | 14 | use ONGR\ElasticsearchDSL\Query\TermLevel\ExistsQuery; 15 | use ONGR\ElasticsearchDSL\Query\TermLevel\TermQuery; 16 | use ONGR\ElasticsearchDSL\Search; 17 | use ONGR\ElasticsearchDSL\Sort\FieldSort; 18 | use ONGR\ElasticsearchDSL\Suggest\Suggest; 19 | 20 | /** 21 | * Test for Search. 22 | */ 23 | class SearchTest extends \PHPUnit\Framework\TestCase 24 | { 25 | /** 26 | * Tests Search constructor. 27 | */ 28 | public function testItCanBeInstantiated() 29 | { 30 | $this->assertInstanceOf('ONGR\ElasticsearchDSL\Search', new Search()); 31 | } 32 | 33 | public function testScrollUriParameter() 34 | { 35 | $search = new Search(); 36 | $search->setScroll('5m'); 37 | 38 | $this->assertArrayHasKey('scroll', $search->getUriParams()); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /tests/Unit/Sort/FieldSortTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ONGR\ElasticsearchDSL\Tests\Unit\Sort; 13 | 14 | use ONGR\ElasticsearchDSL\Query\TermLevel\TermQuery; 15 | use ONGR\ElasticsearchDSL\Sort\FieldSort; 16 | use ONGR\ElasticsearchDSL\Sort\NestedSort; 17 | 18 | class FieldSortTest extends \PHPUnit\Framework\TestCase 19 | { 20 | /** 21 | * Test for toArray() method. 22 | * 23 | */ 24 | public function testToArray() 25 | { 26 | $nestedFilter = new NestedSort('somePath', new TermQuery('somePath.id', 10)); 27 | $sort = new FieldSort('someField', 'asc'); 28 | $sort->setNestedFilter($nestedFilter); 29 | 30 | $expected = [ 31 | 'someField' => [ 32 | 'nested' => [ 33 | 'path' => 'somePath', 34 | 'filter' => [ 35 | 'term' => [ 36 | 'somePath.id' => 10, 37 | ] 38 | ] 39 | ], 40 | 'order' => 'asc' 41 | ], 42 | ]; 43 | $result = $sort->toArray(); 44 | $this->assertEquals($expected, $result); 45 | } 46 | } 47 | --------------------------------------------------------------------------------