Download the PHP package sendynl/opensearch-query-builder without Composer
On this page you can find all versions of the php package sendynl/opensearch-query-builder. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sendynl/opensearch-query-builder
More information about sendynl/opensearch-query-builder
Files in sendynl/opensearch-query-builder
Package opensearch-query-builder
Short Description Build and execute an OpenSearch search query using a fluent PHP API
License MIT
Homepage https://github.com/sendynl/opensearch-query-builder
Informations about the package opensearch-query-builder
Build and execute OpenSearch queries using a fluent PHP API
This package is a lightweight query builder for OpenSearch. It is forked from the spatie/elasticsearch-query-builder library and modified to support OpenSearch. We're always open for PRs if you need anything specific!
Installation
You can install the package via composer:
Basic usage
The only class you really need to interact with is the Sendy\OpenSearchQueryBuilder\Builder class. It requires an \OpenSearch\Client passed in the constructor. Take a look at the OpenSearch SDK docs to learn more about connecting to your OpenSearch cluster.
The Builder class contains some methods to pagination. You can read more about these methods below. Once you've fully built-up the query you can use $builder->search() to execute the query or $builder->getPayload() to get the raw payload for OpenSearch.
Multi-Search Queries
Multi-Search queries are also available using the MultiBuilder class.
Adding queries
The $builder->addQuery() method can be used to add any of the available Query types to the builder. The available query types can be found below or in the src/Queries directory of this repo. Every Query has a static create() method to pass its most important parameters.
The following query types are available:
ExistsQuery
https://docs.opensearch.org/docs/latest/query-dsl/term/exists/
GeoshapeQuery
https://docs.opensearch.org/docs/latest/query-dsl/geo-and-xy/geoshape/
MatchQuery
https://docs.opensearch.org/docs/latest/query-dsl/full-text/match/
MatchPhraseQuery
https://docs.opensearch.org/docs/latest/query-dsl/full-text/match-phrase/
MultiMatchQuery
https://docs.opensearch.org/docs/latest/query-dsl/full-text/multi-match/
NestedQuery
https://docs.opensearch.org/docs/latest/query-dsl/joining/nested/
NestedQuery InnerHits
https://docs.opensearch.org/docs/latest/search-plugins/searching-data/inner-hits/
RangeQuery
https://docs.opensearch.org/docs/latest/query-dsl/term/range/
TermQuery
https://docs.opensearch.org/docs/latest/query-dsl/term/term/
TermsQuery
https://docs.opensearch.org/docs/latest/query-dsl/term/terms/
WildcardQuery
https://docs.opensearch.org/docs/latest/query-dsl/term/wildcard/
PercolateQuery
https://docs.opensearch.org/docs/latest/field-types/supported-field-types/percolator/
BoolQuery
https://docs.opensearch.org/docs/latest/query-dsl/compound/bool/
collapse
The collapse feature allows grouping search results by a specific field while retrieving top documents from each group using inner_hits. This is useful for avoiding duplicate entities in search results while still accessing grouped data.
https://docs.opensearch.org/docs/latest/search-plugins/collapse-search/
Chaining multiple queries
Multiple addQuery() calls can be chained on one Builder. Under the hood they'll be added to a BoolQuery with occurrence type must. By passing a second argument to the addQuery() method you can select a different occurrence type:
More information on the boolean query and its occurrence types can be found in the OpenSearch docs.
Adding aggregations
The $builder->addAggregation() method can be used to add any of the available Aggregations to the builder. The available aggregation types can be found below or in the src/Aggregations directory of this repo. Every Aggregation has a static create() method to pass its most important parameters and sometimes some extra methods.
The following query types are available:
CardinalityAggregation
FilterAggregation
https://docs.opensearch.org/docs/latest/aggregations/bucket/filter/
MaxAggregation
https://docs.opensearch.org/docs/latest/aggregations/metric/maximum/
MinAggregation
https://docs.opensearch.org/docs/latest/aggregations/metric/minimum/
SumAggregation
https://docs.opensearch.org/docs/latest/aggregations/metric/sum/
NestedAggregation
https://docs.opensearch.org/docs/latest/aggregations/bucket/nested/
ReverseNestedAggregation
https://docs.opensearch.org/docs/latest/aggregations/bucket/reverse-nested/
TermsAggregation
https://docs.opensearch.org/docs/latest/aggregations/bucket/terms/
TopHitsAggregation
https://docs.opensearch.org/docs/latest/aggregations/metric/top-hits/
DateHistogramAggregation
https://docs.opensearch.org/docs/latest/aggregations/bucket/date-histogram/
Adding sorts
The Builder (and some aggregations) has a addSort() method that takes a Sort instance to sort the results. You can read more about how sorting works in the OpenSearch docs.
Nested sort
https://docs.opensearch.org/docs/latest/search-plugins/searching-data/sort/#sorting-nested-objects
Nested sort with filter
Retrieve specific fields
The fields() method can be used to request specific fields from the resulting documents without returning the entire _source entry. You can read more about the specifics of the fields parameter in the OpenSearch docs.
Highlighting
The highlight() method can be used to add a highlight section to your query along the rules in the OpenSearch docs.
Post filter
The addPostFilterQuery() method can be used to add a post_filter BoolQuery to your query along the rules in the OpenSearch docs.
Pagination
Finally the Builder also features a size() and from() method for the corresponding OpenSearch search parameters. These can be used to build a paginated search. Take a look the following example to get a rough idea:
Multi-Search Query Builder
OpenSearch provides a "multi-search" API that allows for multiple query bodies to be included in a single request.
Use the MultiBuilder class and add builders to add builders to your query request. The response will include a responses array of the query results, in the same order the requests are added. Use the $multiBuilder->search() to execute the queries, or $multiBuilder->getPayload() for the raw request payload.
Returns the following response JSON shape:
Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
If you've found a bug regarding security please mail [email protected] instead of using the issue tracker.
Credits
- Alex Vanderbist
- Ruben Van Assche
- Sendy
- All Contributors
License
The MIT License (MIT). Please see License File for more information.