From 77c406c07fecca6e9bc76b758c1e69458b0b0d1f Mon Sep 17 00:00:00 2001 From: Daniel Klabbers Date: Wed, 10 Nov 2021 16:52:53 +0100 Subject: [PATCH] improve scoring by boosting titles, now also major --- src/Api/Controllers/SearchController.php | 23 +++++++++++++---- src/Elasticsearch/MatchPhraseQuery.php | 28 ++++++++++++++++++++ src/Elasticsearch/MatchQuery.php | 33 ++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 5 deletions(-) create mode 100644 src/Elasticsearch/MatchPhraseQuery.php create mode 100644 src/Elasticsearch/MatchQuery.php diff --git a/src/Api/Controllers/SearchController.php b/src/Api/Controllers/SearchController.php index eaf341a..c5b0434 100644 --- a/src/Api/Controllers/SearchController.php +++ b/src/Api/Controllers/SearchController.php @@ -2,6 +2,8 @@ namespace Blomstra\Search\Api\Controllers; +use Blomstra\Search\Elasticsearch\MatchPhraseQuery; +use Blomstra\Search\Elasticsearch\MatchQuery; use Blomstra\Search\Save\Document as ElasticDocument; use Blomstra\Search\Elasticsearch\TermsQuery; use Elasticsearch\Client; @@ -19,7 +21,7 @@ use Illuminate\Support\Str; use Psr\Http\Message\ServerRequestInterface; use Spatie\ElasticsearchQueryBuilder\Builder; use Spatie\ElasticsearchQueryBuilder\Queries\BoolQuery; -use Spatie\ElasticsearchQueryBuilder\Queries\MatchQuery; +use Spatie\ElasticsearchQueryBuilder\Queries\Query; use Spatie\ElasticsearchQueryBuilder\Queries\TermQuery; use Spatie\ElasticsearchQueryBuilder\Sorts\Sort; use Tobscure\JsonApi\Document; @@ -38,6 +40,7 @@ class SearchController extends ListDiscussionsController protected function data(ServerRequestInterface $request, Document $document) { + // Not used for now. $type = Arr::get($request->getQueryParams(), 'type'); $actor = RequestUtil::getActor($request); @@ -47,10 +50,10 @@ class SearchController extends ListDiscussionsController $include = array_merge($this->extractInclude($request), ['state']); $filterQuery = (BoolQuery::create()) - ->add( - BoolQuery::create() - ->add(MatchQuery::create('content', $filters['q'])) - ); + ->add($this->sentenceMatch($filters)) + ->add($this->wordMatch($filters)) + ; + $builder = (new Builder($this->elastic)) ->index(resolve('blomstra.search.elastic_index')) ->size($this->extractLimit($request)) @@ -181,4 +184,14 @@ class SearchController extends ListDiscussionsController return $query; } + + protected function sentenceMatch(array $filters): Query + { + return new MatchPhraseQuery('content', $filters['q']); + } + + protected function wordMatch(array $filters) + { + return (new MatchQuery('content', $filters['q']))->boost(.3); + } } diff --git a/src/Elasticsearch/MatchPhraseQuery.php b/src/Elasticsearch/MatchPhraseQuery.php new file mode 100644 index 0000000..c82ac3a --- /dev/null +++ b/src/Elasticsearch/MatchPhraseQuery.php @@ -0,0 +1,28 @@ +boost = $boost; + + return $this; + } + + public function toArray(): array + { + $query = parent::toArray()['match']; + + $query[$this->field]['boost'] = $this->boost; + + return [ + 'match_phrase' => $query + ]; + } +} diff --git a/src/Elasticsearch/MatchQuery.php b/src/Elasticsearch/MatchQuery.php new file mode 100644 index 0000000..b20ff0b --- /dev/null +++ b/src/Elasticsearch/MatchQuery.php @@ -0,0 +1,33 @@ +operator = 'and'; + + return $this; + } + + public function boost(float $boost = 1) + { + $this->boost = $boost; + + return $this; + } + + public function toArray(): array + { + $query = parent::toArray(); + + $query['match'][$this->field]['operator'] = $this->operator; + $query['match'][$this->field]['boost'] = $this->boost; + + return $query; + } +}