Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add parameters before search() ? #28

Closed
Geoffrey63 opened this issue Mar 30, 2018 · 5 comments
Closed

How to add parameters before search() ? #28

Geoffrey63 opened this issue Mar 30, 2018 · 5 comments

Comments

@Geoffrey63
Copy link

Geoffrey63 commented Mar 30, 2018

I am adding a parameter to Searcher like this :

InstantSearch.shared.getSearcher().params.filters = "application_categories:69539"
InstantSearch.shared.getSearcher().search()

On the results, this params is not taken in account.

Here is the debug result. The filter appears in the Searcher params :

(lldb) po InstantSearch.shared.getSearcher().params
SearchParameters{["hitsPerPage": "20", "attributesToHighlight": "[\"name\"]", 
"attributesToRetrieve": "[\"name\",\"description\",\"best_offer\",\"brand\",\"assigned_images\",\"created_on\",
\"offers_count\",\"other_offers\",\"application_categories_dict\"]", "filters": "application_categories:69539"]}

Can any one please tell me how can I add parameter to the InstantSearch ?

@spinach
Copy link
Contributor

spinach commented Apr 3, 2018

Hey @Geoffrey63,

Thanks for submitting this issue. Indeed the documentation is not very clear about that and we're working on better documentation in the near future.

That being said, let me explain what is happening:
You can find here in the filtering section for how to filter using InstantSearch. Basically, using searcher.addFacetRefinement(name:value:). At the end of that section, you'll see the following note:

The filters and facetFilters search parameters will be overridden by the facet refinements; any manually specified value will be lost.

This is because InstantSearch Core is built on top of the AlgoliaSearch-Client, offers better APIs to set the filters, and will override the properties in Client. That being said, I definitely believe that we should do a better job in deciding what/how to expose those methods/properties, and rest assured we're working on this next.

@spinach spinach closed this as completed Apr 3, 2018
@leojkwan
Copy link

Hey @spinach, Running into the same problem, super frustrating why I can't port over same Angular Algolia logic to iOS Instant Search.

let filterState = FilterState()

    // Add filter to conjunctive group
    filterState[or: "disjunctiveGroup"].add(
      Filter.Facet(attribute: "athlete_user_id", stringValue: "ID_ONE"),
      Filter.Facet(attribute: "athlete_user_id", stringValue: AppEnvironment.current.currentUser.userId)
    )

    // Add filter to disjunctive group
    filterState[and: "conjunctiveGroup"].add(Filter.Facet(attribute: "visibility_status", stringValue: "archived", isNegated: true))

    // set filters to Query
    let query: Query = Query()
    query.filters = filterState.description
    print(filterState.description)
    
    searcher.indexQueryState.query.filters = filterState.description
    searcher.search()

The filter state description I get looks right, and I've faceted all the right attributes on the dashboard as 'searchable'( "visibility_status", "athlete_user_id"). The hits return literally a full query without filters. Only thing that worked from the query was the page limit size.

What am I doing wrong?

Followed these docs https://www.algolia.com/doc/api-reference/widgets/filter-state/ios/

@VladislavFitz
Copy link
Contributor

Hello @leojkwan
Your usage of FilterState looks correct.
Could you give us more details of how you dispatch present the search results? Do you use HitsInteractor component?
Did you try the generated FilterState.description string directly in your dashboard? Did it work well?

@VladislavFitz VladislavFitz pinned this issue Jun 29, 2020
@leojkwan
Copy link

@VladislavFitz

So I got things working by using facetFilters = [] with the same query syntax strings I posted above. I’ve tried generating the filter string both manually and through converting filterState as mentioned in the docs but it didn’t work. To be clear I don’t plan on faceting userIds but as long as it works I’m cool with it for now(currently not displaying any facet filter UI).

I am using the hits interactor and though I did not generate the string from the dashboard I am positive it is correct. The only different with facet filters is that negation is annotated with a minus instead of NOT(ie “visibilityStatus:-archived”)

@VladislavFitz VladislavFitz unpinned this issue Jul 20, 2020
@arseniybanayev
Copy link

Struggle with this issue for a few hours, as well. Like @leojkwan, I used query.facetFilters = ["..."] instead of query.filters = "..." in my iOS project, but I don't quite understand why this worked but query.filters = "..." didn't. For instance, in my React project, <Configure filters={...} /> works and I don't need to use facetFilters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants