Skip to content

Commit

Permalink
Update test case for RangeQuery. Update README.md.
Browse files Browse the repository at this point in the history
  • Loading branch information
Filippov Alexander committed Oct 11, 2023
1 parent 1aea7ef commit ac3b9ff
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,32 @@ $results = Product::search('zonga', function(\Elastic\Elasticsearch\Client $clie
> Otherwise, the `HitsIteratorAggregate` class will throw an error. You can check the issue
> [here](https://github.com/matchish/laravel-scout-elasticsearch/issues/215).
### Conditions ###

Scout supports only 3 conditions: `->where(column, value)` (strict equation), `->whereIn(column, array)` and `->whereNotIn(column, array)`:

```php
Product::search('(title:this OR description:this) AND (title:that OR description:that)')
->where('price', 100)
->whereIn('type', ['used', 'like new'])
->whereNotIn('type', ['new', 'refurbished']);
```

Scout does not support any operators, but you can pass ElasticSearch terms like `RangeQuery` as value to `->where()`:

```php

use ONGR\ElasticsearchDSL\Query\TermLevel\RangeQuery;

Product::search('(title:this OR description:this) AND (title:that OR description:that)')
->where('price', new RangeQuery('price', [
RangeQuery::GTE => 100,
RangeQuery::LTE => 1000,
]);
```

Full list of ElasticSearch terms is in `vendor/handcraftedinthealps/elasticsearch-dsl/src/Query/TermLevel`.

### Search amongst multiple models
You can do it with `MixedSearch` class, just pass indices names separated by commas to the `within` method.
```php
Expand Down
50 changes: 47 additions & 3 deletions tests/Feature/SearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,61 @@ public function test_search_with_filters(): void

$this->assertEquals($iphonePromoUsedAndLikeNew->count(), $iphonePromoUsedAndLikeNewAmount);
$this->assertInstanceOf(Product::class, $iphonePromoUsedAndLikeNew->first());
}

public function test_search_with_custom_filter()
{
$dispatcher = Product::getEventDispatcher();
Product::unsetEventDispatcher();

$kindleCheapAmount = rand(1, 5);
$iphoneLuxuryAmount = rand(1, 5);
$iphonePromoUsedAmount = rand(1, 5);
$iphonePromoNewAmount = rand(6, 10);
$iphonePromoLikeNewAmount = rand(1, 5);
$iphonePromoUsedAndLikeNewAmount = $iphonePromoLikeNewAmount + $iphonePromoUsedAmount;

factory(Product::class, $kindleCheapAmount)->states(['iphone', 'cheap'])->create();
factory(Product::class, $iphoneLuxuryAmount)->states(['iphone', 'luxury'])->create();
factory(Product::class, $iphonePromoUsedAmount)->states(['iphone', 'promo', 'used'])->create();
factory(Product::class, $iphonePromoNewAmount)->states(['iphone', 'promo', 'new'])->create();
factory(Product::class, $iphonePromoLikeNewAmount)->states(['iphone', 'promo', 'like new'])->create();

Product::setEventDispatcher($dispatcher);

Artisan::call('scout:import');

// Promo Product Test
$iphonePromoUsedAndLikeNewWithRange = Product::search('iphone')
->where('price', new RangeQuery('price', [
RangeQuery::GTE => 100,
RangeQuery::LTE => 100,
RangeQuery::GTE => 100, // Promo Products
RangeQuery::LTE => 100, // Promo Products
]))
->whereIn('type', ['used', 'like new'])
->get();

$this->assertEquals($iphonePromoUsedAndLikeNewWithRange->count(), $iphonePromoUsedAndLikeNewAmount);
$this->assertInstanceOf(Product::class, $iphonePromoUsedAndLikeNewWithRange->first());
$this->assertInstanceOf(Product::class, $iphonePromoUsedAndLikeNewWithRange->first(), 'Promo Product Assert');

// Luxury Product Test
$iphoneLuxuryUsedAndLikeNewWithRange = Product::search('iphone')
->where('price', new RangeQuery('price', [
RangeQuery::GTE => 1000, // Luxury Products
]))
->get();

$this->assertEquals($iphoneLuxuryUsedAndLikeNewWithRange->count(), $iphoneLuxuryAmount, 'Luxury Product Count Assert');
$this->assertInstanceOf(Product::class, $iphoneLuxuryUsedAndLikeNewWithRange->first());

// Cheap Product Test
$iphoneCheapWithRange = Product::search('iphone')
->where('price', new RangeQuery('price', [
RangeQuery::LTE => 70, // Cheap Products
]))
->get();

$this->assertEquals($kindleCheapAmount, $iphoneCheapWithRange->count(), 'Cheap Product Count Assert');
$this->assertInstanceOf(Product::class, $iphoneCheapWithRange->first());
}

public function test_sorted_paginate(): void
Expand Down

0 comments on commit ac3b9ff

Please sign in to comment.