Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perform a facet search query with Algolia autocomplete

My index objects has a city field and I'd like to retrieve these with autocomplete, but documentation seems missing about how to perform a query (only basic search documentation is available), I found a prototype IndexCore.prototype.searchForFacetValues in the autocomplete.js but I have no idea to use it.

like image 453
Xia Pengda Avatar asked Dec 24 '22 10:12

Xia Pengda


1 Answers

You should be able to use the following source:

var client = algoliasearch("YourApplicationID", "YourSearchOnlyAPIKey");
var index = client.initIndex("YourIndex");

autocomplete("#search-input", { hint: false }, [
  {
    source: function(query, callback) {
      index
        .searchForFacetValues({
          facetName: "countries",
          facetQuery: query
        })
        .then(function(answer) {
          callback(answer.hits);
        })
        .catch(function() {
          callback([]);
        });
    },
    displayKey: "my_attribute",
    templates: {
      suggestion: function(suggestion) {
        return suggestion._highlightResult.my_attribute.value;
      }
    }
  }
]);

This uses the searchForFacetValues method to get the results.

like image 185
Haroen Viaene Avatar answered Dec 27 '22 06:12

Haroen Viaene