forked from helderjs/InformationRetrieval
/
Search.php
46 lines (37 loc) · 1.24 KB
/
Search.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
include 'vendor/autoload.php';
use ZendSearch\Lucene;
class Search {
/**
* Executa uma busca nos indexes criados
*
* @param $query
*/
public function query($query)
{
$dir = realpath(dirname(__FILE__)) . DIRECTORY_SEPARATOR . "data" .DIRECTORY_SEPARATOR;
$jsonDir = $dir . "json";
$indexDir = $dir . "index";
// Percorre os indices
$files = scandir($jsonDir);
foreach ($files as $file) {
if ($file == '.' || $file == '..') {
continue;
}
$indexName = substr($file, 0, -5);
$index = Lucene\Lucene::open($indexDir . DIRECTORY_SEPARATOR . $indexName); // Abre index
$hits = $index->find($query); // Executa query
// Lista resultados
foreach ($hits as $hit) {
$document = $hit->getDocument();
// return a Zend\Search\Lucene\Field object
// from the Zend\Search\Lucene\Document
echo "<h3>" . $document->getFieldValue('url') . "</h3><br />";
//echo "<p>" . $hit->text . "</p><br /><br />";
}
}
}
}
$q = !empty($_GET['q']) ? $_GET['q'] : 'CBF';
$sc = new Search();
$sc->query($q);