matchish / laravel-scout-elasticsearch

Search among multiple models with ElasticSearch and Laravel Scout
MIT License
702 stars 113 forks source link

Add possibility to use instances of classes that implement ONGR\ElasticsearchDSL\BuilderInterface as values for where() in Laravel Scout query builder #260

Closed swayok closed 11 months ago

swayok commented 11 months ago

For https://github.com/matchish/laravel-scout-elasticsearch/issues/259

hkulekci commented 11 months ago

Thanks @swayok. This is nice to solve this problem, and I appreciate your effort. Could we write a test to see when the problem happened and what we solved exactly in this PR?

swayok commented 11 months ago

@hkulekci added test case for RangeQuery but I wasn't able to run it.

hkulekci commented 11 months ago

@matchish could you approve to be able to see test results?

hkulekci commented 11 months ago

@hkulekci added test case for RangeQuery but I wasn't able to run it.

Hey @swayok thanks for the quick response and the tests. Let's wait for @matchish. I hope he will approve soon.

codecov-commenter commented 11 months ago

Codecov Report

All modified lines are covered by tests :white_check_mark:

Comparison is base (97e5578) 96.06% compared to head (ac3b9ff) 96.08%.

:exclamation: Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #260 +/- ## ============================================ + Coverage 96.06% 96.08% +0.01% - Complexity 192 193 +1 ============================================ Files 36 36 Lines 636 638 +2 ============================================ + Hits 611 613 +2 Misses 25 25 ``` | [Files](https://app.codecov.io/gh/matchish/laravel-scout-elasticsearch/pull/260?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Sergey+Shlyakhov) | Coverage Δ | | |---|---|---| | [src/ElasticSearch/SearchFactory.php](https://app.codecov.io/gh/matchish/laravel-scout-elasticsearch/pull/260?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Sergey+Shlyakhov#diff-c3JjL0VsYXN0aWNTZWFyY2gvU2VhcmNoRmFjdG9yeS5waHA=) | `100.00% <100.00%> (ø)` | |

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

hkulekci commented 11 months ago

LGTM. I prefer splitting the tests into different functions. But at least for now, it shows that the implementation is working. If you can, please could you move the test into a new test function as below :

    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,  // Promo Products
                RangeQuery::LTE => 100,  // Promo Products
            ]))
            ->whereIn('type', ['used', 'like new'])
            ->get();

        $this->assertEquals($iphonePromoUsedAndLikeNewWithRange->count(), $iphonePromoUsedAndLikeNewAmount);
        $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());
    }

Thanks @swayok. ^_^

matchish commented 11 months ago

And could you update README plz?

swayok commented 11 months ago

Done

hkulekci commented 11 months ago

Hey, @swayok, thanks for the quick update. I appreciate it.

@matchish LGTM!

matchish commented 11 months ago

Released! Thanks @swayok. Really nice feature