Skip to main content
You can let your users search for products using images, not just words. This querying technique is commonly referred to as reverse image search. It allows users to quickly find more information about a product. This guide shows how to use a third-party API or platform to turn images into Algolia search queries. It uses the all-purpose Google Cloud Vision image recognition API, but you could use other providers like Amazon Rekognition or those made for particular use cases, such as ViSenze for retail. Before implementing reverse image search, enrich your records using the same image recognition platform you plan to use for reverse image search. Once you’ve tagged your , those same classifications can help retrieve the correct record when a user searches using an image.

Before you begin

This guide assumes certain data and software requirements:
  • Algolia records enriched using image classification. It doesn’t cover the UI part of this UX. Specifically, you’ll need to add the ability for users to upload an image to your search box and display results accordingly.
  • Access to an image recognition platform such as Google Cloud Vision API.
You can’t store images directly in Algolia. Instead, store the image on a content delivery network (CDN) or web server and add the image URL to a field in your records. When you retrieve a record from Algolia, use this URL to display the image in your app.

Image classification

1

Transform your images

Since Algolia searches for records using a textual query or set of query parameters, you need to transform your image files into a set of query parameters with an image recognition platform. Image recognition platforms such as Google Cloud Vision API take an image and return a set of classifications, “labels”, for it.Once your users have uploaded an image to use for search, you need to run the image through the same image recognition platform you used to enrich your records. You can use the same function you used to classify images on your records:
JavaScript
// Import the Google Cloud client libraries
const vision = require('@google-cloud/vision');

// Instantiate Google Vision client
const client = new vision.ImageAnnotatorClient();

// Retrieve labels
async function getImageLabels(imageURL, objectID, scoreLimit) {
  const [result] = await client.labelDetection(imageURL);
  const labels = result.labelAnnotations
    .filter((label) => label.score > scoreLimit)
    .map((label) => (
      {
        description: labels.description,
        score: label.score
      }
    ))
  return { imageURL, objectID, labels };
}

const classifiedImage = await getImageLabels("https://images-na.ssl-images-amazon.com/images/I/41uIVaJOLdL.jpg", "439784001", 0.5)
This returns these labels:
JavaScript
const classifiedImage = {
  imageURL: "https://images-na.ssl-images-amazon.com/images/I/41uIVaJOLdL.jpg",
  objectID: "439784001",
  labels: [
    {
      "description": "Outerwear",
      "score": 0.9513528,
    },
    {
      "description": "Azure",
      "score": 0.89286935,
    },
    {
      "description": "Sleeve",
      "score": 0.8724504,
    },
    {
      "description": "Bag",
      "score": 0.86443543,
    },
    {
      "description": "Grey",
      "score": 0.8404184,
    }
  ]
}
2

Turn classifications into an Algolia query

Once you’ve extracted classifications from an image, turn them into an Algolia query. For this, you can send an empty query with classifications as optionalFilters. Optional filters boost results with matching values.To use classifications as optionalFilters, you must declare the classification attributes in attributesForFaceting. Take each classification and format it properly:
JavaScript
function reduceLabelsToFilters(labels) {
  const optionalFilters = labels.map(label => `labels.description:'${label.description}'`);
  return optionalFilters;
}
In this example, image classifications are stored in the label.descriptions nested attribute in each product. You should update the optionalFilters text according to your record format.
3

Pass `optionalFilters` as a query time parameter

How you pass these optionalFilters depends on your frontend implementation. If you’re using InstantSearch, use the configure widget:
instantsearch.widgets.configure({
  optionalFilters: reduceLabelsToFilters(labels)
});
import { liteClient as algoliasearch } from 'algoliasearch/lite';
import { InstantSearch } from 'react-instantsearch-dom';

const searchClient = algoliasearch(
  'undefined',
  'undefined'
);

const App = () => (
  <InstantSearch
    indexName="instant_search"
    searchClient={searchClient}
  >
    <Configure optionalFilters={reduceLabelsToFilters(labels)} />
  </InstantSearch>
);
<template>
  <ais-instant-search
    index-name="instant_search"
    :search-client="searchClient"
  >
    <ais-configure :optionalFilters="reduceLabelsToFilters(labels)" />
  </ais-instant-search>
</template>

<script>
  import { liteClient as algoliasearch } from 'algoliasearch/lite';

  export default {
    data() {
      return {
        searchClient: algoliasearch(
          'undefined',
          'undefined'
        ),
      };
    },
  };
</script>
If you’re using an API client, you can pass it as a parameter in the search method:
namespace Algolia;

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text.Json;
using Algolia.Search.Clients;
using Algolia.Search.Http;
using Algolia.Search.Models.Search;

