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, first enrich your records using the same image recognition platform you plan to use for reverse image search. Once you’ve tagged your records, 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
The Algolia engine searches for records using a textual query
or set of query parameters. The first step in transforming an image file into a set of query parameters is to use an image recognition platform. Image recognition platforms such as Google Cloud Vision API take an image and return a set of classifications—or “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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| // 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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| 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,
}
]
}
|
Turn classifications into an Algolia query
Once you’ve extracted classifications from an image, the next step is to 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 first declare the classification attributes in attributesForFaceting
.
First, you need to take each classification and format it properly:
1
2
3
4
| 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.
Then you can pass these as optionalFilters
as a query time parameter. How you do so depends on your frontend implementation.
If you’re using InstantSearch, you can use the configure
widget:
1
2
3
| instantsearch.widgets.configure({
optionalFilters: reduceLabelsToFilters(labels)
});
|
1
2
3
| <ais-configure
[searchParameters]="{ optionalFilters: reduceLabelsToFilters(labels) }"
></ais-configure>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| <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(
'YourApplicationID',
'YourWriteAPIKey'
),
};
},
};
</script>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| import { liteClient as algoliasearch } from 'algoliasearch/lite';
import { InstantSearch } from 'react-instantsearch-dom';
const searchClient = algoliasearch(
'YourApplicationID',
'YourWriteAPIKey'
);
const App = () => (
<InstantSearch
indexName="instant_search"
searchClient={searchClient}
>
<Configure optionalFilters={reduceLabelsToFilters(labels)} />
</InstantSearch>
);
|
If you’re using an API client, you can pass it as a parameter in the search
method:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
| 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 = "<YOUR_SEARCH_QUERY>", OptionalFilters = optionalFilters }
);
await client.SearchSingleIndexAsync<Hit>("<YOUR_INDEX_NAME>", searchParams);
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| 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: "<YOUR_SEARCH_QUERY>", optionalFilters: optionalFilters);
await client.searchSingleIndex(
indexName: "<YOUR_INDEX_NAME>",
searchParams: searchParams,
);
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
| 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("<YOUR_SEARCH_QUERY>").
SetOptionalFilters(&optionalFilters),
)
_, err = client.SearchSingleIndex(client.NewApiSearchSingleIndexRequest(
"<YOUR_INDEX_NAME>").WithSearchParams(searchParams))
if err != nil {
panic(err)
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| 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("<YOUR_SEARCH_QUERY>").setOptionalFilters(optionalFilters);
client.searchSingleIndex("<YOUR_INDEX_NAME>", searchParams, Hit.class);
} catch (Exception e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| import type { OptionalFilters, SearchParams } from 'algoliasearch';
import { algoliasearch } 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: '<YOUR_SEARCH_QUERY>',
optionalFilters: optionalFilters,
};
await client.searchSingleIndex({ indexName: 'indexName', searchParams: searchParams });
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| import com.algolia.client.api.SearchClient
import com.algolia.client.configuration.*
import com.algolia.client.transport.*
import com.algolia.client.extensions.*
import com.algolia.client.model.search.*
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 = "<YOUR_SEARCH_QUERY>", optionalFilters = optionalFilters)
client.searchSingleIndex(
indexName = "<YOUR_INDEX_NAME>",
searchParams = searchParams,
)
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| <?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('<YOUR_SEARCH_QUERY>')
->setOptionalFilters($optionalFilters)
;
$client->searchSingleIndex(
'<YOUR_INDEX_NAME>',
$searchParams,
);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| 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="<YOUR_SEARCH_QUERY>",
optional_filters=optional_filters,
)
_client.search_single_index(
index_name="<YOUR_INDEX_NAME>",
search_params=search_params,
)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| 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: "<YOUR_SEARCH_QUERY>",
optionalFilters: optional_filters
)
client.search_single_index("<YOUR_INDEX_NAME>", search_params)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
| 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("<YOUR_SEARCH_QUERY>"), optionalFilters = Some(optionalFilters))
Await.result(
client
.searchSingleIndex(
indexName = "<YOUR_INDEX_NAME>",
searchParams = Some(searchParams)
)
.map(_ => Future.unit),
Duration(5, "sec")
)
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
| import Foundation
#if os(Linux) // For linux interop
import FoundationNetworking
#endif
import Core
import Search
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: "<YOUR_SEARCH_QUERY>", optionalFilters: optionalFilters)
)
let response: SearchResponse<Hit> = try await client.searchSingleIndex(
indexName: "<YOUR_INDEX_NAME>",
searchParams: searchParams
)
print(response)
}
|