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

@param  Zend_Search_Lucene_Interface $index
@return Zend_Search_Lucene_Search_Query
508004
public function rewrite(Zend_Search_Lucene_Interface $index)
    {
        if ($this->_field === null) {
            include_once 'Zend/Search/Lucene/Search/Query/Boolean.php';
            $query = new Zend_Search_Lucene_Search_Query_Boolean();

            $hasInsignificantSubqueries = false;

            include_once 'Zend/Search/Lucene.php';
            if (Zend_Search_Lucene::getDefaultSearchField() === null) {
                $searchFields = $index->getFieldNames(true);
            } else {
                $searchFields = array(Zend_Search_Lucene::getDefaultSearchField());
            }

            include_once 'Zend/Search/Lucene/Search/Query/Preprocessing/Fuzzy.php';
            foreach ($searchFields as $fieldName) {
                $subquery = new Zend_Search_Lucene_Search_Query_Preprocessing_Fuzzy(
                    $this->_word,
                    $this->_encoding,
                    $fieldName,
                    $this->_minimumSimilarity
                );

                $rewrittenSubquery = $subquery->rewrite($index);

                if (!($rewrittenSubquery instanceof Zend_Search_Lucene_Search_Query_Insignificant  
                    || $rewrittenSubquery instanceof Zend_Search_Lucene_Search_Query_Empty) 
                ) {
                    $query->addSubquery($rewrittenSubquery);
                }

                if ($rewrittenSubquery instanceof Zend_Search_Lucene_Search_Query_Insignificant) {
                    $hasInsignificantSubqueries = true;
                }
            }

            $subqueries = $query->getSubqueries();

            if (count($subqueries) == 0) {
                $this->_matches = array();
                if ($hasInsignificantSubqueries) {
                    include_once 'Zend/Search/Lucene/Search/Query/Insignificant.php';
                    return new Zend_Search_Lucene_Search_Query_Insignificant();
                } else {
                    include_once 'Zend/Search/Lucene/Search/Query/Empty.php';
                    return new Zend_Search_Lucene_Search_Query_Empty();
                }
            }

            if (count($subqueries) == 1) {
                $query = reset($subqueries);
            }

            $query->setBoost($this->getBoost());

            $this->_matches = $query->getQueryTerms();
            return $query;
        }

        // -------------------------------------
        // Recognize exact term matching (it corresponds to Keyword fields stored in the index)
        // encoding is not used since we expect binary matching
        include_once 'Zend/Search/Lucene/Index/Term.php';
        $term = new Zend_Search_Lucene_Index_Term($this->_word, $this->_field);
        if ($index->hasTerm($term)) {
            include_once 'Zend/Search/Lucene/Search/Query/Fuzzy.php';
            $query = new Zend_Search_Lucene_Search_Query_Fuzzy($term, $this->_minimumSimilarity);
            $query->setBoost($this->getBoost());

            // Get rewritten query. Important! It also fills terms matching container.
            $rewrittenQuery = $query->rewrite($index);
            $this->_matches = $query->getQueryTerms();

            return $rewrittenQuery;
        }


        // -------------------------------------
        // Recognize wildcard queries

        /**
 * @todo check for PCRE unicode support may be performed through Zend_Environment in some future 
*/
        if (@preg_match('/\pL/u', 'a') == 1) {
            $subPatterns = preg_split('/[*?]/u', iconv($this->_encoding, 'UTF-8', $this->_word));
        } else {
            $subPatterns = preg_split('/[*?]/', $this->_word);
        }
        if (count($subPatterns) > 1) {
            include_once 'Zend/Search/Lucene/Search/QueryParserException.php';
            throw new Zend_Search_Lucene_Search_QueryParserException('Fuzzy search doesn\'t support wildcards (except within Keyword fields).');
        }


        // -------------------------------------
        // Recognize one-term multi-term and "insignificant" queries
        include_once 'Zend/Search/Lucene/Analysis/Analyzer.php';
        $tokens = Zend_Search_Lucene_Analysis_Analyzer::getDefault()->tokenize($this->_word, $this->_encoding);

        if (count($tokens) == 0) {
            $this->_matches = array();
            include_once 'Zend/Search/Lucene/Search/Query/Insignificant.php';
            return new Zend_Search_Lucene_Search_Query_Insignificant();
        }

        if (count($tokens) == 1) {
            include_once 'Zend/Search/Lucene/Index/Term.php';
            $term  = new Zend_Search_Lucene_Index_Term($tokens[0]->getTermText(), $this->_field);
            include_once 'Zend/Search/Lucene/Search/Query/Fuzzy.php';
            $query = new Zend_Search_Lucene_Search_Query_Fuzzy($term, $this->_minimumSimilarity);
            $query->setBoost($this->getBoost());

            // Get rewritten query. Important! It also fills terms matching container.
            $rewrittenQuery = $query->rewrite($index);
            $this->_matches = $query->getQueryTerms();

            return $rewrittenQuery;
        }

        // Word is tokenized into several tokens
        include_once 'Zend/Search/Lucene/Search/QueryParserException.php';
        throw new Zend_Search_Lucene_Search_QueryParserException('Fuzzy search is supported only for non-multiple word terms');
    }
