question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Filtering by parameter

See original GitHub issue

I have a backend in Laravel and rendering a vue-instantsearch inside a blade file.

@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" v-if="@json(Auth::user()->id) == result.user_id">
                            <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

In line 15 I have a condition to match the user_id with the current user logged in in the app. The condition is filtering properly but the downside of this is that…

  1. Everything is getting fetch from algolia regardless.
  2. Attributes for faceting are rendered with all fetched records so the faceting doesn’t match with records rendered.

Algolia data looks like this…

name: "Test"
brand: "Test Brand"
description: "This is an awesome description"
category: "accessories"
user_id: 3
objectID: "App\Pattern::17"
path: "patterns/17"
_tags: ["App\Pattern::17"]

I know that algolia has no knowledge of relationship whatsoever. I was hoping to filter the results by pushing the FK but this approach is not optimal.

What other approaches I can take to accomplish this?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
tioiscommented, Jul 16, 2019

@diazgilberto I had the same problem, I figured out that you have to add the attributes you want to filter on to the Index configuration on the Algolia website. In your Index, go to the “Configuration” tab, then “Facets” (even though we’re not using facets), and add the attributes you want to filter on in the “Attributes for faceting” zone and select “Filter only” for these attributes. This is how I ended solving this problem.

Hope this helps!

1reaction
redeemefycommented, Jan 27, 2019

@Haroenv the app is minimal already. The data is toy data and the app is fairly small. Interestingly, with the filters: user_id: ... parameter passed to the XHR request I’m not getting any data back.

Here are the patterns data in algolia

screen shot 2019-01-27 at 9 24 41 am

Here the patterns data in my database

screen shot 2019-01-27 at 9 16 14 am

and I’m logged in with user_id 1.

screen shot 2019-01-27 at 9 09 50 am

the blade.php file

@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') }}"
        :query-parameters="{
            filters: 'user_id: @json(Auth::user()->id)'
          }"
    >
        <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

the XHR request

screen shot 2019-01-27 at 9 11 01 am

As you can see, the filters: user_id: 1 is getting passed in the request.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Filtering with parameters - Tableau
Parameters are extremely simple – they allow users to select or input a value. That value can then in turn be used in...
Read more >
How to Filter with Parameter Actions in Tableau - Cleartelligence
How to Filter with Parameter Actions in Tableau · Click on Dashboard > Actions · Click "Add Action" · Select "Change Parameter" ·...
Read more >
Filtering Parameters
The Data Filter options are divided by input and output parameters. Parameter information is displayed in the Data Filters tables near the bottom...
Read more >
Using Parameter Actions to Filter in Tableau (with Multiple ...
Using Parameter Actions to Filter in Tableau (with Multiple Measures) · Step 1: Create a Parameter · Step 2: Create a Parameter Action...
Read more >
Using filters with parameters in Amazon QuickSight
Use this section to filter the data in an analysis or dashboard by a single-value parameter value. To use a multivalued parameter—one with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found