Learn how to allow users to use images as search queries.
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.
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.
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:
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:
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.
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 labelsList<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 mainimport ( "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 labelsconst 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.SearchClientimport 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 labelsval 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)}
<?phprequire __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 SearchClientSyncfrom algoliasearch.search.models.search_params import SearchParamsdef _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 labelslabels = []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)