class SearchWithOptionalFilters
{
  private static readonly List<string> labels =
  [ /* Your labels */
  ];

  private static OptionalFilters ReduceLabelsToFilters(List<string> labels)
  {
    return new OptionalFilters([]); // Implement your logic here
  }

  async Task Main(string[] args)
  {
    var client = new SearchClient(new SearchConfig("ALGOLIA_APPLICATION_ID", "ALGOLIA_API_KEY"));
    var optionalFilters = ReduceLabelsToFilters(labels);
    var searchParams = new SearchParams(
      new SearchParamsObject { Query = "SEARCH_QUERY", OptionalFilters = optionalFilters }
    );

    await client.SearchSingleIndexAsync<Hit>("INDEX_NAME", searchParams);
  }
}

import 'package:algolia_client_search/algolia_client_search.dart';

final List<String> labels = []; // A list of labels

List<String> reduceLabelsToFilters(List<String> labels) {
  return []; // Implement your logic here
}

void searchWithOptionalFilters() async {
  final client =
      SearchClient(appId: 'ALGOLIA_APPLICATION_ID', apiKey: 'ALGOLIA_API_KEY');

  final optionalFilters = reduceLabelsToFilters(labels);
  final searchParams = SearchParamsObject(
      query: "SEARCH_QUERY", optionalFilters: optionalFilters);

  await client.searchSingleIndex(
    indexName: "INDEX_NAME",
    searchParams: searchParams,
  );
}

package main

import (
	"github.com/algolia/algoliasearch-client-go/v4/algolia/search"
)

func reduceLabelsToFilters(_ []string) (search.OptionalFilters, error) {
	return search.OptionalFilters{}, nil // Implement your logic here
}

func searchWithOptionalFilters() {
	labels := []string{ /* A list of labels */ }

	client, err := search.NewClient("ALGOLIA_APPLICATION_ID", "ALGOLIA_API_KEY")
	if err != nil {
		// The client can fail to initialize if you pass an invalid parameter.
		panic(err)
	}

	optionalFilters, err := reduceLabelsToFilters(labels)
	if err != nil {
		panic(err)
	}

	searchParams := search.SearchParamsObjectAsSearchParams(
		search.NewSearchParamsObject().
			SetQuery("SEARCH_QUERY").
			SetOptionalFilters(&optionalFilters),
	)

	_, err = client.SearchSingleIndex(client.NewApiSearchSingleIndexRequest(
		"INDEX_NAME").WithSearchParams(searchParams))
	if err != nil {
		panic(err)
	}
}

package com.algolia;

import com.algolia.api.SearchClient;
import com.algolia.config.*;
import com.algolia.model.search.*;
import java.util.List;

public class searchWithOptionalFilters {

  private static final List<String> labels = List.of(/* Your labels */);

  private static OptionalFilters reduceLabelsToFilters(List<String> labels) {
    return OptionalFilters.of(""); // Implement your logic here
  }

  public static void main(String[] args) throws Exception {
    try (SearchClient client = new SearchClient("ALGOLIA_APPLICATION_ID", "ALGOLIA_API_KEY")) {
      OptionalFilters optionalFilters = reduceLabelsToFilters(labels);
      SearchParams searchParams = new SearchParamsObject().setQuery("SEARCH_QUERY").setOptionalFilters(optionalFilters);

      client.searchSingleIndex("INDEX_NAME", searchParams, Hit.class);
    } catch (Exception e) {
      System.out.println("An error occurred: " + e.getMessage());
    }
  }
}

import { algoliasearch } from 'algoliasearch';

import type { OptionalFilters, SearchParams } from 'algoliasearch';

const labels: string[] = []; // A list of labels

const reduceLabelsToFilters = (_labels: string[]): OptionalFilters => {
  return []; // Implement your logic here
};

const client = algoliasearch('ALGOLIA_APPLICATION_ID', 'ALGOLIA_API_KEY');

const optionalFilters = reduceLabelsToFilters(labels);
const searchParams: SearchParams = {
  query: 'SEARCH_QUERY',
  optionalFilters: optionalFilters,
};

await client.searchSingleIndex({ indexName: 'indexName', searchParams: searchParams });

import com.algolia.client.api.SearchClient
import com.algolia.client.configuration.*
import com.algolia.client.extensions.*
import com.algolia.client.model.search.*
import com.algolia.client.transport.*

val labels: List<String> = listOf() // A list of labels

val reduceLabelsToFilters: (List<String>) -> OptionalFilters = {
  OptionalFilters.of("") // Implement your logic here
}

