Default Search Models
All models under namespace Pimcore\Bundle\GenericDataIndexBundle\Model\OpenSearch
are deprecated and will be removed in version 2.0
Please use models from Pimcore\Bundle\GenericDataIndexBundle\Model\DefaultSearch
instead.
Default search models can be used when individual search queries are needed to streamline the creation of Elasticsearch or OpenSearch search JSONs.
This is especially useful when you want to create your own search modifiers or when you would like to create services which should directly execute searches through the search client. They are used by the Generic Data Index and its search services internally to handle the execution of search queries on a lower level.
Example usage in search modifier
This example shows how to use a custom search modifier to add a term filter to the search query.
#[AsSearchModifierHandler]
public function handleCustomFilter(CustomFilter $customFilter, SearchModifierContextInterface $context): void
{
$context->getSearch()->addQuery(
new TermFilter(
field: $customFilter->getField(),
term: $customFilter->getValue(),
)
);
}
Search Model
The search model is the main model to create a search query. It can be used to add queries, filters, aggregations and sorting to the search.
use Pimcore\Bundle\GenericDataIndexBundle\Model\DefaultSearch\Search;
$search = (new Search())
->setSize(10) // set the number of results to return
->setFrom(0) // set the offset of the results
->setSource(['field']) // set the fields to return
->addSort(new FieldSort('field', 'asc')) // add a sort
->addQuery(new TermQuery('field', 'value')) // add a query
->addAggregation(new Aggregation('test-aggregation',[...])) // add an aggregation
;
$result = $searchClient->search( [
'index' => $indexName,
'body' => $search->toArray()
]);
Query Models
The query models are used to create a query for the search. They can be used to create any query which is supported by OpenSearch or Elasticsearch.
BoolQuery
Represents a boolean query. It can be used to combine multiple queries with boolean operators. See documentation for OpenSearch or Elasticsearch for more details.
Basic usage
use Pimcore\Bundle\GenericDataIndexBundle\Model\DefaultSearch\Query\BoolQuery;
$boolQuery = new BoolQuery([
'should' => [
['term' => ['field' => 'value']],
['term' => ['field2' => 'value2']],
],
]);
Add additional conditions
$boolQuery = new BoolQuery();
$boolQuery->addCondition('must', [
'term' => ['field' => 'value']
]);
Merge multiple queries
$boolQueryA = new BoolQuery([
'should' => [
['term' => ['field' => 'value']],
],
]);
$boolQueryB = new BoolQuery([
'should' => [
['term' => ['field' => 'value']],
],
]);
// this will result in a query with two "should" conditions
$boolQueryA->merge($boolQueryB);
Use other queries in sub queries
$boolQuery = new BoolQuery([
'should' => [
new TermFilter('field', 'value'),
new TermFilter('field2', 'value2'),
]
]);