Completed
Push — master ( 1c0463...6ee2a7 )
by Rafael
34:09
created

initializeXgramPhraseParameters()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 0
cp 0
rs 8.2222
c 0
b 0
f 0
cc 7
eloc 7
nc 4
nop 0
crap 56
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Search;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2012-2015 Ingo Renner <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 3 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use ApacheSolrForTypo3\Solr\Query;
28
29
/**
30
 * Boosting search component
31
 *
32
 * @author Ingo Renner <[email protected]>
33
 */
34
class RelevanceComponent extends AbstractComponent implements QueryAware
35
{
36
37
    /**
38
     * Solr query
39
     *
40
     * @var Query
41
     */
42
    protected $query;
43
44
    /**
45
     * Initializes the search component.
46
     *
47
     * Sets minimum match, boost function, boost query and tie breaker.
48
     *
49
     */
50 32
    public function initializeSearchComponent()
51
    {
52 32
        if (!empty($this->searchConfiguration['query.']['minimumMatch'])) {
53
            $this->query->setMinimumMatch($this->searchConfiguration['query.']['minimumMatch']);
54
        }
55
56 32
        if (!empty($this->searchConfiguration['query.']['boostFunction'])) {
57
            $this->query->setBoostFunction($this->searchConfiguration['query.']['boostFunction']);
58
        }
59
60 32
        if (!empty($this->searchConfiguration['query.']['boostQuery'])) {
61
            $this->query->setBoostQuery($this->searchConfiguration['query.']['boostQuery']);
62
        }
63
64 32
        if (!empty($this->searchConfiguration['query.']['boostQuery.'])) {
65
            $boostQueries = [];
66
            $boostConfiguration = $this->searchConfiguration['query.']['boostQuery.'];
67
68
            foreach ($boostConfiguration as $query) {
69
                $boostQueries[] = $query;
70
            }
71
72
            $this->query->setBoostQuery($boostQueries);
73
        }
74 32
75
        if (!empty($this->searchConfiguration['query.']['tieParameter'])) {
76
            $this->query->setTieParameter($this->searchConfiguration['query.']['tieParameter']);
77
        }
78
79
        $this->initializePhraseParameters();
80
        $this->initializeXgramPhraseParameters();
81 32
    }
82
83 32
    /**
84 32
     * Provides the extension component with an instance of the current query.
85
     *
86
     * @param Query $query Current query
87
     */
88
    public function setQuery(Query $query)
89
    {
90
        $this->query = $query;
91
    }
92
93
    /**
94
     * Initializes all for phrase search relevant params
95
     *
96
     * Folowing Query Parameters:
97
     *   "ps" Phrase Slop
98
     *   "qs" Query Phrase Slop
99
     *
100
     */
101
    protected function initializePhraseParameters() {
102
        if (empty($this->searchConfiguration['query.']['phrase']) || $this->searchConfiguration['query.']['phrase'] !== 1) {
103
            return;
104
        }
105
106
        if (!empty($this->searchConfiguration['query.']['phrase']['slop'])) {
107
            $this->query->setPhraseSlopParameter($this->searchConfiguration['query.']['phrase']['slop']);
108
        }
109
110
        if (!empty($this->searchConfiguration['query.']['phrase']['querySlop'])) {
111
            $this->query->setQueryPhraseSlopParameter($this->searchConfiguration['query.']['phrase']['querySlop']);
112
        }
113
    }
114
115
    /**
116
     * Initializes all for bigram phrase search relevant params
117
     *
118
     * Folowing Query Parameters:
119
     *   "ps" Phrase Slop
120
     *   "qs" Query Phrase Slop
121
     *
122
     */
123
    protected function initializeXgramPhraseParameters() {
124
        if (!empty($this->searchConfiguration['query.']['bigramPhrase']) && $this->searchConfiguration['query.']['bigramPhrase'] === 1
125
            && !empty($this->searchConfiguration['query.']['bigramPhrase']['slop'])) {
126
            $this->query->setQueryPhraseSlopParameter($this->searchConfiguration['query.']['bigramPhrase']['slop']);
127
        }
128
129
        if (!empty($this->searchConfiguration['query.']['trigramPhrase']) && $this->searchConfiguration['query.']['trigramPhrase'] === 1
130
            && !empty($this->searchConfiguration['query.']['trigramPhrase']['slop'])) {
131
            $this->query->setQueryPhraseSlopParameter($this->searchConfiguration['query.']['trigramPhrase']['slop']);
132
        }
133
    }
134
135
}
136