|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\FullTextSearch\Solr; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Control\Director; |
|
6
|
|
|
use SilverStripe\Core\Injector\Injector; |
|
7
|
|
|
use SilverStripe\Core\Manifest\Module; |
|
8
|
|
|
use SilverStripe\Core\Manifest\ModuleLoader; |
|
9
|
|
|
use SilverStripe\FullTextSearch\Search\FullTextSearch; |
|
10
|
|
|
use SilverStripe\FullTextSearch\Solr\Services\Solr4Service; |
|
11
|
|
|
use SilverStripe\FullTextSearch\Solr\Services\Solr3Service; |
|
12
|
|
|
|
|
13
|
|
|
class Solr |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Configuration on where to find the solr server and how to get new index configurations into it. |
|
17
|
|
|
* |
|
18
|
|
|
* Required fields: |
|
19
|
|
|
* host (default: localhost) - The host or IP Solr is listening on |
|
20
|
|
|
* port (default: 8983) - The port Solr is listening on |
|
21
|
|
|
* path (default: /solr) - The suburl the solr service is available on |
|
22
|
|
|
* |
|
23
|
|
|
* Optional fields: |
|
24
|
|
|
* version (default: 4) - The Solr server version. Currently supports 3 and 4 (you can add a sub-version like 4.5 if |
|
25
|
|
|
* you like, but currently it has no effect) |
|
26
|
|
|
* service (default: depends on version, Solr3Service for 3, Solr4Service for 4) |
|
27
|
|
|
* the class that provides actual communcation to the Solr server |
|
28
|
|
|
* extraspath (default: <basefolder>/fulltextsearch/conf/solr/{version}/extras/) - Absolute path to |
|
29
|
|
|
* the folder containing templates which are used for generating the schema and field definitions. |
|
30
|
|
|
* templates (default: <basefolder>/fulltextsearch/conf/solr/{version}/templates/) - Absolute path to |
|
31
|
|
|
* the configuration default files, e.g. solrconfig.xml. |
|
32
|
|
|
* |
|
33
|
|
|
* indexstore => an array with |
|
34
|
|
|
* |
|
35
|
|
|
* mode - a classname which implements SolrConfigStore, or 'file' or 'webdav' |
|
36
|
|
|
* |
|
37
|
|
|
* When mode == SolrConfigStore_File or file (indexes should be written on a local filesystem) |
|
38
|
|
|
* path - The (locally accessible) path to write the index configurations to. |
|
39
|
|
|
* remotepath (default: the same as indexpath) - The path that the Solr server will read the index configurations from |
|
40
|
|
|
* |
|
41
|
|
|
* When mode == SolrConfigStore_WebDAV or webdav (indexes should stored on a remote Solr server via webdav) |
|
42
|
|
|
* auth (default: none) - A username:password pair string to use to auth against the webdav server |
|
43
|
|
|
* path (default: /solrindex) - The suburl on the solr host that is set up to accept index configurations via webdav |
|
44
|
|
|
* port (default: none) - The port for WebDAV if different from the Solr port |
|
45
|
|
|
* remotepath - The path that the Solr server will read the index configurations from |
|
46
|
|
|
*/ |
|
47
|
|
|
protected static $solr_options = array(); |
|
48
|
|
|
|
|
49
|
|
|
/** A cache of solr_options with the defaults all merged in */ |
|
50
|
|
|
protected static $merged_solr_options = null; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Update the configuration for Solr. See $solr_options for a discussion of the accepted array keys |
|
54
|
|
|
* @param array $options - The options to update |
|
55
|
|
|
*/ |
|
56
|
|
|
public static function configure_server($options = array()) |
|
57
|
|
|
{ |
|
58
|
|
|
self::$solr_options = array_merge(self::$solr_options, $options); |
|
59
|
|
|
self::$merged_solr_options = null; |
|
60
|
|
|
|
|
61
|
|
|
self::$service_singleton = null; |
|
62
|
|
|
self::$service_core_singletons = array(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Get the configured Solr options with the defaults all merged in |
|
67
|
|
|
* @return array - The merged options |
|
68
|
|
|
*/ |
|
69
|
|
|
public static function solr_options() |
|
70
|
|
|
{ |
|
71
|
|
|
if (self::$merged_solr_options) { |
|
72
|
|
|
return self::$merged_solr_options; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$defaults = array( |
|
76
|
|
|
'host' => 'localhost', |
|
77
|
|
|
'port' => 8983, |
|
78
|
|
|
'path' => '/solr', |
|
79
|
|
|
'version' => '4' |
|
80
|
|
|
); |
|
81
|
|
|
|
|
82
|
|
|
// Build some by-version defaults |
|
83
|
|
|
$version = isset(self::$solr_options['version']) ? self::$solr_options['version'] : $defaults['version']; |
|
84
|
|
|
|
|
85
|
|
|
/** @var Module $module */ |
|
86
|
|
|
$module = ModuleLoader::getModule('silverstripe/fulltextsearch'); |
|
87
|
|
|
$modulePath = $module->getPath(); |
|
88
|
|
|
|
|
89
|
|
|
if (version_compare($version, '4', '>=')) { |
|
90
|
|
|
$versionDefaults = [ |
|
91
|
|
|
'service' => Solr4Service::class, |
|
92
|
|
|
'extraspath' => $modulePath . '/conf/solr/4/extras/', |
|
93
|
|
|
'templatespath' => $modulePath . '/conf/solr/4/templates/', |
|
94
|
|
|
]; |
|
95
|
|
|
} else { |
|
96
|
|
|
$versionDefaults = [ |
|
97
|
|
|
'service' => Solr3Service::class, |
|
98
|
|
|
'extraspath' => $modulePath . '/conf/solr/3/extras/', |
|
99
|
|
|
'templatespath' => $modulePath . '/conf/solr/3/templates/', |
|
100
|
|
|
]; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return (self::$merged_solr_options = array_merge($defaults, $versionDefaults, self::$solr_options)); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
public static function set_service_class($class) |
|
108
|
|
|
{ |
|
109
|
|
|
user_error('set_service_class is deprecated - pass as part of $options to configure_server', E_USER_WARNING); |
|
110
|
|
|
self::configure_server(array('service' => $class)); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** @var SolrService | null - The instance of SolrService for core management */ |
|
114
|
|
|
protected static $service_singleton = null; |
|
115
|
|
|
/** @var [SolrService_Core] - The instances of SolrService_Core for each core */ |
|
116
|
|
|
protected static $service_core_singletons = array(); |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Get a SolrService |
|
120
|
|
|
* |
|
121
|
|
|
* @param string $core Optional name of index class |
|
122
|
|
|
* @return SolrService_Core |
|
123
|
|
|
*/ |
|
124
|
|
|
public static function service($core = null) |
|
125
|
|
|
{ |
|
126
|
|
|
$options = self::solr_options(); |
|
127
|
|
|
|
|
128
|
|
|
if (!self::$service_singleton) { |
|
129
|
|
|
self::$service_singleton = Injector::inst()->create( |
|
130
|
|
|
$options['service'], |
|
131
|
|
|
$options['host'], |
|
132
|
|
|
$options['port'], |
|
133
|
|
|
$options['path'] |
|
134
|
|
|
); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
if ($core) { |
|
|
|
|
|
|
138
|
|
|
if (!isset(self::$service_core_singletons[$core])) { |
|
139
|
|
|
self::$service_core_singletons[$core] = self::$service_singleton->serviceForCore( |
|
140
|
|
|
singleton($core)->getIndexName() |
|
141
|
|
|
); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
return self::$service_core_singletons[$core]; |
|
145
|
|
|
} else { |
|
146
|
|
|
return self::$service_singleton; |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
public static function get_indexes() |
|
151
|
|
|
{ |
|
152
|
|
|
return FullTextSearch::get_indexes(SolrIndex::class); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: