PHP code example of andydefer / inverted-index-search
1. Go to this page and download the library: Download andydefer/inverted-index-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/ */
andydefer / inverted-index-search example snippets
use AndyDefer\AlgoKIT\Algorithms\InvertedIndex;
use AndyDefer\InvertedIndexSearch\Services\InvertedIndexSearchService;
use AndyDefer\InvertedIndexSearch\Services\InvertedIndexExpressionEvaluator;
use AndyDefer\StorageKit\Storage\MemoryStorage;
$storage = new MemoryStorage();
$index = new InvertedIndex($storage, 'documents');
$evaluator = new InvertedIndexExpressionEvaluator($index);
$search = new InvertedIndexSearchService($index, $evaluator);
// Indexation des documents
$documents = [
['id' => 'doc1', 'tokens' => ['php', 'laravel', 'web']],
['id' => 'doc2', 'tokens' => ['php', 'python', 'data']],
['id' => 'doc3', 'tokens' => ['php', 'laravel', 'vuejs']],
['id' => 'doc4', 'tokens' => ['python', 'django', 'web']],
];
foreach ($documents as $doc) {
$index->add(InvertedIndexRecord::from([
'document_id' => $doc['id'],
'tokens' => $doc['tokens'],
]));
}
// Recherches
$results1 = $search->and(['php', 'laravel']);
// → doc1, doc3
$results2 = $search->or(['php', 'python']);
// → doc1, doc2, doc3, doc4
$results3 = $search->not('php', 'python');
// → doc1, doc3
$results4 = $search->expression('(php AND laravel) OR (python AND django)');
// → doc1, doc3, doc4
// Requête : php AND (laravel OR vuejs) AND NOT python
$results = $search->query()
->where('php')
->whereGroup(function($q) {
$q->where('laravel')
->orWhere('vuejs');
})
->whereNot('python')
->get();
// Construction dynamique
$builder = $search->query();
if ($mustHave) {
foreach ($mustHave as $token) {
$builder->where($token);
}
}
if ($shouldHave) {
$builder->whereGroup(function($q) use ($shouldHave) {
foreach ($shouldHave as $token) {
$q->orWhere($token);
}
});
}
$results = $builder->get();
// Expression simple
$results = $evaluator->evaluate('php AND laravel');
// Expression avec NOT
$results = $evaluator->evaluate('php AND (NOT python)');
// Expression complexe imbriquée
$results = $evaluator->evaluate('(php AND laravel) OR (python AND django)');
// Expression avec plusieurs niveaux
$results = $evaluator->evaluate('php AND (laravel OR vuejs) AND (NOT python)');
// Syntaxe directe
$results = $search->and(['php', 'laravel']);
// Via expression
$results = $search->expression('php AND laravel');
// Via QueryBuilder
$results = $search->query()->where('php')->where('laravel')->get();
// Syntaxe directe
$results = $search->or(['php', 'python']);
// Via expression
$results = $search->expression('php OR python');
// Via QueryBuilder
$results = $search->query()->where('php')->orWhere('python')->get();
// Syntaxe directe
$results = $search->not('php', 'python');
// Via expression
$results = $search->expression('php AND (NOT python)');
// Via QueryBuilder
$results = $search->query()->where('php')->whereNot('python')->get();