Re-write query into primitive queries in the context of specified index

@param  Zend_Search_Lucene_Interface $index
@return Zend_Search_Lucene_Search_Query
@throws Zend_Search_Lucene_Exception
510778
public function rewrite(Zend_Search_Lucene_Interface $index)
    {
        $this->_matches = array();

        if ($this->_pattern->field === null) {
            // Search through all fields
            $fields = $index->getFieldNames(true /* indexed fields list */);
        } else {
            $fields = array($this->_pattern->field);
        }

        $prefix          = self::_getPrefix($this->_pattern->text);
        $prefixLength    = strlen($prefix);
        $matchExpression = '/^' . str_replace(array('\\?', '\\*'), array('.', '.*'), preg_quote($this->_pattern->text, '/')) . '$/';

        if ($prefixLength < self::$_minPrefixLength) {
            include_once 'Zend/Search/Lucene/Exception.php';
            throw new Zend_Search_Lucene_Exception('At least ' . self::$_minPrefixLength . ' non-wildcard characters are required at the beginning of pattern.');
        }

        /**
 * @todo check for PCRE unicode support may be performed through Zend_Environment in some future 
*/
        if (@preg_match('/\pL/u', 'a') == 1) {
            // PCRE unicode support is turned on
            // add Unicode modifier to the match expression
            $matchExpression .= 'u';
        }

        $maxTerms = Zend_Search_Lucene::getTermsPerQueryLimit();
        foreach ($fields as $field) {
            $index->resetTermsStream();

            include_once 'Zend/Search/Lucene/Index/Term.php';
            if ($prefix != '') {
                $index->skipTo(new Zend_Search_Lucene_Index_Term($prefix, $field));

                while ($index->currentTerm() !== null          &&
                       $index->currentTerm()->field == $field  &&
                       substr($index->currentTerm()->text, 0, $prefixLength) == $prefix) {
                    if (preg_match($matchExpression, $index->currentTerm()->text) === 1) {
                        $this->_matches[] = $index->currentTerm();

                        if ($maxTerms != 0  &&  count($this->_matches) > $maxTerms) {
                            include_once 'Zend/Search/Lucene/Exception.php';
                            throw new Zend_Search_Lucene_Exception('Terms per query limit is reached.');
                        }
                    }

                    $index->nextTerm();
                }
            } else {
                $index->skipTo(new Zend_Search_Lucene_Index_Term('', $field));

                while ($index->currentTerm() !== null  &&  $index->currentTerm()->field == $field) {
                    if (preg_match($matchExpression, $index->currentTerm()->text) === 1) {
                        $this->_matches[] = $index->currentTerm();

                        if ($maxTerms != 0  &&  count($this->_matches) > $maxTerms) {
                            include_once 'Zend/Search/Lucene/Exception.php';
                            throw new Zend_Search_Lucene_Exception('Terms per query limit is reached.');
                        }
                    }

                    $index->nextTerm();
                }
            }

            $index->closeTermsStream();
        }

        if (count($this->_matches) == 0) {
            include_once 'Zend/Search/Lucene/Search/Query/Empty.php';
            return new Zend_Search_Lucene_Search_Query_Empty();
        } else if (count($this->_matches) == 1) {
            include_once 'Zend/Search/Lucene/Search/Query/Term.php';
            return new Zend_Search_Lucene_Search_Query_Term(reset($this->_matches));
        } else {
            include_once 'Zend/Search/Lucene/Search/Query/MultiTerm.php';
            $rewrittenQuery = new Zend_Search_Lucene_Search_Query_MultiTerm();

            foreach ($this->_matches as $matchedTerm) {
                $rewrittenQuery->addTerm($matchedTerm);
            }

            return $rewrittenQuery;
        }
    }
1059038
public function delete($index)
    {
        $request = $this->createRequestInstance(self::INDEX_DELETE, $index, null);

        return $this->client->send($request);
    }
