77use Ehann \RediSearch \Document \AbstractDocumentFactory ;
88use Ehann \RediSearch \Document \DocumentInterface ;
99use Ehann \RediSearch \Exceptions \NoFieldsInIndexException ;
10+ use Ehann \RediSearch \Exceptions \UnknownIndexNameException ;
1011use Ehann \RediSearch \Fields \FieldInterface ;
1112use Ehann \RediSearch \Fields \GeoField ;
1213use Ehann \RediSearch \Fields \NumericField ;
1516use Ehann \RediSearch \Query \Builder as QueryBuilder ;
1617use Ehann \RediSearch \Query \BuilderInterface as QueryBuilderInterface ;
1718use Ehann \RediSearch \Query \SearchResult ;
19+ use Ehann \RedisRaw \Exceptions \RawCommandErrorException ;
20+ use RedisException ;
1821
1922class Index extends AbstractIndex implements IndexInterface
2023{
@@ -65,10 +68,23 @@ public function create()
6568 return $ this ->rawCommand ('FT.CREATE ' , array_merge ($ properties , $ fieldDefinitions ));
6669 }
6770
71+ /**
72+ * @return bool
73+ */
74+ public function exists (): bool
75+ {
76+ try {
77+ $ this ->info ();
78+ return true ;
79+ } catch (UnknownIndexNameException $ exception ) {
80+ return false ;
81+ }
82+ }
83+
6884 /**
6985 * @return array
7086 */
71- protected function getFields (): array
87+ public function getFields (): array
7288 {
7389 $ fields = [];
7490 foreach (get_object_vars ($ this ) as $ field ) {
@@ -127,6 +143,15 @@ public function addTagField(string $name, bool $sortable = false, bool $noindex
127143 return $ this ;
128144 }
129145
146+ /**
147+ * @param string $name
148+ * @return array
149+ */
150+ public function tagValues (string $ name ): array
151+ {
152+ return $ this ->rawCommand ('FT.TAGVALS ' , [$ this ->getIndexName (), $ name ]);
153+ }
154+
130155 /**
131156 * @return mixed
132157 */
@@ -449,14 +474,30 @@ public function verbatim(): QueryBuilderInterface
449474 /**
450475 * @param array $documents
451476 * @param bool $disableAtomicity
477+ * @param bool $replace
452478 */
453- public function addMany (array $ documents , $ disableAtomicity = false )
479+ public function addMany (array $ documents , $ disableAtomicity = false , $ replace = false )
454480 {
481+ $ result = null ;
482+
455483 $ pipe = $ this ->redisClient ->multi ($ disableAtomicity );
456484 foreach ($ documents as $ document ) {
457- $ this ->_add ($ document );
485+ if (is_array ($ document )) {
486+ $ document = $ this ->arrayToDocument ($ document );
487+ }
488+ $ this ->_add ($ document ->setReplace ($ replace ));
489+ }
490+ try {
491+ $ pipe ->exec ();
492+ } catch (RedisException $ exception ) {
493+ $ result = $ exception ->getMessage ();
494+ } catch (RawCommandErrorException $ exception ) {
495+ $ result = $ exception ->getPrevious ()->getMessage ();
496+ }
497+
498+ if ($ result ) {
499+ $ this ->redisClient ->validateRawCommandResults ($ result , 'PIPE ' , [$ this ->indexName , '*MANY ' ]);
458500 }
459- $ pipe ->exec ();
460501 }
461502
462503 /**
@@ -472,7 +513,7 @@ protected function _add(DocumentInterface $document, bool $isFromHash = false)
472513
473514 $ properties = $ isFromHash ? $ document ->getHashDefinition () : $ document ->getDefinition ();
474515 array_unshift ($ properties , $ this ->getIndexName ());
475- return $ this ->rawCommand ($ isFromHash ? 'FT.ADDHASH ' : 'FT.ADD ' , $ properties );
516+ return $ this ->rawCommand ($ isFromHash ? 'HSET ' : 'FT.ADD ' , $ properties );
476517 }
477518
478519 /**
@@ -505,6 +546,15 @@ public function replace($document): bool
505546 return $ this ->_add ($ this ->arrayToDocument ($ document )->setReplace (true ));
506547 }
507548
549+ /**
550+ * @param array $documents
551+ * @param bool $disableAtomicity
552+ */
553+ public function replaceMany (array $ documents , $ disableAtomicity = false )
554+ {
555+ $ this ->addMany ($ documents , $ disableAtomicity , true );
556+ }
557+
508558 /**
509559 * @param $document
510560 * @return bool
0 commit comments