Completed
Pull Request — master (#6)
by Robbie
03:26 queued 52s
created

CwpSearchIndex   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
dl 0
loc 84
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A uploadConfig() 0 11 2
A search() 0 8 2
A init() 0 5 2
A getFieldDefinitions() 0 8 1
1
<?php
2
3
namespace CWP\Core\Model;
4
5
use CwpSearchBoostExtension;
0 ignored issues
show
Bug introduced by
The type CwpSearchBoostExtension 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...
6
use SilverStripe\CMS\Model\SiteTree;
7
use SilverStripe\FullTextSearch\Search\Queries\SearchQuery;
0 ignored issues
show
Bug introduced by
The type SilverStripe\FullTextSea...rch\Queries\SearchQuery 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 SilverStripe\FullTextSearch\Solr\SolrIndex;
0 ignored issues
show
Bug introduced by
The type SilverStripe\FullTextSearch\Solr\SolrIndex 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...
9
use SilverStripe\FullTextSearch\Solr\Stores\SolrConfigStore;
0 ignored issues
show
Bug introduced by
The type SilverStripe\FullTextSea...\Stores\SolrConfigStore 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...
10
use SilverStripe\SiteConfig\SiteConfig;
11
use SilverStripe\View\ArrayData;
12
13
/**
14
 * Abstract wrapper for all cwp-core features
15
 *
16
 * Can be extended by user indexes to include these features. {@see SolrSearchIndex} for an example
17
 */
18
abstract class CwpSearchIndex extends SolrIndex
19
{
20
21
    /**
22
     * Copy all fields into both search and spellcheck data source
23
     *
24
     * @var array
25
     * @config
26
     */
27
    private static $copy_fields = array(
0 ignored issues
show
introduced by
The private property $copy_fields is not used, and could be removed.
Loading history...
28
        '_text',
29
        '_spellcheckText'
30
    );
31
32
    /**
33
     * Default dictionary to use. This will overwrite the 'spellcheck.dictionary' option for searches given,
34
     * unless set to empty.
35
     *
36
     * '_spellcheck' is a predefined by the cwp infrastructure, which is configured
37
     * to be built from the '_spellcheckText' field. You can't rename this within CWP.
38
     *
39
     * @var string
40
     * @config
41
     */
42
    private static $dictionary = '_spellcheck';
43
44
    public function init()
45
    {
46
        // Add optional boost
47
        if (SiteTree::has_extension(CwpSearchBoostExtension::class)) {
48
            $this->setFieldBoosting(SiteTree::class . '_SearchBoost', SiteTree::config()->get('search_boost'));
49
        }
50
    }
51
52
    /**
53
     * Upload config for this index to the given store
54
     *
55
     * @param SolrConfigStore $store
56
     */
57
    public function uploadConfig($store)
58
    {
59
        parent::uploadConfig($store);
60
61
        // Upload configured synonyms {@see SynonymsSiteConfig}
62
        $siteConfig = SiteConfig::current_site_config();
63
        if ($siteConfig->SearchSynonyms) {
64
            $store->uploadString(
65
                $this->getIndexName(),
66
                'synonyms.txt',
67
                $siteConfig->SearchSynonyms
68
            );
69
        }
70
    }
71
72
    /**
73
     *
74
     * @return string
75
     */
76
    public function getFieldDefinitions()
77
    {
78
        $xml = parent::getFieldDefinitions();
79
        $xml .= "\n\n\t\t<!-- Additional custom fields for spell checking -->";
80
        $xml .= "\n\t\t<field name='_spellcheckText' type='textSpellHtml' indexed='true' "
81
            . "stored='false' multiValued='true' />";
82
83
        return $xml;
84
    }
85
86
    /**
87
     *
88
     * @param SearchQuery $query
89
     * @param int $offset
90
     * @param int $limit
91
     * @param array $params
92
     * @return ArrayData
93
     */
94
    public function search(SearchQuery $query, $offset = -1, $limit = -1, $params = [])
95
    {
96
        // Override dictionary if given
97
        if ($dictionary = $this->config()->dictionary) {
98
            $params["spellcheck.dictionary"] = $dictionary;
99
        }
100
101
        return parent::search($query, $offset, $limit, $params);
102
    }
103
}
104