PHP code example of nqxcode / search-engine

1. Go to this page and download the library: Download nqxcode/search-engine 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/ */

    

nqxcode / search-engine example snippets


$searchEngine = new SearchEngine\Engine('Product', $indexDirectory); // $indexDirectory path to index directory

use SearchEngine\ISearchable;

class Product implements ISearchable
{
    // TODO ...
}

use SearchEngine\ISearchable;

class Product implements ISearchable
{
    // ...

    public funtion getAttributesForIndexing()
    {
        // list of couples "field name - field value"
        return array(
            new Attribute('fieldName', $this->fieldName),
            new Attribute('otherFieldName', $this->otherFieldName)
        );
    }
}

$searchEngine->fullUpdateIndex();

$searchEngine->updateIndex($model);

$searchEngine->deleteIndex($model);

/**
 * @var ZendSearch\Lucene\Search\QueryHit[] $hits
 */
$queryHits = $searchEngine->search($query);

/**
 * @var SearchEngine\Result\Hit[] $hits
 */
$hits = $searchEngine->parseHitsByRange($queryHits, $elementsPerPage, $currentPage);

// get the found ISearchable model from each $hit
foreach ($hits as $hit):
    $model = $hit->getItem();
}

/**
 * @var SearchEngine\Result\Hit[] $hits
 */
$hits = $searchEngine->parseHits($queryHits);

// get the found ISearchable model from each $hit
foreach ($hits as $hit):
    $model = $hit->getItem();
}