algolia / scout-extended

Scout Extended: The Full Power of Algolia in Laravel
https://www.algolia.com/doc/framework-integration/laravel/getting-started/introduction-to-scout-extended/
MIT License
400 stars 87 forks source link

where() method not filtering query #108

Closed redeemefy closed 5 years ago

redeemefy commented 5 years ago

I'm trying to integrate algolia in a personal project. The table I'm indexing is the patterns table that has the user_id as FK and is included as one of the fileds I upload to algolia.

class SearchController extends Controller
{
    public function show()
    {
        $search = request('q');
        $patterns = Pattern::search($search)->where('user_id', '=', Auth::user()->id)->get();
        // dd(Auth::user()->id);
        return view('search.index', ['patterns' => $patterns]);
    }
}

If I dump the user_id I get 1. Intentionally I'm searching for something that doesn't belong to that user expecting to get nothing from algoia but I still get the record back. The record that I get back has the user_id 3.

I'm not sure if I'm missing some setting in the algolia dashboard or the code is missing something.

I'm searching for the word test and is the only record that contains that word.

screen shot 2019-01-06 at 9 57 01 am
nunomaduro commented 5 years ago

Thanks for creating this issue, I am here to help you.

First question, you are using scout extended right?

redeemefy commented 5 years ago

I did ran composer require algolia/scout-extended and php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider".

After that I php artisan scout:flush and php artisan scout:import.

nunomaduro commented 5 years ago

Perfect. Now I need you to check if the record that haves the objectID App\Pattern::17 is the exacly the record with the id 17 in your database.

redeemefy commented 5 years ago
screen shot 2019-01-06 at 12 29 17 pm
nunomaduro commented 5 years ago

@diazgilberto This is super weird. Can you try add a dd($options); on the line 135 of the file Laravel\Scout\Engines\AlgoliaEngine and perform a search? And tell me the results of the dd.

redeemefy commented 5 years ago

Laravel\Scout\src\Engines\AlgoliaEngine instead?

nunomaduro commented 5 years ago

Exacly.

redeemefy commented 5 years ago
screen shot 2019-01-06 at 2 16 22 pm
nunomaduro commented 5 years ago

Perfect. Now, remove the previous dd. And on the line 135 replace the current line by: return dd($algolia->search($builder->query, $options));. And tell the results.

redeemefy commented 5 years ago

Weird, "hits" => []

screen shot 2019-01-06 at 2 21 58 pm
nunomaduro commented 5 years ago

You need to put the exact same query that returned the The record that I get back has the user_id 3..

nunomaduro commented 5 years ago

Because the screenshot you have posted, shows that algolia returns 0 records.

redeemefy commented 5 years ago

it is the same query. The search term is test

nunomaduro commented 5 years ago

Perfect. Algolia is returning 0 results from that query. As you can see, hits are empty.

Just to make sure, please use this code on your controller:


        $patterns = Pattern::search('test')->where('user_id', '=', 1)->get();
        dd($patterns);

And confirm that you really got a pattern with the user_id === 3.

redeemefy commented 5 years ago

In algolia the user_id is 3. What I search for the term test, I'm using the user_id 1 expecting to get nothing from algolia. "hits" => [] is exactly what I'm expecting.

nunomaduro commented 5 years ago

Exacly, but you told me that using this code, The result of the $patterns is a model with the user_id == 3 right?

        $patterns = Pattern::search('test')->where('user_id', '=', 1)->get();
        dd($patterns);
redeemefy commented 5 years ago

Collection is empty as expected utilizing your snippet and my code.

$patterns = Pattern::search('test')->where('user_id', '=', 1)->get();
// or
$patterns = Pattern::search('test')->where('user_id', '=', Auth::user()->id)->get();

dd($patterns);

The problem should be when I render the view. Seems like vue-instantsearch is performing a search on render.

@extends('layout._base')

@section('content')
    <ais-index
        app-id="{{ config('scout.algolia.id') }}"
        api-key="{{ config('scout.algolia.public') }}"
        index-name="patterns"
        id="app"
        query="{{ request('q') }}"
    >
        <div class="row">
            <div class="col">
                <ais-results>
                    <template slot-scope="{ result }">
                        <div class="card">
                            <div class="card-header">
                                <a :href="result.path">
                                    <ais-highlight :result="result" attribute-name="name"></ais-highlight>
                                </a>
                            </div>
                            <div class="card-body">
                                <ais-highlight :result="result" attribute-name="description"></ais-highlight>
                            </div>
                        </div>
                        <p>
                        </p>
                    </template>
                </ais-results>
            </div>
            <div class="col-3">
                <div class="card mb-5">
                    <div class="card-header font-weight-bold">Search</div>
                    <div class="card-body">
                        <ais-input placeholder="Search patterns..." class="form-control" :autofocus="true"></ais-input>
                    </div>
                </div>
                <div class="card">
                    <div class="card-header">
                        Filter By Category
                    </div>
                    <div class="card-body">
                        <ais-refinement-list attribute-name="category"></ais-refinement-list>
                    </div>
                </div>
            </div>
        </div>
    </ais-index>
@endsection
nunomaduro commented 5 years ago

Ahah. You have been searching with vue-instantsearch this whole time. Since you are using client-side search, you just have to inspect your network to debug your problem.

nunomaduro commented 5 years ago

Otherwise you can address this issue with our vue-instasearch team : https://github.com/algolia/vue-instantsearch.

redeemefy commented 5 years ago

Well, my initial search is server side. Even though the server side search returns the correct object, looks like vue-instantsearch is querying again when I pass uery="{{ request('q') }}".

redeemefy commented 5 years ago

Closing since scout-extended is behaving as expected and the problem resides somewhere else.

Thanks Nuno!!!

nunomaduro commented 5 years ago

Thanks! Since you are doing client side search you don't need the server side search.

Don't hesitate to check the docs of vue instasearch: https://www.algolia.com/doc/guides/building-search-ui/what-is-instantsearch/vue.

Be sure to stay tuned to my twitter account @enunomaduro for more information about this package. Are you enjoying this package? Please tweet the link https://github.com/algolia/scout-extended and tells us your favourite feature! 🚀

babacarcissedia commented 3 years ago

This is next level support! Thanks @nunomaduro for your work!