/*
(non-Javadoc)

@see mtas.search.spans.util.MtasSpanQuery#rewrite(org.apache.lucene.index.
IndexReader)
176895
@Override
  public MtasSpanQuery rewrite(IndexReader reader) throws IOException {
    MtasSpanQuery newQuery = query.rewrite(reader);
    if (maximumLeft == 0 && maximumRight == 0) {
      return newQuery;
    } else if (((maximumLeft == 0) || (maximumLeft == minimumLeft))
        && ((maximumRight == 0) || (maximumRight == minimumRight))) {
      MtasSpanQuery maximumExpandedQuery = new MtasMaximumExpandSpanQuery(
          newQuery, minimumLeft, maximumLeft, minimumRight, maximumRight);
      return maximumExpandedQuery.rewrite(reader);
    } else if (!query.equals(newQuery)) {
      return new MtasExpandSpanQuery(newQuery, minimumLeft, maximumLeft,
          minimumRight, maximumRight);
    } else {
      return super.rewrite(reader);
    }
  }
1216454
public RewriteResult match(String path) {

        for (CmsRewriteAlias alias : m_aliases) {
            try {
                Pattern pattern = Pattern.compile(alias.getPatternString());
                Matcher matcher = pattern.matcher(path);
                if (matcher.matches()) {
                    String newPath = matcher.replaceFirst(alias.getReplacementString());
                    return new RewriteResult(newPath, alias);
                }
            } catch (PatternSyntaxException e) {
                LOG.warn(e.getLocalizedMessage(), e);
            } catch (IndexOutOfBoundsException e) {
                LOG.warn(e.getLocalizedMessage(), e);
            }
        }
        return null;
    }
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;
        }
    }
773523
public SQLBuilder rewrite(final boolean isSingleRouting) {
        SQLBuilder result = new SQLBuilder(parameters);
        if (sqlTokens.isEmpty()) {
            return appendOriginalLiterals(result);
        }
        appendInitialLiterals(!isSingleRouting, result);
        appendTokensAndPlaceholders(!isSingleRouting, result);
        reviseParameters();
        return result;
    }
1162179
@Override
  public Map<String, Object> rewrite(boolean showDebugInfo) throws IOException {
    Map<String, Object> response = new HashMap<>();
    for (String statsItem : getStatsItems()) {
      if (statsItem.equals(CodecUtil.STATS_TYPE_SUM)) {
        response.put(statsItem, valueSum);
      } else if (statsItem.equals(CodecUtil.STATS_TYPE_N)) {
        response.put(statsItem, valueN);
      } else if (statsItem.equals(CodecUtil.STATS_TYPE_MEAN)) {
        response.put(statsItem, getValue(statsItem));
      } else {
        response.put(statsItem, null);
      }
    }
    if (errorNumber > 0) {
      Map<String, Object> errorResponse = new HashMap<String, Object>();
      for (Entry<String, Integer> entry : getErrorList().entrySet()) {
        errorResponse.put(entry.getKey(), entry.getValue());
      }
      response.put("errorNumber", errorNumber);
      response.put("errorList", errorResponse);
    }
    if (showDebugInfo) {
      response.put("sourceNumber", sourceNumber);
      response.put("stats", "basic");
    }
    return response;
  }
424157
public void updateIndex() {

        if (!isEnabled()) {
            return;
        }

        final IndexWriterConfig indexWriterConfig = new IndexWriterConfig(new StandardAnalyzer());
        indexWriterConfig
                .setCommitOnClose(true)
                .setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);

        try (IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig)) {
            final List<IPortletDefinition> portlets = portletRegistry.getAllPortletDefinitions();
            portlets.forEach(portlet -> indexPortlet(portlet, indexWriter));
        } catch (Exception e) {
            logger.error("Unable to update index", e);
        }
    }
199382
public LuceneIndex current() {
    // Reopen index searcher if necessary.
    IndexSearcher oldSearcher = index.searcher.get();

    try {
      // Guarded lock, to prevent us from unnecessarily synchronizing every time.
      if (oldSearcher == null) {
        synchronized (this) {
          if ((oldSearcher = index.searcher.get()) == null) {
            IndexSearcher newSearcher = new IndexSearcher(index.directory);
            index.searcher.compareAndSet(null, newSearcher);
          }
        }

        // Yes it's open, but is it stale?
      } else {
        DirectoryReader directoryReader = DirectoryReader.openIfChanged(index.directory);
        if (null != directoryReader) {
          index.directory = directoryReader;

          synchronized (this) {
            IndexSearcher newSearcher;
            try {
              newSearcher = new IndexSearcher(directoryReader);
            } catch (AlreadyClosedException e) {
              log.warning("Old index reader could not be reopened. Opening a new one...");
              newSearcher = new IndexSearcher(DirectoryReader.open(index.writer, true));
            }
            if (!index.searcher.compareAndSet(oldSearcher, newSearcher)) {
              // Someone beat us to it. No worries.
              log.finest("Another searcher was already open for this index. Nothing to do.");
            }
          }
        }
      }
    } catch (IOException e) {
      throw new RuntimeException(e);
    }

    return index;
  }
300903
private function _getIndexWriter()
    {
        if ($this->_writer === null) {
            include_once 'Zend/Search/Lucene/Index/Writer.php';
            $this->_writer = new Zend_Search_Lucene_Index_Writer(
                $this->_directory,
                $this->_segmentInfos,
                $this->_formatVersion
            );
        }

        return $this->_writer;
    }
1371318
public void rebuildIndex(String index) throws Exception {

        OpenCms.getSearchManager().rebuildIndex(index, new CmsShellReport(m_cms.getRequestContext().getLocale()));
    }
1265453
@Override
  public MtasSpanQuery rewrite(IndexReader reader) throws IOException {
    MtasSpanQuery newQuery = query.rewrite(reader);
    if (maximumLeft == 0 && maximumRight == 0) {
      return newQuery;
    } else if (!query.equals(newQuery)) {
      return new MtasMaximumExpandSpanQuery(newQuery, minimumLeft, maximumLeft,
          minimumRight, maximumRight);
    } else {
      return super.rewrite(reader);
    }
  }
1152298
public String rewrite() {
        if (sqlTokens.isEmpty()) {
            return originalSQL;
        }
        SQLBuilder result = new SQLBuilder(Collections.emptyList());
        int count = 0;
        for (SQLToken each : sqlTokens) {
            if (0 == count) {
                result.appendLiterals(originalSQL.substring(0, each.getStartIndex()));
            }
            if (each instanceof SchemaToken) {
                appendSchemaPlaceholder(originalSQL, result, (SchemaToken) each, count);
            }
            count++;
        }
        return result.toSQL(masterSlaveRule, metaData.getDataSource());
    }
828614
void reindex(Session session, Index index) {

        setAccessor(index, null);

        RowIterator it = table.rowIterator(session);

        while (it.hasNext()) {
            Row row = it.getNextRow();

            // may need to clear the node before insert
            index.insert(session, this, row);
        }
    }
1255348
public void write(final AbstractIndex index)
		throws IOException
	{

		StringBuilder cmd;

		while (index.size() > 0) {

			System.out.println("Transmit Index [" + index + "]");

			cmd = index.remove();

			if(index instanceof ArticleIndex){
				articleIdxWriter.write(cmd.toString());
			}else if(index instanceof ChronoIndex){
				chronoIdxWriter.write(cmd.toString());
			}else if(index instanceof RevisionIndex){
				revisionIdxWriter.write(cmd.toString());
			}

		}

		if(index instanceof ArticleIndex){
			articleIdxWriter.flush();
		}else if(index instanceof ChronoIndex){
			chronoIdxWriter.flush();
		}else if(index instanceof RevisionIndex){
			revisionIdxWriter.flush();
		}
	}
1276362
void store(IndexReader reader, String field, String prefix, ValueIndex value)
   {
      synchronized (cache)
      {
         Map<Key, ValueIndex> readerCache = cache.get(reader);
         if (readerCache == null)
         {
            readerCache = new HashMap<Key, ValueIndex>();
            cache.put(reader, readerCache);
         }
         readerCache.put(new Key(field, prefix), value);
      }
   }
80471
private boolean rewriteSelectStmt() {
        if (m_mvi != null) {
            final Table view = m_mvi.getDest();
            final String viewName = view.getTypeName();
            // Get the map of select stmt's display column index -> view table (column name, column index)
            m_selectStmt.getFinalProjectionSchema()
                    .resetTableName(viewName, viewName)
                    .toTVEAndFixColumns(m_QueryColumnNameAndIndx_to_MVColumnNameAndIndx.entrySet().stream()
                            .collect(Collectors.toMap(kv -> kv.getKey().getFirst(), Map.Entry::getValue)));
            // change to display column index-keyed map
            final Map<Integer, Pair<String, Integer>> colSubIndx = m_QueryColumnNameAndIndx_to_MVColumnNameAndIndx
                    .entrySet().stream().collect(Collectors.toMap(kv -> kv.getKey().getSecond(), Map.Entry::getValue));
            ParsedSelectStmt.updateTableNames(m_selectStmt.m_aggResultColumns, viewName);
            ParsedSelectStmt.fixColumns(m_selectStmt.m_aggResultColumns, colSubIndx);
            ParsedSelectStmt.updateTableNames(m_selectStmt.m_displayColumns, viewName);
            ParsedSelectStmt.fixColumns(m_selectStmt.m_displayColumns, colSubIndx);
            m_selectStmt.rewriteAsMV(view);
            m_mvi = null; // makes this method re-entrant safe
            return true;
        } else {      // scans all sub-queries for rewriting opportunities
            return m_selectStmt.allScans().stream()
                    .map(scan -> scan instanceof StmtSubqueryScan && rewriteTableAlias((StmtSubqueryScan) scan))
                    .reduce(Boolean::logicalOr).get();
        }
    }
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');
    }
620508
private void outputIndexTermEndElement(final IndexTerm term, final XMLStreamWriter serializer, final boolean indexsee) throws XMLStreamException {
        if (indexsee){
            if (term.getTermPrefix() != null) {
                serializer.writeEndElement(); // see
                inIndexsee = false;
            } else if (inIndexsee) {
                // NOOP
            } else {
                serializer.writeEndElement(); // entry
            }
        } else {
            serializer.writeEndElement(); // entry
        }
    }