Setting Searchable Attributes for Multi-Index Search

Hi there, I recently adopted Algolia for an price check web app that I’m building. I have a NodeJs backend which the web app connects to. So I’m using the Javascript Client SDK with node. My question is how can I set searchable attributes on multiple queries when searching from multiple indexes. My Code is as follows

                    //results is from a a database query.
                    results.forEach((shop, k) => {
                        var tmp = {
                            indexName: shop.shop_uid + '-products',
                            query: query
                        }
                        queries.push(tmp);
                    })

                    var algoliaClient = req.app.get('algoliaClient');

                    algoliaClient.search(queries, (e, content) => {
                         ... //custom code here.
                    })

What could I do better.

Thanks, Regards.

Hey bzmpilime,

I’d be happy to assist you!
To clarify, searchable attributes are not set on a query, they are set on an index.
You only have to set it once on an index, to determine which attributes of the records will be searchable.
In order to provide super fast results, while building the index, we optimize it to search through these attributes specifically.
This is why the searchable attributes setting is only applicable on indexing, and not on queries.

Were you by chance referring to atrributesToRetrieve ?
This is a query time parameter that allows you to determine which attributes we will return in the JSON response.

Here’s a quick example.

index.search({
  query: 'query',
  attributesToRetrieve: [
    "title",
    "content"
  ]
})

I’d love to know more about what you are trying to accomplish, so I can better assist you.
It appears you are trying to pull some data from a DB and push it to Algolia, is this correct?

Look forward to hearing back!

Best,
Nick

3 Likes

Hi @nick.vonholtermann, thanks for responding.

Maybe my question should have been how can I set the searchable attributes on the multiple indexes because my search should only search from specific attributes not all attributes of the object stored in the index, but searching through multiple of these indexes.

Hey @bzmpilime,

To set the searchable attributes through the JS client, you can use this function:

var client = algoliasearch('applicationID', 'apiKey');
var index = client.initIndex('indexName');

index.setSettings({
    searchableAttributes: [
        'title,alternative_title',
        'author',
        'unordered(text)'
      ]
});

Alternatively, you can set these attributes in the dashboard very easily by going to the rankings tab.
Let me know if you have any other questions!

Best,
Nick

this is for a single index right? What about for many indexes searching at the same time.

Correct, that is the operation for a single index.
In order to do operations on more than one index, you can initialize as many as you’d like:

var client = algoliasearch('applicationID', 'apiKey');

var index_1 = client.initIndex('index_1_Name');
var index_2 = client.initIndex('index_2_Name');

Now you can change the settings on them individually:

index_1.setSettings({
searchableAttributes: [
    'title'
  ]
});

index_2.setSettings({
searchableAttributes: [
    'author'
  ]
});

Keep in mind that these settings only need to be set one time on each index, and will persist for future queries.

Hope this was helpful!

Thanks @nick.vonholtermann, I was hoping I could get something that could work well with this implementation https://www.algolia.com/doc/api-client/javascript/search/#search-multiple-indices. Anyway I guess I will just have to go with the way of applying the settings on creating the index.

Thanks anyway.

Ah, I see!

It is actually possible to search in different attributes when using the “search into multiple indices” feature.
It is just important to configure the index, prior to doing so.

There are two important steps when using Algolia:

  1. Set an index settings (this is where you’ll declare which attributes are searchable in this index) - you only need to do this once.
  2. Make a search on one or multiple indices (you can’t change the searchable attributes here)

Let’s take an example with two indices (movies and songs).

  1. To configure the searchable attributes (which is generally done when you initially implement Algolia).
    note: this is also accessible in the dashboard, in the ranking tab of an index
//index configuration
var client = algoliasearch('applicationID', 'apiKey');

var movies = client.initIndex('movies');
movies.setSettings({
searchableAttributes: [
    'director'
    ]
});

var songs = client.initIndex('songs');
songs.setSettings({
searchableAttributes: [
    'artist'
    ]
});
  1. Now that the settings are saved, we can proceed to the user facing search portion and construct our query. This is where you will actually send a search query to Algolia. Here you can specify multiple indices, and set search params on each query individually.
//search query
var queries = [{
    indexName: 'movies',
    query: 'james cameron',
    params: {
        hitsPerPage: 3
    }
}, {
    indexName: 'songs',
    query: 'frank ocean',
    params: {
        hitsPerPage: 3
    }
}]

client.search(queries, searchCallback);

Since we have previously set the searchableAttributes:

  • Your first query will search in the attribute director of the movies index.
  • Your second query will search in the attribute artist of the songs index.

Does this make sense? Let me know if you need more detail :smile:

Thanks, I just hoped there was a on-the-fly way.

Anyway thanks, you’ve been of help

1 Like

No problem, feel free to reach out again with any further questions!

A post was split to a new topic: Setting up indices for multiple tables

what is “query” here
can i retrieve all attributes of query

can you please explain me what is “query” here where i should write query.