Faceting on Search multiple indexes feature from Algolia

Hi,

My name is Marcelo. I am currently working for a project and we use algolia for our search functionality.

We use the JS feature to search multiple indexes and now we want to add some custom filters there (faceting) .

I would like to know if there is a way to define facets while searching multiple indexes. I want to be able to filter by one of the attributes of the objects we are storing (it’s not “tags”).

Any help would be really appreciated.

Thanks in advance,

Regards,

Marcelo

Hello Marcelo,

Thank you for contacting us. I understand you’ve already posted your issue on StackOverflow if I’m correct (https://stackoverflow.com/questions/46507342/faceting-on-search-multiple-indexes-feature-from-algolia).

I’ll take the time to answer to your question here also :slight_smile:

You can pass any search parameters you’d like via the multipleQueries call. In your case, it means that you can use your own attributes for faceting. The only thing you need to do first, is to declare the field you’d like to use for faceting in the attributesForFaceting setting.

Does it answer your question?

Hi Anthony,
Thanks for taking your time to reply to this.
First of everything, yes, that’s my question on stack overflow.
Regarding what you suggest, I am posting here an example of what I am using (this is for building the list of queries):

query.push({
	    indexName: 'My Index',
	    query: 'My Query',
	    params: {
		hitsPerPage: 5
	    }
	});
client.search(query, function searchDone(){});

If I am not getting it wrong, I should add attributesForFaceting to each one of my queries, right? And then, change the query according to the values I want to filter. Is that correct? If not, could you, please, provide an example?
Thank you again.

Hello Anthony,

I didn’t receive any other reply to this topic.

I have been trying with your suggestion but I was not able to make it work.

Now, my queries are built in this way:

query.push({
	indexName: 'My Index',
	attributesForFaceting: ['filterOnly(created_by)'], //Both with filterOnly and without it
	query: 'My text',
	params: {
          hitsPerPage: 5,
          filters: 'NOT created_by:my_id'
        }
});

I tried with facetFilters too but it didn’t work either.

query.push({
	indexName: 'My Index',
	attributesForFaceting: ['filterOnly(created_by)'], //Both with filterOnly and without it
	query: 'My text',
	params: {
          hitsPerPage: 5,
          facetFilters: ['created_by:-my_id']
        }
});

Despite doing it in the ways above, I am still getting the results I don’t want to get.

Am I doing something wrong?

Thank you.

attributesForFaceting can be done per index in setSettings or at search time for each of the queries

But, based on my examples, what’s what I am doing wrong? could you, please, give me a example of setting attributesForFaceting on multiple indexes for multiple queries? Because as per what the first response says, I understand it can be done, but then, when I try to do it, I cannot make it work. How would you change a setting for a index on a query when searching for multiple queries? Because each query would be a different setting.
Thank you.

Can you give a complete snippet of code that doesn’t work? AFAICT what you sent should work. If it’s in the browser you can always check the network tab to see what was exactly used

It’s kind of complicate to share the entire piece of code since this work as part of a component that uses another component and I would need to share all with you.
When I checked the network tab to see what was traveling, filters and attributesForFaceting where being sent to the server but the response from the server contained items that were not supposed to be retrieved by algolia.
For example, this item:

  "hits": [
    {
      "share_url": "http://hintd.com/user/marcelo_blanco7/marcelos-wishlist-with-collaborators?h=59e94eae06847eba4bb97500",
      "shop_url": "/shop/59e94eae06847eba4bb97500",
      "name": "Nikon COOLPIX L32 Digital Marcelo",
      "full_name": "Nikon COOLPIX L32 Digital Marcelo",
      "likes": 0,
      "display_price": "$97",
      "clone_time": 1508462254,
      "created_at": 1508462254,
      "total_comments": 0,
      "created_by": "59863add06847e92f60041a7",
      "img": {
        "full": {
          "src": "https://images-na.ssl-images-amazon.com/images/I/51tpjvrtztL.jpg"
        }
      },
      "buyUrl": "https://www.amazon.com/gp/product/B00T85Q37C/ref=s9u_simh_gw_i4?ie=UTF8&fpl=fresh&pd_rd_i=B00T85Q37C&pd_rd_r=601ab887-b534-11e7-a77e-4f4ee7533ebd&pd_rd_w=psSNL&pd_rd_wg=v2WrF&pf_rd_m=ATVPDKIKX0DER&pf_rd_s=&pf_rd_r=ZYZNT955GS5SN39NT99J&pf_rd_t=36701&pf_rd_p=1cf9d009-399c-49e1-901a-7b8786e59436&pf_rd_i=desktop",
      "brand": "Nikon",
      "price": "97.95",
      "max_price": "97.95",
      "size": "",
      "descrip": "",
      "sold_out": false,
      "partner_hint": false,
      "tags": [
        "Fashion",
        "Menswear",
        "Beauty",
        "Grooming",
        "Home",
        "Gear",
        "Tech",
        "Jewelry",
        "Pets",
        "Fitness",
        "Travel",
        "Food&Drink",
        "Entertainment",
        "Charity",
        "Priceless",
        "Kids"
      ],
      "listName": "Marcelo's Wishlist with Collaborators",
      "listUrl": "/user/marcelo_blanco7/marcelos-wishlist-with-collaborators",
      "userName": "marcelo_blanco7",
      "userUrl": "/user/marcelo_blanco7",
      "userAvatar": "",
      "userInitials": "MB",
      "userFirstName": "Marcelo",
      "userLastName": "Blanco",
      "creator": {
        "id": "59863add06847e92f60041a7",
        "link": "/user/marcelo_blanco7",
        "username": "marcelo_blanco7",
        "displayName": "Marcelo Blanco",
        "firstName": "Marcelo",
        "avatar": "",
        "email": "marcelo+notifications1@hintd.com",
        "initials": "MB"
      },
      "clones": 0,
      "objectID": "59e94eae06847eba4bb97500",
      "_highlightResult": {
        "name": {
          "value": "<b>Nikon</b> COOLPIX L32 Digital Marcelo",
          "matchLevel": "full",
          "fullyHighlighted": false,
          "matchedWords": [
            "nikon"
          ]
        }
      },
      "_rankingInfo": {
        "nbTypos": 0,
        "firstMatchedWord": 0,
        "proximityDistance": 0,
        "userScore": 2873,
        "geoDistance": 0,
        "geoPrecision": 1,
        "nbExactWords": 1,
        "words": 1,
        "filters": 0
      }
    },
    {
      "share_url": "http://hintd.com/user/mind_astic/minds-wishlists-158856?h=59d8dd0706847edaf4b974ff",
      "shop_url": "/shop/59d8dd0706847edaf4b974ff",
      "name": "Nikon Coolpix L340 20.2 MP Digital Camera",
      "full_name": "Nikon Coolpix L340 20.2 MP Digital Camera with 28x Optical Zoom and 3.0-Inch LCD (Black)",
      "likes": 0,
      "display_price": "$196",
      "clone_time": 1507384583,
      "created_at": 1507384583,
      "total_comments": 0,
      "created_by": "5932dbcf7a294cd7590041a7",
      "img": {
        "full": {
          "src": "https://images-na.ssl-images-amazon.com/images/I/91zd5c1CzML.jpg"
        }
      },
      "buyUrl": "https://www.amazon.com/gp/product/B00THKEKEQ/ref=s9u_ri_gw_i4?ie=UTF8&fpl=fresh&pd_rd_i=B00THKEKEQ&pd_rd_r=2dfdacbb-ab67-11e7-beda-45d3af6a687a&pd_rd_w=XDV0k&pd_rd_wg=MKNxR&pf_rd_m=ATVPDKIKX0DER&pf_rd_s=&pf_rd_r=5YQRY7VWPQ8T93BTPKT5&pf_rd_t=36701&pf_rd_p=e8de777f-727a-4395-809e-49e84c65e636&pf_rd_i=desktop",
      "brand": "Nikon",
      "price": "196",
      "max_price": "196",
      "size": "",
      "descrip": "",
      "tags": [
        "Tech"
      ],
      "listName": "Mind's Wishlists 158856",
      "listUrl": "/user/mind_astic/minds-wishlists-158856",
      "userName": "mind_astic",
      "userUrl": "/user/mind_astic",
      "userAvatar": "",
      "userInitials": "MA",
      "userFirstName": "Mind",
      "userLastName": "Astic",
      "creator": {
        "id": "5932dbcf7a294cd7590041a7",
        "link": "/user/mind_astic",
        "username": "mind_astic",
        "displayName": "Mind Astic",
        "firstName": "Mind",
        "avatar": "",
        "email": "marcelo@mindastic.com",
        "initials": "MA"
      },
      "clones": 0,
      "sold_out": false,
      "partner_hint": false,
      "objectID": "59d8dd0706847edaf4b974ff",
      "_highlightResult": {
        "name": {
          "value": "<b>Nikon</b> Coolpix L340 20.2 MP Digital Camera",
          "matchLevel": "full",
          "fullyHighlighted": false,
          "matchedWords": [
            "nikon"
          ]
        }
      },
      "_rankingInfo": {
        "nbTypos": 0,
        "firstMatchedWord": 0,
        "proximityDistance": 0,
        "userScore": 2855,
        "geoDistance": 0,
        "geoPrecision": 1,
        "nbExactWords": 1,
        "words": 1,
        "filters": 0
      }
    }
  ],
  "nbHits": 2,
  "page": 0,
  "nbPages": 1,
  "hitsPerPage": 10,
  "processingTimeMS": 1,
  "facets": {
    "tags": {
      "Tech": 2,
      "Beauty": 1,
      "Charity": 1,
      "Entertainment": 1,
      "Fashion": 1,
      "Fitness": 1,
      "Food&Drink": 1,
      "Gear": 1,
      "Grooming": 1,
      "Home": 1,
      "Jewelry": 1,
      "Kids": 1,
      "Menswear": 1,
      "Pets": 1,
      "Priceless": 1,
      "Travel": 1
    }
  },
  "exhaustiveFacetsCount": true,
  "exhaustiveNbHits": true,
  "query": "nikon",
  "params": "query=nikon&hitsPerPage=10&page=0&attributesToRetrieve=*&highlightPreTag=%3Cais-highlight-0000000000%3E&highlightPostTag=%3C%2Fais-highlight-0000000000%3E&getRankingInfo=1&facets=%5B%22tags%22%5D&tagFilters=",
  "index": "local_HINT",
  "serverUsed": "c9-use-1.algolia.net",
  "parsedQuery": "nikon",
  "timeoutCounts": false,
  "timeoutHits": false
}

