angelvilaplana / module-visual-search
Search the Magento 2 catalog with a photo: image embeddings and OpenSearch k-NN vector search.
Package info
github.com/angelvilaplana/magento2-visual-search
Type:magento2-module
pkg:composer/angelvilaplana/module-visual-search
Requires
- php: ~8.2.0||~8.3.0||~8.4.0||~8.5.0
- magento/framework: ^103.0.7
- magento/module-advanced-search: ^100.4.5
- magento/module-catalog: ^104.0.7
- magento/module-catalog-search: ^102.0.7
- magento/module-cron: ^100.4.7
- magento/module-open-search: ^100.4.1
- magento/module-search: ^101.1.7
- magento/module-store: ^101.1.7
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Let shoppers search your catalog with a photo. A camera button inside the search field opens the camera on phones and the file picker on desktop, where an image can also be dragged onto the search field. Shoppers get visually similar products, with the same filters, sorting and pagination as a text search.
- Works with any catalog: no training, no product type assumptions.
- Results are shown on Magento's own search result page: layered navigation, sorting, pagination, swatches, compare and wishlist keep working.
- Recognizes the product type in the photo from your category names and shows only products of that type.
- Shows no results for products you do not sell, instead of unrelated products.
- Self-hosted and open source: shopper photos are never stored and never sent to third parties.
| Mobile: camera, searching, results | Desktop: drag and drop |
|---|---|
![]() |
![]() |
How it works
Indexing product main images ─► embedding server ─► vectors ─► OpenSearch k-NN index (one per store view)
Searching shopper photo ─► embedding server ─► vector ─► OpenSearch k-NN ─► Magento search result page
An image embedding model turns every image into a vector; similar images produce nearby vectors. The module indexes the main image of each product and of each configurable child (one per color), and searches the nearest products to the shopper photo. The model also understands text, which is used to recognize the product type in the photo from your category names.
Requirements
| Component | Version |
|---|---|
| Magento Open Source or Adobe Commerce | 2.4.7 – 2.4.9 |
| PHP | 8.2 – 8.5 |
| Search engine | OpenSearch 2.x or 3.x with the k-NN plugin |
| Embedding server | Required, see below |
| Magento cron | Required (partial reindex and cache warm-up) |
Tested with Magento 2.4.9, PHP 8.5 and OpenSearch 3.6.
The k-NN plugin is included in the default OpenSearch distribution and in Amazon OpenSearch Service. Check it is available with:
curl -s http://<opensearch-host>:9200/_cat/plugins | grep knn
Elasticsearch is not supported.
Embedding server (required)
The module does not compute embeddings itself: it sends images to an embedding server over HTTP. Without it, nothing can be indexed or searched. The supported server is Weaviate multi2vec-clip (BSD-3-Clause), a small inference service published as Docker images, one per model. It does not depend on the Weaviate database.
Run it next to Magento, on a network Magento can reach:
docker run -d --name visual-search-embedder --restart unless-stopped \ -p 127.0.0.1:9090:8080 \ semitechnologies/multi2vec-clip:openai-clip-vit-base-patch16-1.5.1
Or with Docker Compose:
services: embedder: image: semitechnologies/multi2vec-clip:openai-clip-vit-base-patch16-1.5.1 restart: unless-stopped ports: - "127.0.0.1:9090:8080"
Check it is ready (it takes up to a minute to load the model):
curl http://127.0.0.1:9090/meta
Resources with the recommended model (openai-clip-vit-base-patch16, 512-dimension vectors):
| Resource | Usage |
|---|---|
| Download / disk | ~8 GB / ~19 GB |
| Memory | ~3 GB |
| CPU time per image | ~0.1–0.25 s (an NVIDIA GPU makes it much faster: start the container with --gpus all -e ENABLE_CUDA=1) |
Other models: other models are published as image tags, e.g. sentence-transformers-clip-ViT-B-32-multilingual-v1-1.5.1 for category names that are not in English. After changing the model, run a full reindex and bin/magento cache:flush, and review the similarity settings below, which depend on the model. SigLIP models are not supported yet: the server tokenizes their text incorrectly (weaviate/multi2vec-clip-inference#56).
Securing the embedding server
The embedding server has no authentication of its own: anyone who can reach it can use it. Never expose it to the internet directly.
- Same server or private network (recommended): bind it to localhost as above, or keep it on an internal network or behind a firewall. No API key is needed.
- Separate server (e.g. a GPU server, or a hosting platform that cannot run it next to Magento): put it behind a reverse proxy that serves HTTPS and accepts only requests with your API key. The module sends the key in an
Authorization: Bearer <key>header.
Generate a key:
openssl rand -hex 32
Example nginx configuration on the embedding server, with the embedder listening on 127.0.0.1:9090:
map $http_authorization $embedder_authorized { default 0; "Bearer <your-api-key>" 1; } server { listen 443 ssl; server_name embedder.example.com; ssl_certificate /etc/ssl/certs/embedder.example.com.crt; ssl_certificate_key /etc/ssl/private/embedder.example.com.key; client_max_body_size 64m; if ($embedder_authorized = 0) { return 401; } location = /vectorize { limit_except POST { deny all; } proxy_request_buffering off; proxy_http_version 1.1; proxy_set_header Authorization ""; proxy_pass http://127.0.0.1:9090; } location = /meta { limit_except GET { deny all; } proxy_set_header Authorization ""; proxy_pass http://127.0.0.1:9090; } location / { return 404; } }
The proxy only allows the two endpoints the module and the checks below use, does not forward the key to the embedder, and streams images to it without writing them to disk (proxy_request_buffering off). client_max_body_size must fit an indexing batch: lower Indexing Batch Size if requests are rejected with 413.
Check the proxy from the Magento server:
curl -H "Authorization: Bearer <your-api-key>" https://embedder.example.com/meta
Then set Embedding Server > Endpoint to https://embedder.example.com and Embedding Server > API Key to the key, in the admin or with:
bin/magento config:set visual_search/embedder/api_key <your-api-key>
The key is encrypted with Magento's encryption key before it is stored, is only sent by the server (it never reaches the browser) and is marked as sensitive, so bin/magento app:config:dump does not write it to app/etc/config.php.
Installation
composer require angelvilaplana/module-visual-search bin/magento module:enable AngelVilaplana_VisualSearch bin/magento setup:upgrade
In production mode, also run bin/magento setup:di:compile and bin/magento setup:static-content:deploy.
Then:
-
Go to Stores > Configuration > Catalog > Visual Search.
-
Set Embedding Server > Endpoint to the URL Magento uses to reach the embedding server (e.g.
http://127.0.0.1:9090). If it is behind an authenticating proxy, also set Embedding Server > API Key (see Securing the embedding server). -
Set General > Enabled to Yes.
-
Build the index and keep it up to date with cron:
bin/magento indexer:reindex angelvilaplana_visual_search bin/magento indexer:set-mode schedule angelvilaplana_visual_search
The first full reindex takes about 0.1–0.3 s per product image on CPU.
Configuration
Stores > Configuration > Catalog > Visual Search
| Setting | Default | Description |
|---|---|---|
| General > Enabled | No | Shows the camera button and enables indexing for the store view. |
| Embedding Server > Endpoint | http://embedder:8080 |
Base URL of the embedding server. |
| Embedding Server > API Key | Empty | Sent as Authorization: Bearer <key> to an embedding server behind an authenticating proxy (see Securing the embedding server). Stored encrypted. |
| Embedding Server > Timeout (seconds) | 30 | Request timeout. |
| Embedding Server > Indexing Batch Size | 16 | Images sent per request while indexing. |
| Search > Maximum Results | 48 | Maximum number of similar products, paginated and filterable on the result page. |
| Search > Maximum Upload Size (MB) | 5 | Largest accepted image. Photos are resized in the browser before upload. |
| Search > Searches per Minute per Client | 10 | Rate limit per IP address. |
| Search > Relevance Margin | 0.12 | Products less similar than the best match by more than this value are not shown. Model dependent. |
| Search > Near-Identical Similarity | 0.88 | Products at least this similar are shown even outside the recognized category. Model dependent. |
| Category Recognition > Enabled | Yes | Shows only products of the category recognized in the photo. |
| Category Recognition > Categories | All active leaf categories | Categories that describe product types. |
| Category Recognition > Minimum Confidence | 0.5 | Probability required to apply the recognized category. |
| Category Recognition > Detect Products Not in the Catalog | Yes | Shows no results for photos of product types the store does not sell. |
Recommended settings
- Category Recognition > Categories: select only categories that describe product types (Jackets, Bags, Pants…) and leave out marketing ones (Sale, New Arrivals…). Marketing categories mix product types and make the recognition less reliable.
- Layered navigation: the result page shows the attributes with Use in Search Results Layered Navigation enabled (Stores > Attributes > Product), exactly like the text search page.
- Category names: the default model understands English. Use a multilingual model for other languages.
Result page
Results use Magento's catalog search result page (/visualsearch/result/?token=…): the module's layout includes the catalogsearch_result_index handle, so everything Magento and other modules add to that page is available. A frontend plugin on Magento\OpenSearch\SearchAdapter\Mapper::buildQuery() restricts the catalog quick search to the similar products and ranks them by similarity, while Magento builds filters and aggregations as usual. The plugins only act on this page.
The page heading shows the shopper photo, kept in the browser session storage; it is not uploaded again. Results are kept for one hour, or until the cache is flushed.
Security and privacy
- Uploaded images are validated with Magento's uploader, checked by their real content type and limited to 25 megapixels before being decoded, which prevents decompression bomb attacks.
- Images are re-encoded before use, discarding metadata and any non-image content, and are never stored.
- Search requests require a valid form key and are rate limited per IP address.
- The embedding server API key is stored encrypted and is never sent to the browser or written to the logs.
- Only validation errors are shown to shoppers; internal errors are logged in
var/log/exception.log.
Performance
- A search takes about 0.2–0.3 s on CPU: the embedding (~0.1–0.25 s) plus a few OpenSearch queries (milliseconds).
- Text embeddings of the category names are computed once and cached; they are only recomputed when category names change. The
angelvilaplana_visual_search_warm_up_label_cachecron job (every 5 minutes) and every full reindex compute them in advance, so no shopper waits after a cache flush or a category rename. - Full reindexes build a new index and switch an alias when complete, so search keeps working during the rebuild. Product changes are reindexed by cron through Magento's materialized views.
Limitations
- Luma-based themes. For Hyvä, install the Hyvä compatibility module (
angelvilaplana/module-visual-search-hyva). - OpenSearch only.
- The main image of the default scope is indexed; store-view-specific main images are ignored.
- Category recognition and out-of-catalog detection rely on English names with the default model.
- Out-of-catalog detection only knows the generic product types in its list (see Extending).
Troubleshooting
| Symptom | Check |
|---|---|
| No camera button | General > Enabled is Yes for the store view; flush the cache. |
| "Something went wrong" when searching | var/log/exception.log and var/log/system.log; usually the embedding server is not reachable from Magento (curl <endpoint>/meta from the Magento server). |
Unexpected embedding server response. {"status":401} in var/log/system.log |
Embedding Server > API Key does not match the key of the proxy. |
| No results for any photo | The angelvilaplana_visual_search index was built (bin/magento indexer:status) and OpenSearch has the k-NN plugin. |
| Products of the wrong type | Restrict Category Recognition > Categories to product-type categories. |
| No filters on the result page | Enable Use in Search Results Layered Navigation on the product attributes. |
Extending
- Embedding server: implement
AngelVilaplana\VisualSearch\Api\EmbeddingProviderInterfaceand declare it as a preference in your module'sdi.xml. - Generic product types: extend the
genericLabelsargument ofAngelVilaplana\VisualSearch\Model\Search\OutOfCatalogDetectorindi.xml.
Running the tests
Run them from the Magento test folders, which their configuration files expect:
# Unit tests cd dev/tests/unit ../../../vendor/bin/phpunit -c phpunit.xml.dist ../../../vendor/angelvilaplana/module-visual-search/Test/Unit # Integration tests (database and OpenSearch) cd dev/tests/integration ../../../vendor/bin/phpunit -c phpunit.xml.dist ../../../vendor/angelvilaplana/module-visual-search/Test/Integration
Integration tests create and delete OpenSearch indices. Set a dedicated opensearch-index-prefix in dev/tests/integration/etc/install-config-mysql.php (e.g. magento2_integration); otherwise the test installation shares the store's prefix and overwrites its indices.



