-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathIndex.php
More file actions
784 lines (663 loc) · 17.1 KB
/
Index.php
File metadata and controls
784 lines (663 loc) · 17.1 KB
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
<?php
namespace FKRediSearch;
use FKRediSearch\Fields\FieldInterface;
use FKRediSearch\Fields\GeoField;
use FKRediSearch\Fields\NumericField;
use FKRediSearch\Fields\TextField;
use FKRediSearch\Fields\TagField;
class Index {
/**
* @var object
*/
public $client;
/**
* @var string
*/
private $indexName;
/**
* @var bool
*/
private $noOffsetsEnabled = FALSE;
/**
* @var bool
*/
private $noFieldsEnabled = FALSE;
/**
* @var array|int
*/
private $stopWords = NULL;
/**
* @var string
*/
private $on = 'hash';
/**
* @var array|string
*/
private $prefix = '*';
/**
* @var string
*/
private $language = 'english';
/**
* @var string
*/
private $langField = NULL;
/**
* @var float
*/
private $score = 1.0;
/**
* @var string
*/
private $scoreField = NULL;
/**
* @var string
*/
private $payloadField = NULL;
/**
* @var int
*/
private $maxFields = NULL;
/**
* @var bool
*/
private $temporary = FALSE;
/**
* @var int
*/
private $expires = 1;
/**
* @var bool
*/
private $noHighlight = FALSE;
/**
* @var bool
*/
private $nofreqs = FALSE;
/**
* @var bool
*/
private $skipInScan = FALSE;
/**
* Index constructor.
*
* @param RedisRaw\PredisAdapter $client The redis client instance
*/
public function __construct( RedisRaw\PredisAdapter $client ) {
$this->client = $client;
}
/**
* Drop existing index.
*
* @param bool $deleteHash
*
* @return bool
*/
public function drop( bool $deleteHash = FALSE ) {
$dropOptions = array(
$this->getIndexName()
);
if ( $deleteHash ) {
$dropOptions[] = 'DD';
}
return $this->client->rawCommand( 'FT.DROPINDEX', $dropOptions );
}
/**
* Create index with passed fields and settings
*
* @return mixed
*/
public function create() {
$properties = array( $this->getIndexName() );
$properties = array_merge( $properties, array('ON', $this->on) );
if ( is_array( $this->getPrefix() ) ) {
$prefixCount = count( $this->getPrefix() );
$properties = array_merge( $properties, array_merge( array('PREFIX', $prefixCount), $this->getPrefix()) );
} else {
$properties = array_merge( $properties, array('PREFIX', 1, $this->getPrefix()) );
}
$properties = array_merge( $properties, array('LANGUAGE', $this->getDefaultLang()) );
if ( $this->hasLangField() ) {
$properties = array_merge( $properties, array('LANGUAGE_FIELD', $this->getLangField()) );
}
$properties = array_merge( $properties, array('SCORE', $this->getScore()) );
if ( $this->isScoreFieldSet() ) {
$properties = array_merge( $properties, array( 'SCORE_FIELD', $this->getScoreField()) );
}
if ( $this->isPayloadFieldSet() ) {
$properties = array_merge( $properties, array( 'PAYLOAD_FIELD', $this->getPayloadField()) );
}
if ( $this->isMaxFieldsSet() ) {
$properties = array_merge( $properties, array( 'MAXTEXTFIELDS', $this->getMaxFields()) );
}
if ( $this->isNoOffsetsEnabled() ) {
$properties[] = 'NOOFFSETS';
}
if ( $this->isTemporary() ) {
$properties = array_merge( $properties, array( 'TEMPORARY', $this->getExpirationTime()) );
}
if ( $this->isNoHighlight() ) {
$properties[] = 'NOHL';
}
if ( $this->isNoFieldsEnabled() ) {
$properties[] = 'NOFIELDS';
}
if ( $this->isNoFreqsSet() ) {
$properties[] = 'NOFREQS';
}
if ( $this->isSkipInitialScanSet() ) {
$properties[] = 'SKIPINITIALSCAN';
}
if ( ! is_null( $this->stopWords ) ) {
$properties[] = 'STOPWORDS';
$properties[] = count( $this->stopWords );
$properties = array_merge( $properties, $this->stopWords );
}
if ( $this->stopWords == 0 ) {
$properties[] = 'STOPWORDS';
$properties[] = 0;
}
$properties[] = 'SCHEMA';
$fieldDefinitions = [];
foreach ( get_object_vars( $this ) as $field ) {
if ( $field instanceof FieldInterface ) {
$fieldDefinitions = array_merge( $fieldDefinitions, $field->getDefinition() );
}
}
if ( count( $fieldDefinitions ) === 0 ) {
return $this;
}
return $this->client->rawCommand( 'FT.CREATE', array_merge( $properties, $fieldDefinitions ) );
}
/**
* The Redis structure which the index will be created based on
* Currently, only HASH supported, but other types will be supported in the future
*
* @param string $on
*
* @return object Index
*/
public function on( string $on = 'HASH' ) {
$this->on = $on;
return $this;
}
/**
* Tells the index which keys it should index.
* You can add several prefixes to index.
* Since the argument is optional, the default is * (all keys)
*
* @param array|string $prefix
*
* @return object Index
*/
public function setPrefix( $prefix = '*' ) {
$this->prefix = $prefix;
return $this;
}
/**
* @return array|string
*/
public function getPrefix() {
return $this->prefix;
}
/**
* If set, term offsets won't be stores for documents
* This saves memory, but does not allow exact searches or highlighting.
* Note: Implies NOHL .
*
* @param bool $noOffsetsEnabled
*
* @return object Index
*/
public function setNoOffsetsEnabled( bool $noOffsetsEnabled ) {
$this->noOffsetsEnabled = $noOffsetsEnabled;
return $this;
}
/**
* @return bool
*/
public function isNoOffsetsEnabled() {
return $this->noOffsetsEnabled;
}
/**
* @return bool
*/
public function isNoFieldsEnabled() {
return $this->noFieldsEnabled;
}
/**
* @param bool $noFieldsEnabled
*
* @return object Index
*/
public function setNoFieldsEnabled( bool $noFieldsEnabled ) {
$this->noFieldsEnabled = $noFieldsEnabled;
return $this;
}
/**
* @param string $indexName
*
* @return string
*/
public function setIndexName( string $indexName ) {
$this->indexName = $indexName;
return $this;
}
/**
* @return string
*/
public function getIndexName() {
return ! is_string( $this->indexName ) || $this->indexName === '' ? self::class : $this->indexName;
}
/**
* @param array $stopWords Array of custom stop words
*
* @return object Index
*/
public function setStopWords( array $stopWords = NULL ) {
$this->stopWords = $stopWords;
return $this;
}
/**
* @return object Index
*/
public function noStopWords() {
$this->stopWords = 0;
return $this;
}
/**
* If set indicates the default language for documents in the index.
* Default to English.
* Note: A stemmer is used for the supplied language during indexing. If an unsupported language is sent, the command returns an error. The supported languages are:
* "arabic", "danish", "dutch", "english", "finnish", "french", "german", "hungarian", "italian", "norwegian", "portuguese", "romanian", "russian", "spanish", "swedish", "tamil", "turkish" "chinese"
*
* @param string $language
*
* @return object Index
*/
public function setDefaultLang( string $language ) {
$this->language = $language;
return $this;
}
public function getDefaultLang() {
return $this->language;
}
/**
* If set indicates the document field that should be used as the document language.
*
* @param string $field
*
* @return object Index
*/
public function setLangField( string $field ) {
$this->langField = $field;
return $this;
}
/**
* @return bool
*/
public function hasLangField() {
return isset( $this->langField );
}
/**
* @return bool
*/
public function getLangField() {
return isset( $this->langField );
}
/**
* If set indicates the default score for documents in the index.
* Default score is 1.0.
*
* @param float $score
*
* @return object Index
*/
public function setScore( float $score ) {
$this->score = $score;
return $this;
}
/**
* @return float
*/
public function getScore() {
return $this->score;
}
/**
* If set indicates the document field that should be used as the document's rank based on the user's ranking.
* Ranking must be between 0.0 and 1.0. If not set the default score is 1.
*
* @param string $scoreField
*
* @return object Index
*/
public function setScoreField( string $scoreField ) {
$this->scoreField = $scoreField;
return $this;
}
/**
* @return bool
*/
public function isScoreFieldSet() {
return isset( $this->scoreField );
}
/**
* @return string|null
*/
public function getScoreField() {
return $this->scoreField;
}
/**
* If set indicates the document field that should be used as a binary safe payload string to the document,
* that can be evaluated at query time by a custom scoring function, or retrieved to the client.
*
* @param string $payloadField
*
* @return object Index
*/
public function setPayloadField( string $payloadField ) {
$this->payloadField = $payloadField;
return $this;
}
/**
* @return bool
*/
public function isPayloadFieldSet() {
return isset( $this->payloadField );
}
/**
* @return string|null
*/
public function getPayloadField() {
return $this->payloadField;
}
/**
* For efficiency, RediSearch encodes indexes differently if they are created with less than 32 text fields.
* This option forces RediSearch to encode indexes as if there were more than 32 text fields,
* which allows you to add additional fields (beyond 32) using FT.ALTER .
*
* @param int $maxFields
*
* @return object Index
*/
public function setMaxFields( int $maxFields ) {
$this->maxFields = $maxFields;
return $this;
}
/**
* @return bool
*/
public function isMaxFieldsSet() {
return isset( $this->maxFields );
}
public function getMaxFields() {
return $this->maxFields;
}
/**
* Marks index as temporary and sets expiration time in seconds
* @param int $expires Index expiration time in seconds
*
* @return object Index
*/
public function setTemporary( int $expires ) {
$this->temporary = TRUE;
$this->expires = $expires;
return $this;
}
/**
* @return bool
*/
public function isTemporary() {
return $this->temporary;
}
/**
* @return int
*/
public function getExpirationTime() {
return $this->expires;
}
/**
* Conserves storage space and memory by disabling highlighting support.
* If set, we do not store corresponding byte offsets for term positions.
* Note: NOHL is also implied by NOOFFSETS .
*
* @param bool $noHighlight
*
* @return object Index
*/
public function setNoHighlight( bool $noHighlight ) {
$this->noHighlight = $noHighlight;
return $this;
}
/**
* @return bool
*/
public function isNoHighlight() {
return $this->noHighlight;
}
/**
* If set, we avoid saving the term frequencies in the index.
* This saves memory but does not allow sorting based on the frequencies of a given term within the document.
*
* @param bool $noFreqs
*
* @return object Index
*/
public function setNoFreqs( bool $noFreqs ) {
$this->nofreqs = $noFreqs;
return $this;
}
/**
* @return bool
*/
public function isNoFreqsSet() {
return $this->nofreqs;
}
/**
* If set, we do not scan and index.
*
* @param bool $noInitialScan
*
* @return object Index
*/
public function skipInitialScan( bool $noInitialScan ) {
$this->skipInScan = $noInitialScan;
return $this;
}
/**
* @return bool
*/
public function isSkipInitialScanSet() {
return $this->skipInScan;
}
/**
* Get field from the index info.
* This is useful when we want to retrieve the score field from an existing index.
*
* @param string $field The field key you want to get
*
* @return false|mixed
*/
public function getFieldFromInfo( string $field ) {
$indexInfo = $this->getInfo();
if ( empty( $indexInfo) || !is_array( $indexInfo ) ) {
return FALSE;
}
return $indexInfo['index_definition'][ $field ];
}
/**
* @param array $synonymList
*
* @return void
*/
public function synonymAdd( array $synonymList = array() ) {
if ( empty( $synonymList ) || ! is_array( $synonymList ) ) {
return;
}
foreach ( $synonymList as $key => $synonym ) {
$synonymGroup = array_map( 'trim', $synonym );
$synonymCommand = array_merge( array( $this->getIndexName() ), array( "synonymGroup:$key" ), $synonymGroup );
$this->client->rawCommand( 'FT.SYNUPDATE', $synonymCommand );
}
}
/**
* @param
*
* @return void
*/
public function synonymDump() {
$this->client->rawCommand( 'FT.SYNDUMP', array( $this->getIndexName() ) );
}
/**
* @return array
*/
protected function getFields() {
$fields = [];
foreach ( get_object_vars( $this ) as $field ) {
if ( $field instanceof FieldInterface ) {
$fields[ $field->getName() ] = $field;
}
}
return $fields;
}
/**
* Add documents to the index.
*
* @param Document $document
*
* @return object $this
* @since 0.1.0
*
*/
public function add( Document $document ) {
$properties = $document->getDefinition();
array_unshift( $properties, $document->getId() );
if ( $document->getScore() !== NULL && $this->getFieldFromInfo('score_field') ) {
$properties = array_merge( $properties, array( $this->getFieldFromInfo('score_field'), $document->getScore() ) );
}
if ( $document->getLanguage() !== NULL && $this->getFieldFromInfo('language_field') ) {
$properties = array_merge( $properties, array( $this->getFieldFromInfo('language_field'), $document->getLanguage() ) );
}
return $this->client->rawCommand( 'HSET', $properties );
}
/**
* @param string $name
* @param float $weight
* @param bool $sortable
* @param bool $noindex
*
* @return object Index
*/
public function addTextField( string $name, float $weight = 1.0, bool $sortable = FALSE, bool $noindex = FALSE ) {
$this->$name = ( new TextField( $name ) )->setSortable( $sortable )->setNoindex( $noindex )->setWeight( $weight );
return $this;
}
/**
* @param string $name
* @param bool $sortable
* @param bool $noindex
*
* @param string $separator
*
* @return object Index
*/
public function addTagField( string $name, bool $sortable = FALSE, bool $noindex = FALSE, string $separator = ',' ) {
$this->$name = ( new TagField( $name ) )->setSortable( $sortable )->setNoindex( $noindex )->setSeparator( $separator );
return $this;
}
/**
* @param string $name
* @param bool $sortable
* @param bool $noindex
*
* @return object Index
*/
public function addNumericField( string $name, bool $sortable = FALSE, bool $noindex = FALSE ) {
$this->$name = ( new NumericField( $name ) )->setSortable( $sortable )->setNoindex( $noindex );
return $this;
}
/**
* @param string $name
*
* @param bool $noindex
*
* @return object Index
*/
public function addGeoField( string $name, bool $noindex = FALSE ) {
$this->$name = ( new GeoField( $name ) )->setNoindex( $noindex );
return $this;
}
/**
* Delete post from index.
*
* @param string|null $id
*
* @return object $this
* @since 0.1.0
*/
public function delete( string $id = NULL ) {
if ( $id === NULL ) {
return NULL;
}
$command = array( $this->indexName, $id );
$this->client->rawCommand( 'DEL', $command );
return $this;
}
/**
* Write entire redisearch index to the disk to persist it.
*
* @return object Index
* @since 0.1.0
*/
public function writeToDisk() {
return $this->client->rawCommand( 'SAVE', [] );
}
/**
* Returns information and statistics on the index.
*
* @param string|null $indexName
*
* @return array
*/
public function getInfo( string $indexName = NULL ) {
$indexInfo = $this->client->rawCommand( 'FT.INFO', array( empty( $indexName ) ? $this->indexName : $indexName ) );
if ( empty( $indexInfo ) || !is_array( $indexInfo ) ) {
return NULL;
}
array_walk_recursive( $indexInfo, function(&$item, $key) {
$item = (string) $item;
} );
return $this->normalizeInfoArray( $indexInfo );
}
/**
* @param array $redisArray
*
* @return array
*/
private function normalizeInfoArray( array $redisArray ) {
$newArray = array();
for ( $i = 0; $i < count( $redisArray ); $i += 2 ) {
$tmp = &$redisArray[$i + 1];
if(isset($tmp)){
if ( $redisArray[$i] === 'fields' ) {
foreach ( $redisArray[ $i + 1 ] as $field ) {
$fieldName = $field[0];
array_shift( $field );
$newArray[ $redisArray[ $i ] ][ $fieldName ] = $this->normalizeInfoArray( $field );
}
} elseif ( $redisArray[$i] === 'prefixes' ) {
foreach ( $redisArray[$i + 1] as $field ) {
$newArray[ $redisArray[$i] ][] = $field;
}
} elseif ( (gettype( $redisArray[$i] ) === 'string' && gettype( $redisArray[$i + 1] ) === 'string' ) ||
( gettype( $redisArray[$i + 1] ) === 'array' && empty( $redisArray[$i + 1] ) )
) {
$newArray[ $redisArray[$i] ] = $redisArray[$i + 1];
} elseif ( gettype( $redisArray[$i + 1] ) === 'array' && !empty( $redisArray[$i + 1] ) ) {
$newArray[ $redisArray[$i] ] = $this->normalizeInfoArray( $redisArray[$i + 1] );
}
}
}
return $newArray;
}
}