Daniel J Daniel J - 1 month ago 13
PHP Question

Algolia facets, tags, and filtering

I'm in the process of changing over my site's search engine from SQL based to Algolia based. I've looked through the documentation a lot but I'm still not fully sure I understand exactly how this should be set up.

Each row has over 100 columns, which translates to over 100 parameters for each item in Algolia. At least half of them are used for filtering in one way or another. A record might look something like this:

{
"id":"1",
"Manufacturer": "Abcd",
"Brand": "Efgh",
"Description": "This is a description",
"approved": "1",
"status": "2",
...
}


There are multiple fields that should be searchable by users, like Manufacturer, Brand, and Description. Those fields also need to be filtered by, for example something like
brand = 'Abcd'
. It is my understanding that this is a perfect example of when Facets should be used.

But what about something like id, approved, or status? These aren't fields that need to be searched, and to me they don't seem like good uses for Facets. Yet, that seems like one of the only ways that I can use them for filtering. Am I just supposed to have 50+ facets?

Or should those be
_tags
? If so... how do I add anything to tags? I can't find any method to do so in the Dashboard, and I can't find anything in the PHP documentation either.

And shouldn't those three be searchable anyways, since they're numerical attributes? The documentation states that a field must be a Facet to be filterable, unless it is numeric. Yet the following search turns up empty:

$res = $index->search('', ['filters' => 'id = 1', 'hitsPerPage' => 15, ]);

Answer Source

Like Julien said in a response, the correct thing to do was to set them all as facets. Any attributes that are only going to be used for filtering and not for faceting should be set to filter only:

$index->setSettings([
  'attributesForFaceting' => [
    'filterOnly(approved)'
  ]
]);

The reason my numeric filters weren't returning anything is because the data wasn't pushed to Algolia correctly. All numeric values were sent as strings. So using my example in the original post, rather than looking like this:

{
  ...
  "id": 1,
  ...
}

it looked like this:

{
  ...
  "id": "1",
  ...
}