|
| 1 | +<?php |
| 2 | +namespace Flowpack\ElasticSearch\ContentRepositoryAdaptor\Driver\Version1\Query; |
| 3 | + |
| 4 | +/* |
| 5 | + * This file is part of the Flowpack.ElasticSearch.ContentRepositoryAdaptor package. |
| 6 | + * |
| 7 | + * (c) Contributors of the Neos Project - www.neos.io |
| 8 | + * |
| 9 | + * This package is Open Source Software. For the full copyright and license |
| 10 | + * information, please view the LICENSE file which was distributed with this |
| 11 | + * source code. |
| 12 | + */ |
| 13 | + |
| 14 | +use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception; |
| 15 | +use TYPO3\Flow\Utility\Arrays; |
| 16 | + |
| 17 | +class FunctionScoreQuery extends FilteredQuery |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var array |
| 21 | + */ |
| 22 | + protected $functionScoreRequest = [ |
| 23 | + 'functions' => [] |
| 24 | + ]; |
| 25 | + |
| 26 | + /** |
| 27 | + * @param array $functions |
| 28 | + * @return void |
| 29 | + */ |
| 30 | + public function functions(array $functions) |
| 31 | + { |
| 32 | + if (isset($functions['functions'])) { |
| 33 | + $this->functionScoreRequest = $functions; |
| 34 | + } else { |
| 35 | + $this->functionScoreRequest['functions'] = $functions; |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @param string $scoreMode |
| 41 | + * @return void |
| 42 | + * @throws Exception\QueryBuildingException |
| 43 | + */ |
| 44 | + public function scoreMode($scoreMode) |
| 45 | + { |
| 46 | + if (!in_array($scoreMode, ['multiply', 'first', 'sum', 'avg', 'max', 'min'])) { |
| 47 | + throw new Exception\QueryBuildingException('Invalid score mode', 1454016230); |
| 48 | + } |
| 49 | + $this->functionScoreRequest['score_mode'] = $scoreMode; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @param string $boostMode |
| 54 | + * @return void |
| 55 | + * @throws Exception\QueryBuildingException |
| 56 | + */ |
| 57 | + public function boostMode($boostMode) |
| 58 | + { |
| 59 | + if (!in_array($boostMode, ['multiply', 'replace', 'sum', 'avg', 'max', 'min'])) { |
| 60 | + throw new Exception\QueryBuildingException('Invalid boost mode', 1454016229); |
| 61 | + } |
| 62 | + $this->functionScoreRequest['boost_mode'] = $boostMode; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @param integer|float $boost |
| 67 | + * @return void |
| 68 | + * @throws Exception\QueryBuildingException |
| 69 | + */ |
| 70 | + public function maxBoost($boost) |
| 71 | + { |
| 72 | + if (!is_numeric($boost)) { |
| 73 | + throw new Exception\QueryBuildingException('Invalid max boost', 1454016230); |
| 74 | + } |
| 75 | + $this->functionScoreRequest['max_boost'] = $boost; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @param integer|float $score |
| 80 | + * @return void |
| 81 | + * @throws Exception\QueryBuildingException |
| 82 | + */ |
| 83 | + public function minScore($score) |
| 84 | + { |
| 85 | + if (!is_numeric($score)) { |
| 86 | + throw new Exception\QueryBuildingException('Invalid max boost', 1454016230); |
| 87 | + } |
| 88 | + $this->functionScoreRequest['min_score'] = $score; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * {@inheritdoc} |
| 93 | + */ |
| 94 | + protected function prepareRequest() |
| 95 | + { |
| 96 | + if ($this->functionScoreRequest['functions'] === []) { |
| 97 | + return parent::prepareRequest(); |
| 98 | + } |
| 99 | + $currentQuery = $this->request['query']; |
| 100 | + |
| 101 | + $baseQuery = $this->request; |
| 102 | + unset($baseQuery['query']); |
| 103 | + |
| 104 | + $functionScore = $this->functionScoreRequest; |
| 105 | + $functionScore['query'] = $currentQuery; |
| 106 | + $query = Arrays::arrayMergeRecursiveOverrule($baseQuery, [ |
| 107 | + 'query' => [ |
| 108 | + 'function_score' => $functionScore |
| 109 | + ] |
| 110 | + ]); |
| 111 | + |
| 112 | + return $query; |
| 113 | + } |
| 114 | +} |
0 commit comments