PHP code example of baraja-core / doctrine-fulltext-search
1. Go to this page and download the library: Download baraja-core/doctrine-fulltext-search library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
baraja-core / doctrine-fulltext-search example snippets
use Baraja\Search\Search;
use Doctrine\ORM\EntityManagerInterface;
$search = new Search($entityManager);
$search = new Search(
em: $entityManager,
queryNormalizer: new CustomQueryNormalizer(),
scoreCalculator: new CustomScoreCalculator(),
);
$entityMap = [
Article::class => [
':title', // Title column - always shown
'description', // Normal - searched and in snippet
'!slug', // Search only - searched but not in snippet
'_authorId', // Select only - loaded but not searched
],
];
$query = 'conference 2020..2024';
// Finds results containing years 2020, 2021, 2022, 2023, or 2024
$results = $search->search($query, $entityMap);
// Total count
$count = $results->getCountResults();
// Search time in milliseconds
$time = $results->getSearchTime();
// "Did you mean?" suggestion
$suggestion = $results->getDidYouMean();
// Iterate results
foreach ($results as $item) {
echo $item->getTitle();
}
// Get first 10 results
$items = $results->getItems();
// With pagination
$items = $results->getItems(limit: 20, offset: 40);
// Filter by entity type
$articles = $results->getItemsOfType(Article::class, limit: 10);
// Get only IDs
$ids = $results->getIds(limit: 100);
echo $results;
$results = $search->search('programing', $entityMap);
if ($results->getCountResults() === 0) {
$suggestion = $results->getDidYouMean();
if ($suggestion !== null) {
echo "Did you mean: $suggestion?"; // "programming"
}
}
$results = $search->search($query, $entityMap, useAnalytics: false);
// Or with SelectorBuilder
$results = $search->selectorBuilder($query)
->addEntity(Article::class)
->addColumnTitle('title')
->search(useAnalytics: false);
use Baraja\Search\ScoreCalculator\IScoreCalculator;
class CustomScoreCalculator implements IScoreCalculator
{
public function process(string $haystack, string $query, string $mode = null): int
{
// Your custom scoring logic
return $score;
}
}
use Baraja\Search\QueryNormalizer\IQueryNormalizer;
class CustomQueryNormalizer implements IQueryNormalizer
{
public function normalize(string $query): string
{
// Your normalization logic
return $normalizedQuery;
}
}
$container = new Container(
entityManager: $em,
searchTimeout: 5000, // 5 seconds
);
$search = new Search($em, container: $container);