searchForFacetValues, excluding values containing numbers or underscore

I need to return results of a searchForFacetValues allowing only values that contain the equivalent of this regex:

[a-zA-Z -]

My code is like this:

   index.searchForFacetValues({
        facetName: '_tags',
        maxFacetHits: 100,
        sortFacetValuesBy: 'alpha',
        maxValuesPerFacet: 100,
        facetQuery: $("#inputfield input").val()
    }, searchCallback);

Some of my values contain numbers or an underscore and I can filter them out once they are returned, but the good values might not be among the first 100…

How can I do this?

Hi @unjobsmedia,

Thanks for contacting Algolia.

Our searchForFacetValues method does not have such a RegEx feature built in, instead it is a standard text search looking for matches or close-matches (with typos) for the value that you input into the facetQuery.

If I understand your goal correctly, your user may search only using pure letters like "a-zA-Z".

You would have to instead enrich your data with a new attribute that does not have numbers or underscores so that it can better match your search query.

Example:

{
   "my_facet_value": "ABC_123_DEF",
   "my_alt_facet_value": "ABCDEF"
}

It is only with this pre-computed data that a search for the Algolia facet value could work well. Is this doable for you at indexing time?

1 Like

Thanks Ajay. I can do this, no problem.

But I expected to be able to apply a filter, like this:

filters: 'NOT "_"',

for example, to not return results contain underscore.

When I tried that, the results were different, but not what I expected.

First, let me add that on this web page, the user has an empty search input and some default results are shown, using this code:

if ($("#inputfield input").val() === '') {
                window.search();
            }

When I applied the filter above, the results contained only the values with numbers. So far, so good, I thought. Then I tried

'NOT "_" OR "1"' and 'NOT "_" OR NOT "1"'

which are valid filters according the to tool here https://www.algolia.com/doc/api-reference/api-parameters/filters/#filters-syntax-validator

But the first 100 results were still the numeric ones. The values I want to remove are UNIX timestamps, all starting with “1”.

So yes, I can add another attribute, but I thought I was on the right track. Is it really not possible using a filter?

Hi @unjobsmedia,

Thank you for checking! I can confirm our filters are still looking to match existing attribute values. They do not have RegEx capabilities built in.

I will add a new attribute to solve this but to clarify, I was not attempting to use a regex which I know is not supported. I thought that a filter might work, and the approach looked promising. Perhaps filters only work for entire words (values) and not just for a character within a word?

Hi @unjobsmedia,

Perhaps filters only work for entire words (values) and not just for a character within a word?

Yes, exactly! This is why you can’t use filters for your exact use case and having an extra attributes without underscores and numbers will work better.

Just for the reference, you can see an example fo searchForFacetValues with aa filter here: https://www.algolia.com/doc/api-reference/api-methods/search-for-facet-values/#search-for-facet-values-with-an-additional-searchparameters, you’ll see that it achieves something different from what you are trying to do.

I hope this is clearer, let us know if you still need help!