Skip to content

Commit

Permalink
Merge pull request #232 from hkulekci/documentation-update-for-search…
Browse files Browse the repository at this point in the history
…-callback

readme changed to mention about callback function parameters of the search
  • Loading branch information
matchish authored Feb 10, 2023
2 parents 3b5aed8 + 07ee895 commit d8b0737
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,13 @@ There is two ways.
By default, when you pass a query to the `search` method, the engine builds a [query_string](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html) query, so you can build queries like this

```php
Product::search('title:this OR description:this) AND (title:that OR description:that')`
Product::search('(title:this OR description:this) AND (title:that OR description:that)')
```

If it's not enough in your case you can pass a callback to the query builder

```php
$results = Product::search('zonga', function($client, $body) {
$results = Product::search('zonga', function(\Elastic\Elasticsearch\Client $client, $body) {

$minPriceAggregation = new MinAggregation('min_price');
$minPriceAggregation->setField('price');
Expand All @@ -219,8 +219,14 @@ $results = Product::search('zonga', function($client, $body) {
})->raw();
```

`$client` is `\ElasticSearch\Client` object from [elasticsearch/elasticsearch](https://packagist.org/packages/elasticsearch/elasticsearch) package
And `$body` is `ONGR\ElasticsearchDSL\Search` from [ongr/elasticsearch-dsl](https://packagist.org/packages/ongr/elasticsearch-dsl) package
> Note : The callback function will get 2 parameters. First one is `$client` and it is an object of `\Elastic\Elasticsearch\Client`
> class from [elasticsearch/elasticsearch](https://packagist.org/packages/elasticsearch/elasticsearch) package.
> And the second one is `$body` which is an object of `\ONGR\ElasticsearchDSL\Search` from
> [ongr/elasticsearch-dsl](https://packagist.org/packages/handcraftedinthealps/elasticsearch-dsl) package. So, while
> as you can see the example above, `$client->search(....)` method will return an
> `\Elastic\Elasticsearch\Response\Elasticsearch` object. And you need to use `asArray()` method to get array result.
> Otherwise, the `HitsIteratorAggregate` class will throw an error. You can check the issue
> [here](https://github.com/matchish/laravel-scout-elasticsearch/issues/215).
### Search amongst multiple models
You can do it with `MixedSearch` class, just pass indices names separated by commas to the `within` method.
Expand Down

0 comments on commit d8b0737

Please sign in to comment.