suspend fun searchWithOptionalFilters() {
  val client = SearchClient(appId = "ALGOLIA_APPLICATION_ID", apiKey = "ALGOLIA_API_KEY")

  val optionalFilters = reduceLabelsToFilters(labels)
  val searchParams =
    SearchParamsObject(query = "SEARCH_QUERY", optionalFilters = optionalFilters)

  client.searchSingleIndex(indexName = "INDEX_NAME", searchParams = searchParams)
}

<?php

require __DIR__.'/../vendor/autoload.php';
use Algolia\AlgoliaSearch\Api\SearchClient;
use Algolia\AlgoliaSearch\Model\Search\OptionalFilters;
use Algolia\AlgoliaSearch\Model\Search\SearchParamsObject;

$labels = []; // A list of labels
$reduceLabelsToFilters = function (array $labels): OptionalFilters {
    // Implement your logic here
    return new OptionalFilters();
};

$client = SearchClient::create('ALGOLIA_APPLICATION_ID', 'ALGOLIA_API_KEY');

$optionalFilters = $reduceLabelsToFilters($labels);
$searchParams = (new SearchParamsObject())
    ->setQuery('SEARCH_QUERY')
    ->setOptionalFilters($optionalFilters)
;

$client->searchSingleIndex(
    'INDEX_NAME',
    $searchParams,
);

from algoliasearch.search.client import SearchClientSync

from algoliasearch.search.models.search_params import SearchParams


def _reduce_labels_to_filters(_labels):
    # Implement your logic here
    return []


labels = []  # A list of labels

_client = SearchClientSync("ALGOLIA_APPLICATION_ID", "ALGOLIA_API_KEY")


optional_filters = _reduce_labels_to_filters(labels)
search_params = SearchParams(
    query="SEARCH_QUERY",
    optional_filters=optional_filters,
)

_client.search_single_index(
    index_name="INDEX_NAME",
    search_params=search_params,
)

require "algolia"

def reduce_labels_to_filters(_labels)
  # Implement your logic here
  []
end

# A list of labels
labels = []

client = Algolia::SearchClient.create("ALGOLIA_APPLICATION_ID", "ALGOLIA_API_KEY")

optional_filters = reduce_labels_to_filters(labels)
search_params = Algolia::Search::SearchParamsObject.new(
  query: "SEARCH_QUERY",
  optionalFilters: optional_filters
)

client.search_single_index("INDEX_NAME", search_params)

import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Await
import scala.concurrent.duration.Duration

import algoliasearch.api.SearchClient
import algoliasearch.config.*
import algoliasearch.extension.SearchClientExtensions
import algoliasearch.search.OptionalFilters.SeqOfOptionalFilters
import algoliasearch.search.SearchParamsObject

val labels: List[String] = List() // A list of labels

val reduceLabelsToFilters: Seq[String] => SeqOfOptionalFilters = _ => {
  SeqOfOptionalFilters(Seq()) // Implement your logic here
}

def searchWithOptionalFilters(): Future[Unit] = {
  val client = SearchClient(appId = "ALGOLIA_APPLICATION_ID", apiKey = "ALGOLIA_API_KEY")

  val optionalFilters = reduceLabelsToFilters(labels)
  val searchParams = SearchParamsObject(query = Some("SEARCH_QUERY"), optionalFilters = Some(optionalFilters))

  Await.result(
    client
      .searchSingleIndex(
        indexName = "INDEX_NAME",
        searchParams = Some(searchParams)
      )
      .map(_ => Future.unit),
    Duration(5, "sec")
  )
}

import Foundation
#if os(Linux) // For linux interop
    import FoundationNetworking
#endif

import AlgoliaCore
import AlgoliaSearch

let labels: [String] = [] // A list of labels

let reduceLabelsToFilters = { (_: [String]) in
    SearchOptionalFilters.arrayOfSearchOptionalFilters([]) // Implement your logic here
}

func searchWithOptionalFilters() async throws {
    let client = try SearchClient(appID: "ALGOLIA_APPLICATION_ID", apiKey: "ALGOLIA_API_KEY")

    let optionalFilters = reduceLabelsToFilters(labels)
    let searchParams = SearchSearchParams.searchSearchParamsObject(
        SearchSearchParamsObject(query: "SEARCH_QUERY", optionalFilters: optionalFilters)
    )

    let response: SearchResponse<Hit> = try await client.searchSingleIndex(
        indexName: "INDEX_NAME",
        searchParams: searchParams
    )
    print(response)
}

See also

Last modified on May 19, 2026