diff --git a/src/SWP/Bundle/ElasticSearchBundle/Repository/ArticleRepository.php b/src/SWP/Bundle/ElasticSearchBundle/Repository/ArticleRepository.php index cf7dbf397..8617fce75 100644 --- a/src/SWP/Bundle/ElasticSearchBundle/Repository/ArticleRepository.php +++ b/src/SWP/Bundle/ElasticSearchBundle/Repository/ArticleRepository.php @@ -37,29 +37,19 @@ public function findByCriteria(Criteria $criteria, array $extraFields = [], bool $fields = $criteria->getFilters()->getFields(); $boolFilter = new BoolQuery(); - if (null !== $criteria->getTerm() && '' !== $criteria->getTerm()) { - $searchBy = ['title', 'lead', 'keywords.name']; + $term = $criteria->getTerm(); + if (null !== $term && '' !== $term) { + $searchBy = ['title^10', 'lead^4', 'body^2', 'keywords.name']; foreach ($extraFields as $extraField) { $searchBy[] = 'extra.'.$extraField; } if ($searchByBody) { - $searchBy[] = 'body'; + array_splice($searchBy, 2, 0, ['body']); } - $priority = 1; - foreach (array_reverse($searchBy) as $key => $field) { - $searchBy[$key] = $field.'^'.$priority; - ++$priority; - } - $query = new MultiMatch(); - $query->setQuery($criteria->getTerm()); - $query->setFields($searchBy); - $query->setType(MultiMatch::TYPE_PHRASE); - $boolFilter->addMust($query); - - /*$boolQuery = new BoolQuery(); + $boolQuery = new BoolQuery(); $phraseMultiMatchQuery = new MultiMatch(); $phraseMultiMatchQuery->setQuery($term); @@ -109,15 +99,14 @@ public function findByCriteria(Criteria $criteria, array $extraFields = [], bool $nested->setPath('authors'); $functionScore = new Query\FunctionScore(); $functionScore->addWeightFunction(15, new Query\Match('authors.name', $term)); - $functionScore->addWeightFunction(5, new Query\Match('authors.biography', $term)); + $functionScore->addWeightFunction(0.5, new Query\Match('authors.biography', $term)); $functionScore->addWeightFunction(15, new Query\MatchPhrase('authors.name', $term)); - $functionScore->addWeightFunction(10, new Query\MatchPhrase('authors.biography', $term)); + $functionScore->addWeightFunction(1, new Query\MatchPhrase('authors.biography', $term)); $functionScore->setQuery($bool); $nested->setQuery($functionScore); $boolQuery->addShould($nested); $boolFilter->addMust($boolQuery); -*/ } else { $boolFilter->addMust(new MatchAll()); } @@ -215,8 +204,8 @@ public function findByCriteria(Criteria $criteria, array $extraFields = [], bool $query = Query::create($functionScore) ->addSort([ - //'_score' => 'desc', - 'publishedAt' => $criteria->getOrder()->getDirection(), + '_score' => 'desc', + $criteria->getOrder()->getField() => $criteria->getOrder()->getDirection(), ]); $query->setSize(SearchResultLoader::MAX_RESULTS);