Is being retrieved despite of adding the filter 'NOT created_by:59863add06847e92f60041a7' (and doing it in the other ways I tested) and it shouldn’t since its created_by attribute is 59863add06847e92f60041a7

This, for example, is the payload for the request of one of my examples:
{"requests":[{"indexName":"local_USER","params":"query=nikon&hitsPerPage=5&facetFilters=id%3A-58d3e8f206847e7c240041b1"},{"indexName":"local_CURATED","params":"query=nikon&hitsPerPage=5"},{"indexName":"local_HINT","params":"query=nikon&hitsPerPage=5&facetFilters=created_by%3A-58d3e8f206847e7c240041b1"}]}

Here is another one (using filters instead of facetFilters):
{"requests":[{"indexName":"local_USER","params":"query=nikon&hitsPerPage=5&filters=NOT%20id%3A58d3e8f206847e7c240041b1"},{"indexName":"local_CURATED","params":"query=nikon&hitsPerPage=5"},{"indexName":"local_HINT","params":"query=nikon&hitsPerPage=5&filters=NOT%20created_by%3A58d3e8f206847e7c240041b1"}]}

None of them is sending attributesForFaceting despite them being set on the code:

query.push({
	indexName: 'local_USER',
	attributesForFaceting: ['created_by'],
	query: 'nikon',
	params: {
		hitsPerPage: 5, 
		facetFilters: 'created_by:-3A58d3e8f206847e7c240041b1'
    }
});

Am I adding it in the wrong place? As per what I understood from your replies, I am doing it correctly.

I am running out ideas, so any help would be much appreciated.

Thank you.

1 Like

Any idea Haroen?

Thanks.

Hi Marcelo!

attributesForFaceting is not a search parameter, it’s an index setting.
Using it at query time won’t have any effect. However, you must make sure you used it in your index settings or that you configured your facets properly in the dashboard before trying to use facetFilters:

Once you’ve configured your index properly, then you can use facetFilters as you did.
Note that in your example, what you’re doing is removing records with created_by = 3A58d3e8f206847e7c240041b1.

Hope that helps!

1 Like