felipe096
felipe096

Reputation: 3

is this the correct way to get facets from algolia index?

This code is not very pretty, so I wonder if there is a better way to get the facets from an index?

var algolia = algoliasearch('id', 'key');
var helper = algoliasearchHelper(algolia, 'getstarted_actors', {
  facets: ['*'],
});
helper.on("result", results => console.log(Object.keys(results['_rawResults'][0].facets)));

Upvotes: 0

Views: 161

Answers (1)

Ajay D.
Ajay D.

Reputation: 229

You can also do an empty query search and then do whatever you need with the response - in the simple example below we get the facets and print them to console:

var algoliasearch = require('algoliasearch');
var client = algoliasearch('latency', '249078a3d4337a8231f1665ec5a44966');
var index = client.initIndex('bestbuy');

index.search(
  {
    query: '', // empty query
    facets: ['*'],  // retrieve all facets
  },
  function searchDone(err, content) {
    if (err) throw err;
    console.log(JSON.stringify(content.facets, undefined, 2)); // print
  }
);

Upvotes: 0

Related Questions