Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algolia Manual Auto complete in Flutter

I'm busy implementing indexed search in Flutter / Dart using algolia. I've ran into a problem that's not an actual problem. I'm struggling to figure out how to query for autocomplete results. Meaning, if I pass in the term "burg" I should be getting back "bacon burger", "avo burger", etc.

The problem is not with flutter, I can do everything I want to in flutter. The problem is not with dart, I know my way around Dart well. The problem is with Algolia docs and how to implement autocomplete if they don't have a provided widget for the platform.

The things I've been googling that I would like to know is.

  • What do I pass into the query to indicate that the results I want should be auto complete results and not complete match. I didn't type all of that into google but that's basically what I've been trying to search.

I'm using this plugin and have setup the instance properly as I can get results back from my indexed data on Algolia. I'm setting the facets and passing in values for those facetFilters. It works great for normal search but I can't figure out, from the docs, how to adjust my query to make it return auto complete results for potential matches and not the results of the actual search.

I hope that's clear. to make it even clearer. Things that I don't need.

  • How to setup Algolia (That's done)
  • How to use algolia in Flutter (That's done)
  • Using a certain architecture to make this easier (that's done). I just need to add 1 function to return the titles of auto complete and the functionality will work
  • How to build an auto complete widget (That's done)
  • Basically no flutter specific, UI specific or architecture specific help. Just how to query for auto complete results.

I've looked at Query suggestions on Algolia docs but the code isn't very clear on what I need to query if i want to manually get these suggestions from the algolia instance. I've looked at autocomplete js queries are hidden, or at least from my eyes. I've looked at Autocomplete Vue to no avail. And every other auto complete link that popped up.

like image 721
Filled Stacks Avatar asked Sep 16 '20 11:09

Filled Stacks


1 Answers

This question has been open for a long time. I assume you probably don't need an answer to it anymore, but given the amount of upvotes, it might still be worth answering it.

The behavior you're looking for is called "prefix matching" in Algolia's documentation.

The main parameter to control this behavior in Algolia is queryType.
It accepts 3 values: prefixLast, prefixNone & prefixAll.

  1. prefixLast treats only the last word of the query as a potential prefix.
    All the previous words are matched as complete words.
    It's a good fit for "as-you-type search" experiences, and most likely the value you were looking for.
    This is the default value of this parameter, so you should just be able to run client.query("burg") to get the expected behavior.
    Note: if the last word is followed by a space, it is not considered as a prefix.

  2. prefixNone treats no word of the query as a potential prefix.
    It's a good fit for traditional "press Enter -> get search results" experiences.

  3. prefixAll matches all words of the query as prefixes.
    It's rarely a good option, as it might bring a lot of false positives.
    If you'd like this behavior only for some words (e.g. "avo" for "avocado"), I'd suggest using synonyms instead.

https://www.algolia.com/doc/api-reference/api-parameters/queryType/

This parameter can be changed in your index settings, or at query time.

like image 50
Jerska Avatar answered Oct 01 '22 09:10

Jerska