Qar
Qar

Reputation: 1804

Algolia search for array that contains value

I am using Algolia search, and right now I use this to find a specific item by id:

algolia.getObject(id)

However, I need to make a search by barcode rather than ID - need a pointer in the right direction here.

The barcodes field is an array that can contain one or more barcode numbers.

Upvotes: 1

Views: 326

Answers (1)

Samuel Vaillant
Samuel Vaillant

Reputation: 3847

You can trigger a search with filters on the barcodes attributes. The filters parameter supports multiple format, numeric values included. It does not matter if the attribute hold a single or multiple (an array) values. Here is an example with the JavaScript client:

const algoliasearch = require('algoliasearch');
const client = algoliasearch('YOUR_APP_ID', 'YOUR_API_KEY');
const index = client.initIndex('YOUR_INDEX_NAME');

index
  .search({
    filters: 'barcodes = YOUR_BARCODE_VALUE',
  })
  .then(response => {
    console.log(response.hits);
  });

The above example assumes that your records have a structure like this one:

{
  "barcodes": [10, 20, 30]
}

Upvotes: 1

Related Questions