471014
public function delete($id)
    {
        if ($id instanceof Zend_Search_Lucene_Search_QueryHit) {
            /* @var $id Zend_Search_Lucene_Search_QueryHit */
            $id = $id->id;
        }

        if ($id >= $this->_docCount) {
            include_once 'Zend/Search/Lucene/Exception.php';
            throw new Zend_Search_Lucene_Exception('Document id is out of the range.');
        }

        $segmentStartId = 0;
        foreach ($this->_segmentInfos as $segmentInfo) {
            if ($segmentStartId + $segmentInfo->count() > $id) {
                break;
            }

            $segmentStartId += $segmentInfo->count();
        }
        $segmentInfo->delete($id - $segmentStartId);

        $this->_hasChanges = true;
    }
599496
public function getSimilarity()
    {
        if (count($this->_indices) == 0) {
            include_once 'Zend/Search/Lucene/Exception.php';
            throw new Zend_Search_Lucene_Exception('Indices list is empty');
        }

        $similarity = reset($this->_indices)->getSimilarity();

        foreach ($this->_indices as $index) {
            if ($index->getSimilarity() !== $similarity) {
                include_once 'Zend/Search/Lucene/Exception.php';
                throw new Zend_Search_Lucene_Exception('Indices have different similarity.');
            }
        }

        return $similarity;
    }
436830
public static function at($string, $index)
    {
        if (self::length($string) >= ($index + 1)) {
            return $string[$index];
        }

        exception(StringOutIndexException::class);
    }
984904
public function delete($id)
    {
        foreach ($this->_indices as $index) {
            $indexCount = $index->count();

            if ($indexCount > $id) {
                $index->delete($id);
                return;
            }

            $id -= $indexCount;
        }

        include_once 'Zend/Search/Lucene/Exception.php';
        throw new Zend_Search_Lucene_Exception('Document id is out of the range.');
    }
95098
public function addSecondaryIndex(Index $index)
    {
        if ($this->isEmbeddedDocument) {
            throw MappingException::embeddedDocumentsNotIndexable($this->getName());
        }

        if (isset($this->secondaryIndexes[$index->getName()])) {
            throw MappingException::indexExists($this->getName(), $index->getName());
        }

        $this->secondaryIndexes[$index->getName()] = $index;
    }
642462
public function get($index)
    {
        if (null === $index) {
            throw new Exception\InvalidArgumentException('The specified key name is null');
        }

        if (!$this->has($index)) {
            throw new Exception\OutOfBoundsException(sprintf('Index "%s" is out of bounds', $index));
        }

        return $this->collections[$index];
    }
390524
protected function resolveIndexType($index)
    {
        $indexTypes = $this->getIndexTypes();
        $index->type = array_key_exists($index->name, $indexTypes) ? $indexTypes[$index->name] : 'unknown';
        $index->isRt = ($index->type == 'rt');
    }
294709
public function remove($index)
    {
        if (!$index instanceof EntityInterface) {
            $index = $this->getRepository()->get($index);
        }
        
        return $this->findAndRemove($index);
    }
513999
public function seek( $index )
    {
        $this->rewind();

        if ( $index < 1 )
        {
            throw new \OutOfBoundsException( 'Bogus position' );
        }

        while ( $this->_lineNumber < $index && null !== $this->current() )
        {
            $this->next();
        }

        if ( null === $this->current() )
        {
            throw new \OutOfBoundsException( 'Bogus position' );
        }
    }
155188
protected function replace(int $index, MiddlewareInterface $middleware){
        if(!isset($this->queue[$index])){
            throw new \InvalidArgumentException(
                "Queue does not have index {$index}."
            );
        }

        if($this->isAlreadyRegistered($middleware)){
            throw new Exception\MiddlewareAlreadyRegisteredException(
                static::createMsgAlreadyRegisterd($middleware)
            );
        }

        $this->registerd[$middleware]   = true;
        $this->queue[$index]            = $middleware;

        return $this;
    }
