QueryComponentBoostTrait::setBoostTerms()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Trait QueryComponentBoostTrait|Firesphere\SolrSearch\Traits\QueryComponentBoostTrait Trait to set boosting on fields
4
 * for the {@link \Firesphere\SolrSearch\Factories\QueryComponentFactory}
5
 *
6
 * @package Firesphere\Solr\Search
7
 * @author Simon `Firesphere` Erkelens; Marco `Sheepy` Hermo
8
 * @copyright Copyright (c) 2018 - now() Firesphere & Sheepy
9
 */
10
11
namespace Firesphere\SolrSearch\Traits;
12
13
use Firesphere\SolrSearch\Factories\QueryComponentFactory;
14
use Firesphere\SolrSearch\Queries\BaseQuery;
15
use Minimalcode\Search\Criteria;
16
17
/**
18
 * Trait QueryComponentBoostTrait adds support for boosting to a QueryComponent
19
 *
20
 * @package Firesphere\Solr\Search
21
 */
22
trait QueryComponentBoostTrait
23
{
24
    /**
25
     * BaseQuery that is going to be executed
26
     *
27
     * @var BaseQuery
28
     */
29
    protected $query;
30
    /**
31
     * Terms that are going to be boosted
32
     *
33
     * @var array
34
     */
35
    protected $boostTerms = [];
36
    /**
37
     * Query set that has been executed
38
     *
39
     * @var array
40
     */
41
    protected $queryArray = [];
42
43
    /**
44
     * Get the boosted terms
45
     *
46
     * @return array
47
     */
48 9
    public function getBoostTerms(): array
49
    {
50 9
        return $this->boostTerms;
51
    }
52
53
    /**
54
     * Set the boosted terms manually
55
     *
56
     * @param array $boostTerms
57
     * @return QueryComponentFactory
58
     */
59 9
    public function setBoostTerms(array $boostTerms): self
60
    {
61 9
        $this->boostTerms = $boostTerms;
62
63 9
        return $this;
64
    }
65
66
    /**
67
     * Build the boosted field setup through Criteria
68
     *
69
     * Add the index-time boosting to the query
70
     */
71 9
    protected function buildBoosts(): void
72
    {
73 9
        $boostedFields = $this->query->getBoostedFields();
74 9
        $queries = $this->getQueryArray();
75 9
        foreach ($boostedFields as $field => $boost) {
76 1
            $terms = [];
77 1
            foreach ($queries as $term) {
78 1
                $terms[] = $term;
79
            }
80 1
            if (count($terms)) {
81 1
                $booster = Criteria::where(str_replace('.', '_', $field))
82 1
                    ->in($terms)
83 1
                    ->boost($boost);
84 1
                $this->queryArray[] = $booster->getQuery();
85
            }
86
        }
87 9
    }
88
89
    /**
90
     * Any class using this needs getQueryArray
91
     *
92
     * @return mixed
93
     */
94
    abstract public function getQueryArray();
95
96
    /**
97
     * Set boosting at Query time
98
     *
99
     * @param array $search
100
     * @param string $term
101
     * @param array $boostTerms
102
     * @return array
103
     */
104 1
    protected function buildQueryBoost($search, string $term, array &$boostTerms): array
105
    {
106 1
        foreach ($search['fields'] as $boostField) {
107 1
            $boostField = str_replace('.', '_', $boostField);
108 1
            $criteria = Criteria::where($boostField)
109 1
                ->is($term)
110 1
                ->boost($search['boost']);
111 1
            $boostTerms[] = $criteria->getQuery();
112
        }
113
114 1
        return $boostTerms;
115
    }
116
}
117