AlgoliaHelper: How to facet filter a nested array property

Working on modifying a client’s Algolia search tool (using AlgoliaHelper JS) and need to figure out how to reference/add a facet refinement - I don’t have access to the Algolia account itself so I’m working just from the API docs and what I get back in the existing search results.

Given the results arrive in this format:
{
“title”: “lorem ipsum”,
“description”: “lorem ipsum”,
“locs”: [
{
“streetAddress1”: “4501”,
“streetAddress2”: “Pleasanton Avenue”,
“streetAddress3”: “”,
“postalCode”: “94566”,
“latitude”: “37.6599327”,
“longitude”: “-121.8820906”,
“city”: “Pleasanton”,
“region”: “California”,
“country”: “US”,
“name”: “”,
“has_been_geocoded”: 1,
“geocode_string”: “4501 Pleasanton Ave, Pleasanton, CA 94566, USA”,
“virtual”: “No”,
“directions”: “lorem ipsum”
}
],

}

If I was interested in adding a postalCode straight string search (to find entries that have a loc with a postalCode of 94566, for example, would the facet be locs.postalCode, locs[0].postalCode, locs.0.postalCode, or something else entirely?

I’m getting back no errors, but also no results with the above attempts, so if I’m structuring the facet name wrong, I wouldn’t know. Would I be able to tell just from console.log’ing the results of a basic search if that field is numeric or not?

Hi @zach.gaskins, welcome to the community forum! Thanks for reaching out about this. In order to do filtering on nested arrays there’s two things I would keep in mind:

  1. The particular attribute for faceting needs to be declared in attributes for faceting (For example “locs.postalCode”)
  2. The correct syntax for applying a filter would actually look like this: “locs.postalCode:94566”, without the index. (“0”)

This should allow you to filter for a particular value, even if you don’t know which element in the array of locations has that postal code value.

Please let us know if that doesn’t work for you!

Maria,
Thanks for the quick reply!

So here’s how I’ve initialized the helper:

const params = { hitsPerPage: 5, facets: ['locs.postalCode'] },
algolia = algoliasearch({credentials here}),
algoliaHelper = algoliasearchHelper(algolia, "new_opportunities", params);
algoliaHelper.clearRefinements();
const $zipInput = $("#algolia-query-zip"); // Input with postal code, eg 94566
// {Refinement below goes here}
algoliaHelper.search();

Later, I’ve tried both of these individually:

algoliaHelper.addNumericRefinement('locs.postalCode','=',$zipInput.val() * 1);

and

algoliaHelper.addFacetRefinement('locs.postalCode',$zipInput.val());

Neither returns results (but no errors either).

I’m wondering if I’m being held back by only having the result structure and not being able to see the actual structure of the data as it’s stored, which could differ.

Thanks for providing those code snippets! Both refinement options actually look good to me. The results format you posted previously seems to imply that postal codes are strings, and what is returned in the results should be the same as how the data is stored, so the facetRefinement rather than numericRefinement should work.

However, if you do not have access to the account, and thus cannot make index settings changes, you will not be able to set the locs.postal code as an attribute for faceting. Without declaring it for faceting, it is not possible to use it as a filter. Is it possible for you to get access to the account or someone who can make the index setting changes?