685527
public function at($index)
    {
        if (isset($this->_children[$index])) {
            return $this->_children[$index];
        }
        throw new OutOfBoundsException("Index $index does not exist");
    }
152578
public function searchValue($index): self
    {
        $this->generatorToArray();

        // init
        $return = [];

        if ($this->array === []) {
            return static::create(
                [],
                $this->iteratorClass,
                false
            );
        }

        // php cast "bool"-index into "int"-index
        if ((bool) $index === $index) {
            $index = (int) $index;
        }

        if (\array_key_exists($index, $this->array) === true) {
            $return = [$this->array[$index]];
        }

        return static::create(
            $return,
            $this->iteratorClass,
            false
        );
    }
503999
protected function getIndex()
	{
		if (!$this->index) {
			$path = rtrim(Config::get('search.connections.zend.path'), '/') . '/' . $this->name;
			
			try {
				$this->index = \ZendSearch\Lucene\Lucene::open($path);
			} catch (\ZendSearch\Exception\ExceptionInterface $e) {
				$this->index = \ZendSearch\Lucene\Lucene::create($path);
			} catch (\ErrorException $e) {
				if (!file_exists($path)) {
					throw new \Exception(
						"'path' directory does not exist for the 'zend' search driver: '"
						. rtrim(Config::get('search.connections.zend.path'), '/')
						. "'"
					);
				}
				throw $e;
			}
			
			\ZendSearch\Lucene\Analysis\Analyzer\Analyzer::setDefault(
				new \ZendSearch\Lucene\Analysis\Analyzer\Common\Utf8Num\CaseInsensitive()
			);
		}
		
		return $this->index;
	}
944451
public function setIndex($index)
    {
        if (!is_int($index) || $index < 0) {
            throw new \InvalidArgumentException(sprintf('The value\'s index must be a positive integer, "%s" given', var_export($index, true)));
        }
        $this->index = $index;
        return $this;
    }
158839
public function openedRQLastTerm()
    {
        $tokens = Zend_Search_Lucene_Analysis_Analyzer::getDefault()->tokenize($this->_rqFirstTerm, $this->_encoding);
        if (count($tokens) > 1) {
            include_once 'Zend/Search/Lucene/Search/QueryParserException.php';
            throw new Zend_Search_Lucene_Search_QueryParserException('Range query boundary terms must be non-multiple word terms');
        } else if (count($tokens) == 1) {
            include_once 'Zend/Search/Lucene/Index/Term.php';
            $from = new Zend_Search_Lucene_Index_Term(reset($tokens)->getTermText(), $this->_context->getField());
        } else {
            $from = null;
        }

        $tokens = Zend_Search_Lucene_Analysis_Analyzer::getDefault()->tokenize($this->_currentToken->text, $this->_encoding);
        if (count($tokens) > 1) {
            include_once 'Zend/Search/Lucene/Search/QueryParserException.php';
            throw new Zend_Search_Lucene_Search_QueryParserException('Range query boundary terms must be non-multiple word terms');
        } else if (count($tokens) == 1) {
            include_once 'Zend/Search/Lucene/Index/Term.php';
            $to = new Zend_Search_Lucene_Index_Term(reset($tokens)->getTermText(), $this->_context->getField());
        } else {
            $to = null;
        }

        if ($from === null  &&  $to === null) {
            include_once 'Zend/Search/Lucene/Search/QueryParserException.php';
            throw new Zend_Search_Lucene_Search_QueryParserException('At least one range query boundary term must be non-empty term');
        }

        include_once 'Zend/Search/Lucene/Search/Query/Range.php';
        $rangeQuery = new Zend_Search_Lucene_Search_Query_Range($from, $to, false);
        include_once 'Zend/Search/Lucene/Search/QueryEntry/Subquery.php';
        $entry      = new Zend_Search_Lucene_Search_QueryEntry_Subquery($rangeQuery);
        $this->_context->addEntry($entry);
    }