CoreIndex::getFieldsForIndexing()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 2
eloc 12
nc 2
nop 0
dl 0
loc 23
rs 9.8666
c 2
b 1
f 0
1
<?php
2
3
namespace Firesphere\SearchBackend\Indexes;
4
5
use Firesphere\SearchBackend\Queries\CoreQuery;
6
use Firesphere\SearchBackend\Traits\IndexingTraits\CoreIndexTrait;
7
use Firesphere\SolrSearch\Queries\SolrQuery;
0 ignored issues
show
Bug introduced by
The type Firesphere\SolrSearch\Queries\SolrQuery 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...
8
use LogicException;
9
use SilverStripe\Core\Config\Configurable;
10
use SilverStripe\Core\Extensible;
11
use SilverStripe\Dev\Deprecation;
12
13
abstract class CoreIndex
14
{
15
    use Extensible;
16
    use Configurable;
17
    use CoreIndexTrait;
18
19
    /**
20
     * Field types that can be added
21
     * Used in init to call build methods from configuration yml
22
     *
23
     * @array
24
     */
25
    protected static $fieldTypes = [
26
        'FulltextFields',
27
        'SortFields',
28
        'FilterFields',
29
        'BoostedFields',
30
        'CopyFields',
31
        'DefaultField',
32
        'FacetFields',
33
        'StoredFields',
34
    ];
35
36
    /**
37
     * Required to initialise the fields.
38
     * It's loaded in to the non-static properties for backward compatibility with FTS
39
     * Also, it's a tad easier to use this way, loading the other way around would be very
40
     * memory intensive, as updating the config for each item is not efficient
41
     */
42
    public function init()
43
    {
44
        $config = static::config()->get($this->getIndexName());
45
        if (!$config) {
46
            Deprecation::notice('5', 'Please set an index name and use a config yml');
47
        }
48
49
        if (!empty($this->getClasses())) {
50
            if (!$this->usedAllFields) {
51
                Deprecation::notice('5', 'It is advised to use a config YML for most cases');
52
            }
53
54
            return;
55
        }
56
57
        $this->initFromConfig($config);
58
    }
59
60
    /**
61
     * Get the name of this index.
62
     *
63
     * @return string
64
     */
65
    abstract public function getIndexName(): string;
66
67
    /**
68
     * Generate the config from yml if possible
69
     * @param array|null $config
70
     */
71
    protected function initFromConfig($config): void
72
    {
73
        if (!$config || !array_key_exists('Classes', $config)) {
74
            throw new LogicException('No classes or config to index found!');
75
        }
76
77
        $this->setClasses($config['Classes']);
78
79
        // For backward compatibility, copy the config to the protected values
80
        // Saves doubling up further down the line
81
        foreach (self::$fieldTypes as $type) {
82
            if (array_key_exists($type, $config)) {
83
                $method = 'set' . $type;
84
                if (method_exists($this, $method)) {
85
                    $this->$method($config[$type]);
86
                }
87
            }
88
        }
89
    }
90
91
    /**
92
     * Get all fields that are required for indexing in a unique way
93
     *
94
     * @return array
95
     */
96
    public function getFieldsForIndexing(): array
97
    {
98
        $facets = [];
99
        foreach ($this->getFacetFields() as $field) {
100
            $facets[] = $field['Field'];
101
        }
102
        // Return values to make the key reset
103
        // Only return unique values
104
        // And make it all a single array
105
        $fields = array_values(
106
            array_unique(
107
                array_merge(
108
                    $this->getFulltextFields(),
109
                    $this->getSortFields(),
110
                    $facets,
111
                    $this->getFilterFields()
112
                )
113
            )
114
        );
115
116
        $this->extend('updateFieldsForIndexing', $fields);
117
118
        return $fields;
119
    }
120
121
    /**
122
     * Execute a search of the given query against the
123
     * current index.
124
     *
125
     * @param CoreQuery $query
126
     * @return mixed
127
     */
128
    abstract public function doSearch($query);
129
}
130