TeamTNT\TNTSearch\TNTSearch::searchBoolean PHP Method

searchBoolean() public method

public searchBoolean ( $phrase, $numOfResults = 100 )
    public function searchBoolean($phrase, $numOfResults = 100)
    {
        $stack = [];
        $startTimer = microtime(true);
        $expression = new Expression();
        $postfix = $expression->toPostfix("|" . $phrase);
        foreach ($postfix as $token) {
            if ($token == '&') {
                $left = array_pop($stack);
                $right = array_pop($stack);
                if (is_string($left)) {
                    $left = $this->getAllDocumentsForKeyword($this->stemmer->stem($left), true)->pluck('doc_id');
                }
                if (is_string($right)) {
                    $right = $this->getAllDocumentsForKeyword($this->stemmer->stem($right), true)->pluck('doc_id');
                }
                if (is_null($left)) {
                    $left = [];
                }
                if (is_null($right)) {
                    $right = [];
                }
                $stack[] = array_values(array_intersect($left, $right));
            } else {
                if ($token == '|') {
                    $left = array_pop($stack);
                    $right = array_pop($stack);
                    if (is_string($left)) {
                        $left = $this->getAllDocumentsForKeyword($this->stemmer->stem($left), true)->pluck('doc_id');
                    }
                    if (is_string($right)) {
                        $right = $this->getAllDocumentsForKeyword($this->stemmer->stem($right), true)->pluck('doc_id');
                    }
                    if (is_null($left)) {
                        $left = [];
                    }
                    if (is_null($right)) {
                        $right = [];
                    }
                    $stack[] = array_unique(array_merge($left, $right));
                } else {
                    if ($token == '~') {
                        $left = array_pop($stack);
                        if (is_string($left)) {
                            $left = $this->getAllDocumentsForWhereKeywordNot($this->stemmer->stem($left), true)->pluck('doc_id');
                        }
                        if (is_null($left)) {
                            $left = [];
                        }
                        $stack[] = $left;
                    } else {
                        $stack[] = $token;
                    }
                }
            }
        }
        if (count($stack)) {
            $docs = new Collection($stack[0]);
        } else {
            $docs = new Collection();
        }
        $counter = 0;
        $docs = $docs->filter(function ($item) use(&$counter, $numOfResults) {
            $counter++;
            if ($counter <= $numOfResults) {
                return $item;
            }
        });
        $stopTimer = microtime(true);
        if ($this->isFileSystemIndex()) {
            return $this->filesystemMapIdsToPaths($docs)->toArray();
        }
        return ['ids' => $docs->toArray(), 'hits' => $docs->count(), 'execution time' => round($stopTimer - $startTimer, 7) * 1000 . " ms"];
    }

Usage Example

Example #1
0
 public function testSearchBoolean()
 {
     $tnt = new TNTSearch();
     $tnt->loadConfig($this->config);
     $indexer = $tnt->createIndex($this->indexName);
     $indexer->disableOutput = true;
     $indexer->query('SELECT id, title, article FROM articles;');
     $indexer->run();
     $tnt->selectIndex($this->indexName);
     $res = $tnt->searchBoolean('romeo juliet queen');
     $this->assertEquals([7], $res['ids']);
     $res = $tnt->searchBoolean('Hamlet or Macbeth');
     $this->assertEquals([3, 4, 1, 2], $res['ids']);
     $this->assertEquals(4, $res['hits']);
     $res = $tnt->searchBoolean('juliet -well');
     $this->assertEquals([5, 6, 7, 8, 10], $res['ids']);
     $res = $tnt->searchBoolean('juliet -romeo');
     $this->assertEquals([10], $res['ids']);
     $res = $tnt->searchBoolean('hamlet -king');
     $this->assertEquals([2], $res['ids']);
     $res = $tnt->searchBoolean('hamlet superman');
     $this->assertEquals([], $res['ids']);
     $res = $tnt->searchBoolean('hamlet or superman');
     $this->assertEquals([1, 2], $res['ids']);
     $res = $tnt->searchBoolean('hamlet');
     $this->assertEquals([1, 2], $res['ids']);
     $res = $tnt->searchBoolean('eldred -bar');
     $this->assertEquals([11], $res['ids']);
     $res = $tnt->searchBoolean('Eldred -bar');
     $this->assertEquals([11], $res['ids']);
 }