1054051
public function schemaByIndex($index)
    {
        $schema = array();

        foreach ($this->collection->schema() as $value) {
            $schema[] = $value;
        }

        return (empty($schema[$index])) ? null : $schema[$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');
    }
685527
public function at($index)
    {
        if (isset($this->_children[$index])) {
            return $this->_children[$index];
        }
        throw new OutOfBoundsException("Index $index does not exist");
    }
1049789
public function item(int $index)
    {
        $this->tokenize();
        if ($index >= count($this->tokens)) {
            return null;
        }
        return $this->tokens[$index];
    }
149175
public function getPartByIndex($index)
    {
        if (!isset($this->_parts[$index])) {
            return null;
        }

        $part = $this->_parts[$index];
        $part->parent = $this;

        return $part;
    }
1351313
public static function getDefaultSearchField()
    {
        if (count($this->_indices) == 0) {
            include_once 'Zend/Search/Lucene/Exception.php';
            throw new Zend_Search_Lucene_Exception('Indices list is empty');
        }

        $defaultSearchField = reset($this->_indices)->getDefaultSearchField();

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

        return $defaultSearchField;
    }
581696
public function apply($input, $index, IntermediateContext $context)
    {
        if (! $context->memory->contains($this)) {
            $context->memory->offsetSet($this, 0);
        }

        $count = $context->memory->offsetGet($this);
        $context->memory->offsetSet($this, $count+1);

        $takeIt = $count >= $this->skip;

        $context->outUseItem     = $takeIt;
        $context->outCanContinue = true;

        return $input;
    }
1146404
public function addIndex($index)
    {
        if ($index instanceof Index) {
            $index = $index->getName();
        }

        if (!\is_scalar($index)) {
            throw new InvalidException('Invalid param type');
        }

        $this->_indices[] = (string) $index;

        return $this;
    }
294709
public function remove($index)
    {
        if (!$index instanceof EntityInterface) {
            $index = $this->getRepository()->get($index);
        }
        
        return $this->findAndRemove($index);
    }
1363434
public function withIndex(Fluent $index)
    {
        $this->indexes[] = $index;

        if ($index->name == 'unique') {
            $this->unique[] = $index;
        }

        return $this;
    }
1090848
public function create($index)
    {
        $realname = $this->storage->getIndexRealname($index);

        $search = (new Search($this->client))->setIndex($realname);

        if ($this->dispatcher) {
            $this->dispatcher->dispatch('ucms_search.search_create', new GenericEvent($search));
        }

        return $search;
    }
512943
public function parent(Int $index = 1) : stdClass
    {
        return $this->_layer($this->debug, $index, $index + 1);
    }
1308524
public function rewrite(Zend_Search_Lucene_Interface $index)
    {
        if ($this->_term->field != null) {
            return $this;
        } else {
            include_once 'Zend/Search/Lucene/Search/Query/MultiTerm.php';
            $query = new Zend_Search_Lucene_Search_Query_MultiTerm();
            $query->setBoost($this->getBoost());

            include_once 'Zend/Search/Lucene/Index/Term.php';
            foreach ($index->getFieldNames(true) as $fieldName) {
                $term = new Zend_Search_Lucene_Index_Term($this->_term->text, $fieldName);

                $query->addTerm($term);
            }

            return $query->rewrite($index);
        }
    }
1076872
public function sort($index)
    {
        $this->sort_order = $index->get_parameters('ORDER');

        if (empty($this->raw_data)) {
            return;
        }

        // when sorting search result it's good to make the index smaller
        if ($index->count() != $this->count_messages()) {
            $index->filter($this->get());
        }

        $result  = array_fill_keys($index->get(), null);
        $datalen = strlen($this->raw_data);
        $start   = 0;

        // Here we're parsing raw_data twice, we want only one big array
        // in memory at a time

        // Assign roots
        while (($pos = @strpos($this->raw_data, self::SEPARATOR_ELEMENT, $start))
            || ($start < $datalen && ($pos = $datalen))
        ) {
            $len   = $pos - $start;
            $elem  = substr($this->raw_data, $start, $len);
            $start = $pos + 1;

            $items = explode(self::SEPARATOR_ITEM, $elem);
            $root  = (int) array_shift($items);

            if ($root) {
                $result[$root] = $root;
                foreach ($items as $item) {
                    list($lv, $id) = explode(self::SEPARATOR_LEVEL, $item);
                    $result[$id] = $root;
                }
            }
        }

        // get only unique roots
        $result = array_filter($result); // make sure there are no nulls
        $result = array_unique($result);

        // Re-sort raw data
        $result = array_fill_keys($result, null);
        $start = 0;

        while (($pos = @strpos($this->raw_data, self::SEPARATOR_ELEMENT, $start))
            || ($start < $datalen && ($pos = $datalen))
        ) {
            $len   = $pos - $start;
            $elem  = substr($this->raw_data, $start, $len);
            $start = $pos + 1;

            $npos = strpos($elem, self::SEPARATOR_ITEM);
            $root = (int) ($npos ? substr($elem, 0, $npos) : $elem);

            $result[$root] = $elem;
        }

        $this->raw_data = implode(self::SEPARATOR_ELEMENT, $result);
    }
791085
public function getQuery($index = false)
    {
        return ($index) ? array_value($this->query, $index) : $this->query;
    }
1195181
public function rewrite(Zend_Search_Lucene_Interface $index)
    {
        $this->_matches = array();

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

        include_once 'Zend/Search/Lucene.php';
        $maxTerms = Zend_Search_Lucene::getTermsPerQueryLimit();
        foreach ($fields as $field) {
            $index->resetTermsStream();

            include_once 'Zend/Search/Lucene/Index/Term.php';
            if ($this->_lowerTerm !== null) {
                $lowerTerm = new Zend_Search_Lucene_Index_Term($this->_lowerTerm->text, $field);

                $index->skipTo($lowerTerm);

                if (!$this->_inclusive  
                    && $index->currentTerm() == $lowerTerm
                ) {
                    // Skip lower term
                    $index->nextTerm();
                }
            } else {
                $index->skipTo(new Zend_Search_Lucene_Index_Term('', $field));
            }


            if ($this->_upperTerm !== null) {
                // Walk up to the upper term
                $upperTerm = new Zend_Search_Lucene_Index_Term($this->_upperTerm->text, $field);

                while ($index->currentTerm() !== null          &&
                       $index->currentTerm()->field == $field  &&
                       $index->currentTerm()->text  <  $upperTerm->text) {
                    $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();
                }

                if ($this->_inclusive  &&  $index->currentTerm() == $upperTerm) {
                    // Include upper term into result
                    $this->_matches[] = $upperTerm;
                }
            } else {
                // Walk up to the end of field data
                while ($index->currentTerm() !== null  &&  $index->currentTerm()->field == $field) {
                    $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;
        }
    }