Passed
Push — master ( 160c08...0f35aa )
by Gordon
04:48 queued 02:17
created

Searcher::makeQueryOr()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 7
rs 10
1
<?php declare(strict_types = 1);
2
3
/**
4
 * Created by PhpStorm.
5
 * User: gordon
6
 * Date: 25/3/2561
7
 * Time: 1:35 น.
8
 */
9
10
namespace Suilven\ManticoreSearch\Service;
11
12
use Manticoresearch\Search;
13
use SilverStripe\ORM\ArrayList;
14
use SilverStripe\ORM\DataObject;
15
use Suilven\FreeTextSearch\Container\SearchResults;
16
use Suilven\FreeTextSearch\Helper\SearchHelper;
0 ignored issues
show
Bug introduced by
The type Suilven\FreeTextSearch\Helper\SearchHelper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Suilven\FreeTextSearch\Indexes;
18
use Suilven\FreeTextSearch\Types\SearchParamTypes;
0 ignored issues
show
Bug introduced by
The type Suilven\FreeTextSearch\Types\SearchParamTypes was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
20
class Searcher extends \Suilven\FreeTextSearch\Base\Searcher implements \Suilven\FreeTextSearch\Interfaces\Searcher
21
{
22
    /** @var \Suilven\ManticoreSearch\Service\Client */
23
    private $client;
24
25
    public function __construct()
26
    {
27
        $this->client = new Client();
28
    }
29
30
31
    public function search(?string $q): SearchResults
32
    {
33
        $q = \is_null($q)
34
            ? ''
35
            : $q;
36
        if ($this->searchType === SearchParamTypes::OR) {
37
            $q = $this->makeQueryOr($q);
38
        }
39
        $startTime = \microtime(true);
40
        $client = new Client();
41
        $manticoreClient = $client->getConnection();
42
43
        $searcher = new Search($manticoreClient);
44
        $searcher->setIndex($this->indexName);
45
46
        $searcher->limit($this->pageSize);
47
        $offset=$this->pageSize * ($this->page-1);
48
        $searcher->offset($offset);
49
50
        $indexes = new Indexes();
51
        $index = $indexes->getIndex($this->indexName);
52
53
        $searcher->highlight(
54
            [],
55
            ['pre_tags' => '<b>', 'post_tags'=>'</b>']
56
        );
57
58
       // $q = 'sheep';
59
        \error_log('Q: ' . $q);
60
61
        $manticoreResult = $searcher->search($q)->get();
62
        $allFields = $this->getAllFields($index);
63
64
65
        $ssResult = new ArrayList();
66
        while ($manticoreResult->valid()) {
67
            $hit = $manticoreResult->current();
68
            $source = $hit->getData();
69
            $ssDataObject = new DataObject();
70
71
            $this->populateSearchResult($ssDataObject, $allFields, $source);
72
73
            // manticore lowercases fields, so as above normalize them back to the SS fieldnames
74
            $highlights = $hit->getHighlight();
75
            $fieldsToHighlight = $index->getHighlightedFields();
76
            $this->addHighlights($ssDataObject, $allFields, $highlights, $fieldsToHighlight);
77
78
            $ssDataObject->ID = $hit->getId();
79
            $ssResult->push($ssDataObject);
80
            $manticoreResult->next();
81
        }
82
83
        // we now need to standardize the output returned
84
85
        $searchResults = new SearchResults();
86
        $searchResults->setRecords($ssResult);
87
        $searchResults->setPage($this->page);
88
        $searchResults->setPageSize($this->pageSize);
89
        $searchResults->setQuery($q);
90
        $searchResults->setTotalNumberOfResults($manticoreResult->getTotal());
91
92
        $endTime = \microtime(true);
93
        $delta = $endTime - $startTime;
94
        $delta = \round(1000*$delta)/1000;
95
        $searchResults->setTime($delta);
96
97
        return $searchResults;
98
    }
99
100
101
    /** @return array<string> */
102
    public function getAllFields(\Suilven\FreeTextSearch\Index $index): array
103
    {
104
        $allFields = \array_merge(
105
            $index->getFields(),
106
            $index->getTokens(),
107
            //$index->getHasManyFields(),
108
            $index->getHasOneFields(),
109
            $index->getStoredFields()
110
        );
111
112
        $hasManyFields = $index->getHasManyFields();
113
        foreach (\array_keys($hasManyFields) as $key) {
114
            $allFields[] = $key;
115
        }
116
117
        return $allFields;
118
    }
119
120
121
    public function refactorKeyName(string $keyname): string
122
    {
123
        // @todo This is a hack as $Title is rendering the ID in the template
124
        if ($keyname === 'Title') {
125
            $keyname = 'ResultTitle';
126
        } elseif ($keyname === 'link') {
127
            $keyname = 'Link';
128
        };
129
130
        return $keyname;
131
    }
132
133
134
    /** @param array<string> $allFields */
135
    public function matchKey(string $key, array $allFields): string
136
    {
137
        $keyname = $key;
138
        foreach ($allFields as $field) {
139
            if (\strtolower($field) === $key) {
140
                $keyname = $field;
141
142
                break;
143
            }
144
        }
145
146
        return $keyname;
147
    }
148
149
150
    /** @param \SilverStripe\ORM\DataObject $dataObject a dataObject relevant to the index */
151
    public function searchForSimilar(DataObject $dataObject): SearchResults
152
    {
153
        $helper = new SearchHelper();
154
        $indexedTextFields = $helper->getTextFieldPayload($dataObject);
155
        $textForCurrentIndex = $indexedTextFields[$this->indexName];
156
157
        // @todo Search by multiple fields?
158
        $amalgamatedText = '';
159
        foreach (\array_keys($textForCurrentIndex) as $fieldName) {
160
            $amalgamatedText .= $textForCurrentIndex[$fieldName] . ' ';
161
        }
162
        $this->searchType = SearchParamTypes::OR;
0 ignored issues
show
Bug Best Practice introduced by
The property searchType does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
163
164
        return $this->search($amalgamatedText);
165
    }
166
167
168
    /**
169
     * Make a query OR instead of the default AND
170
     *
171
     * @param string $q the search query
172
     * @return string same query for with the terms separated by a | character,to form an OR query
173
     */
174
    private function makeQueryOr(string $q): string
175
    {
176
        $q = \trim($q);
177
        /** @var array<int, string> $splits */
178
        $splits = \preg_split('/\s+/', $q);
179
180
        return \implode('|', $splits);
181
    }
182
183
184
    /**
185
     * @param array<string> $allFields
186
     * @param array<string, string|int|float|bool> $source
187
     */
188
    private function populateSearchResult(DataObject &$ssDataObject, array $allFields, array $source): void
189
    {
190
        $keys = \array_keys($source);
191
        foreach ($keys as $key) {
192
            /** @var string $keyname */
193
            $keyname = $this->matchKey($key, $allFields);
194
            $keyname = $this->refactorKeyName($keyname);
195
196
            /** @phpstan-ignore-next-line */
197
            $ssDataObject->$keyname = $source[$key];
198
        }
199
    }
200
201
202
    /**
203
     * @param array<string> $allFields
204
     * @param array<array<string>> $highlights
205
     * @param array<string> $fieldsToHighlight
206
     */
207
    private function addHighlights(
208
        DataObject &$ssDataObject,
209
        array $allFields,
210
        array $highlights,
211
        array $fieldsToHighlight
212
    ): void {
213
        $highlightsSS = [];
214
        $lowercaseFieldsToHighlight = [];
215
        foreach ($fieldsToHighlight as $fieldname) {
216
            $lowercaseFieldsToHighlight[] = \strtolower($fieldname);
217
        }
218
219
        $keys = \array_keys($highlights);
220
        foreach ($keys as $key) {
221
            if (!isset($highlights[$key]) || !\in_array($key, $lowercaseFieldsToHighlight, true)) {
222
                continue;
223
            }
224
            $keyname = $key;
225
            foreach ($allFields as $field) {
226
                if (\strtolower($field) === $key) {
227
                    $keyname = $field;
228
229
                    continue;
230
                }
231
            }
232
233
            if ($key === 'link') {
234
                $keyname = 'Link';
235
            }
236
237
            $highlightsSS[$keyname] = $highlights[$key];
238
        }
239
240
        /** @phpstan-ignore-next-line */
241
        $ssDataObject->Highlights = $highlightsSS;
242
    }
243
}
244