From f12cacbc7c6460a0d0dd0d0a096b35c4930c616b Mon Sep 17 00:00:00 2001 From: Nathan Bolender Date: Thu, 8 Jun 2017 23:20:11 -0400 Subject: [PATCH] Allow search-as-you-type functionality on boolean search --- src/TNTSearch.php | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/TNTSearch.php b/src/TNTSearch.php index 81760dd..f99620e 100644 --- a/src/TNTSearch.php +++ b/src/TNTSearch.php @@ -130,6 +130,9 @@ public function search($phrase, $numOfResults = 100) public function searchBoolean($phrase, $numOfResults = 100) { + $keywords = $this->breakIntoTokens($phrase); + $lastKeyword = end($keywords); + $stack = []; $startTimer = microtime(true); @@ -141,11 +144,13 @@ public function searchBoolean($phrase, $numOfResults = 100) $left = array_pop($stack); $right = array_pop($stack); if (is_string($left)) { - $left = $this->getAllDocumentsForKeyword($this->stemmer->stem($left), true) + $isLastKeyword = $left == $lastKeyword; + $left = $this->getAllDocumentsForKeyword($this->stemmer->stem($left), true, $isLastKeyword) ->pluck('doc_id'); } if (is_string($right)) { - $right = $this->getAllDocumentsForKeyword($this->stemmer->stem($right), true) + $isLastKeyword = $right == $lastKeyword; + $right = $this->getAllDocumentsForKeyword($this->stemmer->stem($right), true, $isLastKeyword) ->pluck('doc_id'); } if (is_null($left)) { @@ -162,11 +167,13 @@ public function searchBoolean($phrase, $numOfResults = 100) $right = array_pop($stack); if (is_string($left)) { - $left = $this->getAllDocumentsForKeyword($this->stemmer->stem($left), true) + $isLastKeyword = $left == $lastKeyword; + $left = $this->getAllDocumentsForKeyword($this->stemmer->stem($left), true, $isLastKeyword) ->pluck('doc_id'); } if (is_string($right)) { - $right = $this->getAllDocumentsForKeyword($this->stemmer->stem($right), true) + $isLastKeyword = $right == $lastKeyword; + $right = $this->getAllDocumentsForKeyword($this->stemmer->stem($right), true, $isLastKeyword) ->pluck('doc_id'); } if (is_null($left)) {