Search Architecture
Search Index Table
-- catalogsearch_fulltext (search index)
SELECT entity_id, data_index
FROM catalogsearch_fulltext
WHERE store_id = 1
LIMIT 5;
+-----------+-------------------------------------------------------------------+
| entity_id | data_index |
+-----------+-------------------------------------------------------------------+
| 1 | T-Shirt Blue Cotton comfortable summer wear |
| 2 | T-Shirt Red Polyester lightweight sports |
| 3 | iPhone 15 Pro Max smartphone mobile phone |
+-----------+-------------------------------------------------------------------+
-- Search attributes
SELECT * FROM catalogsearch_fulltext_rlpt
WHERE store_id = 1 AND entity_id = 1;
+-----------+--------------+-----------+----------+--------+
| entity_id | attribute_id | store_id | data_index | weight|
+-----------+--------------+-----------+----------+--------+
| 1 | 71 | 1 | T-Shirt | 1.0 |
| 1 | 73 | 1 | Blue Cotton comfortable | 1.0 |
+-----------+--------------+-----------+----------+--------+
Search Engine Options
| Engine | Description | Use Case |
|---|---|---|
| MySQL | Default fulltext search | Small catalogs |
| Elasticsearch | Open-source search | Medium catalogs |
| OpenSearch | AWS fork of Elasticsearch | Large catalogs |
| Algolia | SaaS search service | Enterprise |
| Apache Solr | Enterprise search | Large catalogs |
Search Configuration
Engine Configuration
// app/config.php
return [
'catalog' => [
'search' => [
'engine' => 'elasticsearch8',
'elasticsearch8_server_hostname' => 'localhost',
'elasticsearch8_server_port' => '9200',
'elasticsearch8_index_prefix' => 'magento',
'elasticsearch8_enable_auth' => '0',
'elasticsearch8_username' => '',
'elasticsearch8_password' => '',
]
]
];
Search Configuration Options
Admin > Stores > Configuration > Catalog > Catalog Search
1. Search Engine:
- MySQL
- Elasticsearch 8
- OpenSearch
2. Index Prefix:
- magento (for multiple installations)
3. Enable Autocomplete:
- Yes/No
- Min Characters: 3
- Max Suggestions: 10
4. Search Suggestions:
- Show Results Count: Yes
- Show Descriptions: Yes
Reindex Search
# Reindex search
bin/magento indexer:reindex catalogsearch_fulltext
# Check status
bin/magento indexer:status catalogsearch_fulltext
# Force reindex
bin/magento indexer:reindex catalogsearch_fulltext --force
Search Criteria and Query
Search Collection
// Basic search
$collection = $objectManager->get(
\Magento\Catalog\Model\ResourceModel\Product\Collection::class
);
$collection->addSearchFilter('T-Shirt');
// Search with specific attributes
$collection->addAttributeToFilter('name', ['like' => '%T-Shirt%']);
$collection->addAttributeToFilter('description', ['like' => '%cotton%']);
// Fulltext search
$collection->addFieldToFilter('search_name', ['like' => '%T-Shirt%']);
Search Criteria API
// Using SearchCriteriaBuilder
$searchCriteriaBuilder = $objectManager->get(
\Magento\Framework\Api\SearchCriteriaBuilder::class
);
$searchCriteria = $searchCriteriaBuilder
->addFilter('name', '%T-Shirt%', 'like')
->addFilter('status', 1)
->addFilter('visibility', ['in' => [2, 4]])
->create();
$productRepository = $objectManager->get(
\Magento\Catalog\Api\ProductRepositoryInterface::class
);
$products = $productRepository->getList($searchCriteria);
foreach ($products->getItems() as $product) {
echo $product->getName();
}
Search Results Processing
// Get search results
$query = $objectManager->get(
\Magento\Search\Model\Query::class
);
$results = $query->load();
// Get product results
$products = $results->getProductCollection();
// Get search suggestions
$suggestions = $results->getSuggestedResults();
// Get related categories
$categories = $results->getCategoryCollection();
Autocomplete
// Autocomplete suggestions
$autocomplete = $objectManager->get(
\Magento\Search\Model\Autocomplete::class
);
$suggestions = $autocomplete->suggest('T-Shirt');
// Returns:
// [
// ['title' => 'T-Shirt Blue', 'type' => 'product'],
// ['title' => 'T-Shirt Red', 'type' => 'product'],
// ['title' => 'T-Shirts', 'type' => 'category'],
// ]
Search Optimization
Searchable Attributes
// Mark attribute as searchable
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'custom_searchable',
[
'type' => 'varchar',
'input' => 'text',
'label' => 'Custom Searchable',
'searchable' => true,
'filterable' => true,
'used_in_product_listing' => true,
]
);
Relevance Weighting
// Configure relevance weights
// Elasticsearch: set boost on attributes
$searchRequestBuilder = $objectManager->get(
\Magento\Search\Model\QueryFactory::class
);
// Name: weight 10
// SKU: weight 8
// Description: weight 5
// Short Description: weight 7
Search Performance
// Optimize search queries
$collection->addAttributeToSelect(['name', 'sku', 'price', 'image']);
// Only load needed attributes
// Use limit
$collection->setPageSize(20);
// Use index
$collection->addFieldToFilter('status', 1);
// Avoid leading wildcards
// BAD: $collection->addSearchFilter('%T-Shirt%');
// GOOD: $collection->addSearchFilter('T-Shirt%');
Search Configuration Best Practices
1. Use Elasticsearch/OpenSearch for large catalogs
2. Configure autocomplete with min characters
3. Set appropriate relevance weights
4. Index all searchable attributes
5. Monitor search analytics
6. Test search queries regularly
7. Use synonyms for common terms
8. Configure search suggestions
Quiz
1. What is the default search engine in Magento?
2. How do you reindex search?
3. What affects search relevance?
Flashcards
Question
What is catalog search?
Click to reveal answer
Answer
Fulltext search functionality for finding products in catalog
Question
What search engines are available?
Click to reveal answer
Answer
MySQL, Elasticsearch, OpenSearch, Algolia, Solr
Question
How to make attribute searchable?
Click to reveal answer
Answer
Set searchable=true in attribute configuration
Question
What is relevance weighting?
Click to reveal answer
Answer
Boost values determining how much each attribute affects search ranking
Question
How to reindex search?
Click to reveal answer
Answer
bin/magento indexer:reindex catalogsearch_fulltext
Revision Notes
Key Takeaways
- 1. Catalog search uses fulltext index (catalogsearch_fulltext table)
- 2. Available engines: MySQL, Elasticsearch, OpenSearch, Algolia, Solr
- 3. Configure searchable attributes and relevance weights
- 4. Search Criteria API for programmatic search
- 5. Reindex search after product changes
Interview Tips
- • Explain search engine options and trade-offs
- • Discuss relevance weighting and configuration
- • Describe search performance optimization
Cheat Sheet
Search Tables:
catalogsearch_fulltext → Fulltext search index
catalogsearch_query → Search queries
catalogsearch_suggest → Search suggestions
Search Engines:
MySQL (default), Elasticsearch 8, OpenSearch
Configuration:
catalog/search/engine = elasticsearch8
catalog/search/autocomplete = 1
catalog/search/min_chars = 3
Reindex:
bin/magento indexer:reindex catalogsearch_fulltext
Attributes:
searchable = true
filterable = true
used_in_product_listing = true