-
Notifications
You must be signed in to change notification settings - Fork 17
/
ASolrSearchable.php
394 lines (370 loc) · 12 KB
/
ASolrSearchable.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<?php
/**
* Allows easy indexing and searching for Active Records using Solr.
*
* Usage:
* <pre>
* $model = User::model()->find();
* $behavior = new ASolrSearchable();
* $behavior->attributes = array(
* "name", "skills", "country.name"
* );
* $model->attachBehavior("ASolrSearchable", $behavior);
* $model->index(); // adds the user to the solr index
*
* $model->name = "Test User";
* $model->save(); // document is automatically reindexed
* $model->address = "123 Fake Street";
* $model->save(); // document is not reindexed because we don't care about the address field
*
* $criteria = new ASolrCriteria;
* $criteria->query = "name:'Test User'";
* $users = $model->findAllBySolr($criteria); // all the users with the name "Test User"
*
* $model->delete(); // document is automatically deleted from solr after the model is deleted
* </pre>
* @package packages.solr
* @author Charles Pick / PeoplePerHour.com
*/
class ASolrSearchable extends CActiveRecordBehavior {
/**
* The class name of the solr document to instantiate
* @var string
*/
public $documentClass = "ASolrDocument";
/**
* Whether to automatically index or reindex the document when it changes.
* Defaults to true.
* You can also pass a method that will be evaluated in order to decide if
* the current model should be indexed or not
* eg: autoIndex => function() { return true; },
* while in the definition of attributes in the models behaviors() method
* @var boolean | callable
*/
public $autoIndex = true;
/**
* Whether to be smart about when to reindex documents.
* If this is true, changes will be pushed to solr only if attributes that
* we care about have changed.
* @var boolean
*/
public $smartIndex = true;
/**
* The configuration for the associated ASolrDocument class
* @var array
*/
public $solrDocumentConfig = array();
/**
* The solr document associated with this model instance
* @var ASolrDocument
*/
protected $_solrDocument;
/**
* The solr criteria associated with this model instance
* @var ASolrCriteria
*/
protected $_solrCriteria;
/**
* The attributes that should be indexed in solr
* @var array
*/
protected $_attributes;
/**
* Stores the attributes of the model after it is found.
* Used to determine whether any of the attributes we care about have changed or not
* @var array
*/
protected $_oldAttributes = array();
/**
* Sets the attributes that should be indexed in solr
* @param array $attributes
*/
public function setAttributes($attributes)
{
$a = array();
foreach($attributes as $key => $value) {
if (is_integer($key)) {
$key = $value;
}
$a[$key] = $value;
}
$this->_attributes = $a;
}
/**
* Gets the attributes that should be indexed in solr
* @return array
*/
public function getAttributes()
{
if ($this->_attributes === null) {
$names = $this->getOwner()->attributeNames();
$this->_attributes = array_combine($names,$names);
}
return $this->_attributes;
}
/**
* Gets a list of objects and attributes that
* @return array a multidimensional array of objects and attributes
*/
protected function resolveAttributes() {
$names = array();
foreach($this->getAttributes() as $modelAttribute => $docAttribute) {
if (!strstr($modelAttribute,".")) {
$names[$modelAttribute] = array($this->getOwner(),$modelAttribute);
continue;
}
$reference = $this->getOwner(); /* @var CActiveRecord $reference */
$pointers = explode(".",$modelAttribute);
$lastItem = array_pop($pointers);
foreach($pointers as $pointer) {
$reference = $reference->{$pointer};
}
$names[$modelAttribute] = array($reference, $lastItem);
}
return $names;
}
/**
* Resolves the attribute name to the field name on solr.
* Default implementation replaces "." with "__" (double underscore)
* <pre>
* echo $behavior->resolveAttributeName("name"); // "name"
* echo $behavior->resolveAttributeName("country.name"); // "country__name"
* </pre>
* @param $attributeName
* @return mixed
*/
protected function resolveAttributeName($attributeName) {
$attributes = $this->getAttributes();
$attributeName = $attributes[$attributeName];
return str_replace(".","__",$attributeName);
}
/**
* Resolves the value of an attribute on an owner object.
*
* @param mixed $owner the object or array of objects that the attribute belongs to
* @param string $attribute the name of the attribute to get the value for
* @return mixed the attribute value
*/
protected function resolveAttributeValue($owner, $attribute) {
if (is_array($owner)) {
$value = array();
foreach($owner as $item)
if (is_array($item) && isset($item[$attribute]))
$value[] = $item[$attribute];
else if (is_object($item) && isset($item->{$attribute}))
$value[] = $item->{$attribute};
return $value;
}
return isset($owner->{$attribute}) ? $owner->{$attribute} : null;
}
/**
* Sets the solr document associated with this model instance
* @param ASolrDocument $solrDocument the solr document
*/
public function setSolrDocument($solrDocument)
{
$this->_solrDocument = $solrDocument;
}
/**
* Gets the solr document associated with this model instance.
* @param boolean $refresh whether to refresh the document, defaults to false
* @return ASolrDocument the solr document
*/
public function getSolrDocument($refresh = false)
{
if ($this->_solrDocument === null || $refresh) {
$config = $this->solrDocumentConfig;
$config['class'] = $this->documentClass;
$this->_solrDocument = Yii::createComponent($config);
foreach($this->resolveAttributes() as $attribute => $item) {
list($object, $property) = $item;
$resolvedAttributeName = $this->resolveAttributeName($attribute);
$this->_solrDocument->{$resolvedAttributeName} = $this->resolveAttributeValue($object, $property);
}
}
return $this->_solrDocument;
}
/**
* Adds the solr document to the index
* @return boolean true if the document was indexed successfully
*/
public function index() {
if (!$this->isIndexable())
return true;
$document = $this->getSolrDocument(true);
if (!$document->save()) {
return false;
}
if ($this->smartIndex) {
$this->_oldAttributes = array();
foreach($this->resolveAttributes() as $key => $item) {
list($object, $property) = $item;
$this->_oldAttributes[$key] = $this->resolveAttributeValue($object, $property);
}
}
return true;
}
/**
* Triggered after the attached model is found.
* Stores the current state of attributes we care about to see if they have changed.
* @param CEvent $event the event raised
*/
public function afterFind($event) {
if ($this->smartIndex) {
$this->_oldAttributes = array();
foreach($this->resolveAttributes() as $key => $item) {
list($object, $property) = $item;
$this->_oldAttributes[$key] = $this->resolveAttributeValue($object, $property);
}
}
}
/**
* Deletes the relevant document from the solr index after the model is deleted
* @param CEvent $event the event raised
*/
public function afterDelete($event) {
$this->getSolrDocument()->delete();
}
/**
* Adds the relevant document to the solr index after the model is saved if $this->autoIndex is true.
* For existing records, the document will only be re-indexed if attributes we care about have changed.
* @param CEvent $event the event raised
*/
public function afterSave($event) {
if (!$this->isIndexable() || !$this->getIsModified()) {
return;
}
$this->index();
}
/**
* Finds an active record that matches the given criteria using solr
* @param ASolrCriteria $criteria the solr criteria to use for searching
* @return CActiveRecord|null the found record, or null if nothing was found
*/
public function findBySolr($criteria = null) {
$c = new ASolrCriteria();
$c->mergeWith($this->getSolrCriteria());
if ($criteria !== null) {
$c->mergeWith($criteria);
}
if ($c->getQuery() == "") {
$c->setQuery("*:*");
}
$document = $this->getSolrDocument()->find($c);
if (!is_object($document)) {
return null;
}
return $this->populateFromSolr($document,false);
}
/**
* Finds all active records that matches the given criteria using solr
* @param ASolrCriteria $criteria the solr criteria to use for searching
* @return CActiveRecord[] an array of results
*/
public function findAllBySolr($criteria = null) {
$c = new ASolrCriteria();
$c->mergeWith($this->getSolrCriteria());
if ($criteria !== null) {
$c->mergeWith($criteria);
}
if ($c->getQuery() == "") {
$c->setQuery("*:*");
}
return $this->populateFromSolr($this->getSolrDocument()->findAll($c),true);
}
/**
* Populates active record objects from solr
* @param ASolrDocument|array $document the document(s) to populate the records from
* @param boolean $all whether to populate a list of records instead of just one, defaults to false
* @return CActiveRecord|array the active record(s) populated from solr
*/
public function populateFromSolr($document, $all = false) {
if ($all) {
$results = array();
foreach($document as $doc) {
$results[] = $this->populateFromSolr($doc,false);
}
return $results;
}
$relations = $this->getOwner()->getMetaData()->relations;
$attributes = array();
$relationAttributes = array();
foreach($this->getAttributes() as $modelAttribute => $docAttribute) {
$resolved = $this->resolveAttributeName($modelAttribute);
if (!strstr($modelAttribute,".")) {
$attributes[$modelAttribute] = $document->{$resolved};
continue;
}
$reference = &$relationAttributes;
$pointers = explode(".",$modelAttribute);
$last = array_pop($pointers);
foreach($pointers as $pointer) {
if (!isset($reference[$pointer])) {
$reference[$pointer] = array();
}
$reference =& $reference[$pointer];
}
$reference[$last] = $document->{$resolved};
}
$modelClass = get_class($this->getOwner());
$model = $modelClass::model()->populateRecord($attributes);
if (count($relationAttributes)) {
foreach($relationAttributes as $relationName => $attributes) {
$relationClass = $relations[$relationName]->className;
$model->{$relationName} = $relationClass::model()->populateRecord($attributes);
}
}
return $model;
}
/**
* Determines whether any attributes that we care about on the model have been modified or not.
* @return boolean true if the item has been modified, otherwise false
*/
public function getIsModified() {
if (!$this->smartIndex || count($this->_oldAttributes) == 0) {
return true;
}
foreach($this->resolveAttributes() as $key => $item) {
if (!isset($this->_oldAttributes[$key])) {
return true;
}
list($object, $property) = $item;
if ($this->_oldAttributes[$key] != $this->resolveAttributeValue($object, $property)) {
return true;
}
}
return false;
}
/**
* Resets the scope
* @return ASolrSearchable $this with the scope reset
*/
public function resetScope() {
$this->_solrCriteria = null;
return $this;
}
/**
* Sets the solr criteria associated with this model
* @param ASolrCriteria $solrCriteria the solr criteria
*/
public function setSolrCriteria($solrCriteria) {
$this->_solrCriteria = $solrCriteria;
}
/**
* Gets the solr criteria associated with this model
* @return ASolrCriteria the solr criteria
*/
public function getSolrCriteria() {
if ($this->_solrCriteria === null) {
$this->_solrCriteria = new ASolrCriteria();
}
return $this->_solrCriteria;
}
/**
* Checks whether the current model should be indexed or not.
* @return bool
*/
protected function isIndexable() {
return is_callable($this->autoIndex) ? call_user_func($this->autoIndex) : $this->autoIndex;
}
}