## Getting Started
### Minimal Setup
1. **Installation**
Add the bundle via Composer:
```bash
composer require sitepark/atoolo-search-bundle
Enable it in config/bundles.php:
return [
// ...
Sitepark\AtooloSearchBundle\AtooloSearchBundle::class => ['all' => true],
];
Configuration Publish the default config:
php artisan vendor:publish --tag=atoolo-search-config
Update config/atoolo_search.yaml with your Solr connection details:
solr:
host: 'http://solr:8983/solr'
core: 'atoolo'
First Indexing
Index a resource type (e.g., Article) via CLI:
php artisan atoolo:search:index Article
Or programmatically:
use Sitepark\AtooloSearchBundle\Command\IndexCommand;
$indexer = $this->get(IndexCommand::class);
$indexer->handle(new ArrayObject(['Article']));
Basic Search
Use the SearchQueryBuilder to construct queries:
use Sitepark\AtooloSearchBundle\Query\SearchQueryBuilder;
$query = (new SearchQueryBuilder())
->addTerm('keyword')
->setPage(1)
->setLimit(10)
->build();
$results = $this->get('atoolo_search.search_service')->search($query);
IndexCommand for bulk operations (e.g., cron jobs):
php artisan atoolo:search:index Article --batch-size=50
$resource = $this->get('atoolo_resource.manager')->get('Article', 1);
$this->get('atoolo_search.indexer')->index($resource);
IndexSchema2xDocument to add custom fields:
class CustomDocument extends IndexSchema2xDocument
{
public function getCustomField()
{
return $this->getResource()->getCustomAttribute();
}
}
$query = (new SearchQueryBuilder())
->addTerm('laravel')
->addFilter('category', 'technology')
->addFacet('category')
->setSort('relevance')
->build();
$results = $this->get('atoolo_search.search_service')->search($query);
$facets = $results->getFacets();
$total = $results->getTotal();
SolrSuggest for autocomplete:
$suggestions = $this->get('atoolo_search.solr_suggest')->suggest('laravle');
GeoLocatedFilter for location-based queries:
$query->addFilter(new GeoLocatedFilter(52.5200, 13.4050, 10)); // lat, lng, radius (km)
$similar = $this->get('atoolo_search.more_like_this')->find(123);
# config/atoolo_search.yaml
query_templates:
featured_articles:
filters:
- { field: 'featured', value: 'true' }
sort: 'sp_sortvalue desc'
Then apply:
$query->applyTemplate('featured_articles');
ResourceUpdatedEvent) and trigger indexing:
public function onResourceUpdated(ResourceUpdatedEvent $event)
{
$this->get('atoolo_search.indexer')->index($event->getResource());
}
$this->app->bind('custom.indexer', function () {
return new CustomIndexer();
});
JsonResource:
public function toArray($request)
{
return [
'id' => $this->id,
'title' => $this->title,
'teaser' => $this->teaser,
'url' => route('articles.show', $this->id),
];
}
$cacheKey = 'search_results_' . md5($query->getQuery());
return Cache::remember($cacheKey, 3600, function () use ($query) {
return $this->get('atoolo_search.search_service')->search($query);
});
IndexSchema2xDocument to map custom fields:
class CustomDocument extends IndexSchema2xDocument
{
public function getCustomField()
{
return $this->getResource()->getCustomField();
}
}
Register the mapper in services.yaml:
services:
Sitepark\AtooloSearchBundle\Document\IndexSchema2xDocument:
arguments:
$fieldMappers:
- '@custom.document_mapper'
$query->addModifier(new BoostModifier('featured', 2.0));
Indexing Delays:
sync mode for critical updates:
php artisan atoolo:search:index Article --sync
atoolo:search:worker) for async tasks.Field Mapping Errors:
IndexSchema2xDocument methods return the correct type (e.g., string, int)./solr/#/atoolo/schema) for missing fields.Locale Handling:
$query->setLocale('de');
atoolo_resource.locale_resolver to detect user locale.Permission Groups:
include_groups is set in config/atoolo_search.yaml:
indexer:
include_groups: ['admin', 'editor']
Solr Core Mismatch:
atoolo_search.solr.core in config/atoolo_search.yaml matches your Solr setup.Solr Logs:
Enable debug mode in config/atoolo_search.yaml:
debug: true
Check Solr logs (/var/log/solr.log) for query errors.
Query Inspection: Dump the raw Solr query:
$query = $searchService->buildQuery($searchQuery);
dd($query->getSolrQuery());
Index Validation: Validate indexed documents:
php artisan atoolo:search:validate Article
Custom Indexers:
Extend AbstractIndexer for specialized logic:
class CustomIndexer extends AbstractIndexer
{
protected function getDocumentClass(): string
{
return CustomDocument::class;
}
}
Register in services.yaml:
services:
custom.indexer:
class: App\CustomIndexer
tags: ['atoolo_search.indexer']
Query Filters: Create custom filters (e.g., for tags):
class TagFilter implements FilterInterface
{
public function apply(QueryBuilder $query, $value)
{
$query->addFilter('tags', $value);
}
}
How can I help you explore Laravel packages today?