public function SearchBuilder::build in Elasticsearch Connector 8.5
Same name and namespace in other branches
- 8.7 src/ElasticSearch/Parameters/Builder/SearchBuilder.php \Drupal\elasticsearch_connector\ElasticSearch\Parameters\Builder\SearchBuilder::build()
- 8.2 src/ElasticSearch/Parameters/Builder/SearchBuilder.php \Drupal\elasticsearch_connector\ElasticSearch\Parameters\Builder\SearchBuilder::build()
- 8.6 src/ElasticSearch/Parameters/Builder/SearchBuilder.php \Drupal\elasticsearch_connector\ElasticSearch\Parameters\Builder\SearchBuilder::build()
Build up the body of the request to the Elasticsearch _search endpoint.
Return value
array Array or parameters to send along to the Elasticsearch _search endpoint.
File
- src/
ElasticSearch/ Parameters/ Builder/ SearchBuilder.php, line 69
Class
- SearchBuilder
- Class SearchBuilder.
Namespace
Drupal\elasticsearch_connector\ElasticSearch\Parameters\BuilderCode
public function build() {
// Query options.
$params = IndexFactory::index($this->index, TRUE);
$query_options = $this
->getSearchQueryOptions();
// Set the size and from parameters.
$this->body['from'] = $query_options['query_offset'];
$this->body['size'] = $query_options['query_limit'];
// Sort.
if (!empty($query_options['sort'])) {
$this->body['sort'] = $query_options['sort'];
}
// Build the query.
if (!empty($query_options['query_search_string']) && !empty($query_options['query_search_filter'])) {
$this->body['query']['bool']['must'] = $query_options['query_search_string'];
$this->body['query']['bool']['filter'] = $query_options['query_search_filter'];
}
elseif (!empty($query_options['query_search_string'])) {
if (empty($this->body['query'])) {
$this->body['query'] = [];
}
$this->body['query'] += $query_options['query_search_string'];
}
elseif (!empty($query_options['query_search_filter'])) {
$this->body['query'] = $query_options['query_search_filter'];
}
// TODO: Handle fields on filter query.
if (empty($fields)) {
unset($this->body['fields']);
}
if (empty($this->body['post_filter'])) {
unset($this->body['post_filter']);
}
// TODO: Fix the match_all query.
if (empty($query_body)) {
$query_body['match_all'] = [];
}
$exclude_source_fields = $this->query
->getOption('elasticsearch_connector_exclude_source_fields', []);
if (!empty($exclude_source_fields)) {
$this->body['_source'] = [
'excludes' => $exclude_source_fields,
];
}
// More Like This.
$this
->setMoreLikeThisQuery($query_options);
$params['body'] = $this->body;
// Preserve the options for further manipulation if necessary.
$this->query
->setOption('ElasticParams', $params);
// Allow other modules to alter index config before we create it.
$indexName = IndexFactory::getIndexName($this->index);
$dispatcher = \Drupal::service('event_dispatcher');
$buildSearchParamsEvent = new BuildSearchParamsEvent($params, $indexName);
$event = $dispatcher
->dispatch(BuildSearchParamsEvent::BUILD_QUERY, $buildSearchParamsEvent);
$params = $event
->getElasticSearchParams();
return $params;
}