Perform a facet search query with Algolia autocomplete

My pointer objects have a field city, and I would like to get them autocompleted, but there is not enough documentation on how to execute the query (only basic search documentation is available), I found a prototype IndexCore.prototype.searchForFacetValuesin autocomplete.js, but I have no idea how to use it.

+4
source share
1 answer

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;
      }
    }
  }
]);

To obtain the results, the method is used searchForFacetValues.

+10
source

Source: https://habr.com/ru/post/1693350/


All Articles