diff --git a/src/Api/Controllers/SearchController.php b/src/Api/Controllers/SearchController.php index 57b03b8..9caad2b 100644 --- a/src/Api/Controllers/SearchController.php +++ b/src/Api/Controllers/SearchController.php @@ -4,6 +4,7 @@ namespace Blomstra\Search\Api\Controllers; use Blomstra\Search\Elasticsearch\MatchPhraseQuery; use Blomstra\Search\Elasticsearch\MatchQuery; +use Blomstra\Search\Elasticsearch\WildcardQuery; use Blomstra\Search\Save\Document as ElasticDocument; use Blomstra\Search\Elasticsearch\TermsQuery; use Elasticsearch\Client; @@ -204,7 +205,10 @@ class SearchController extends ListDiscussionsController protected function partialMatch(string $q) { - return (new MatchQuery('content', "*$q*"))->boost(.2); + return (new WildcardQuery('content', "*$q*")) + ->caseSensitivity(false) + ->rewrite('constant_score_boolean') + ->boost(.2); } protected function getGroups(User $actor): Collection diff --git a/src/Elasticsearch/WildcardQuery.php b/src/Elasticsearch/WildcardQuery.php new file mode 100644 index 0000000..0b5fb44 --- /dev/null +++ b/src/Elasticsearch/WildcardQuery.php @@ -0,0 +1,46 @@ +boost = $boost; + + return $this; + } + + public function caseSensitivity(bool $sensitivity = true) + { + $this->sensitivity = $sensitivity; + + return $this; + } + + public function rewrite(string $rewrite = null) + { + $this->rewrite = $rewrite; + + return $this; + } + + public function toArray(): array + { + $query = parent::toArray(); + + $query['wildcard'][$this->field]['boost'] = $this->boost; + $query['wildcard'][$this->field]['case_insensitive'] = ! $this->sensitivity; + + if ($this->rewrite) { + $query['wildcard'][$this->field]['rewrite'] = $this->rewrite; + + } + + return $query; + } +}