public Query rewrite(Query original) throws IOException {
   Query query = original;
   for (Query rewrittenQuery = query.rewrite(reader);
       rewrittenQuery != query;
       rewrittenQuery = query.rewrite(reader)) {
     query = rewrittenQuery;
   }
   return query;
 }
 /**
  * we rewrite against an empty indexreader: as we don't want things like rangeQueries that don't
  * summarize the document
  */
 private static Query rewrite(Query original) throws IOException {
   Query query = original;
   for (Query rewrittenQuery = query.rewrite(EMPTY_INDEXREADER);
       rewrittenQuery != query;
       rewrittenQuery = query.rewrite(EMPTY_INDEXREADER)) {
     query = rewrittenQuery;
   }
   return query;
 }
 @Override
 public Query rewrite(IndexReader reader) throws IOException {
   Query matchRewritten = match.rewrite(reader);
   Query contextRewritten = context.rewrite(reader);
   if (match != matchRewritten || context != contextRewritten) {
     return new BoostingQuery(matchRewritten, contextRewritten, boost);
   }
   return super.rewrite(reader);
 }
 @Override
 public Query rewrite(IndexReader reader) throws IOException {
   Query query =
       innerRangeQuery(
           lowerTerm, upperTerm, includeLower, includeUpper, timeZone, forcedDateParser);
   return query.rewrite(reader);
 }
    @Override
    public Query rewrite(IndexReader reader) throws IOException {
      SolrRequestInfo info = SolrRequestInfo.getRequestInfo();

      CoreContainer container = info.getReq().getCore().getCoreDescriptor().getCoreContainer();

      final SolrCore fromCore = container.getCore(fromIndex);

      if (fromCore == null) {
        throw new SolrException(
            SolrException.ErrorCode.BAD_REQUEST, "Cross-core join: no such core " + fromIndex);
      }
      RefCounted<SolrIndexSearcher> fromHolder = null;
      fromHolder = fromCore.getRegisteredSearcher();
      final Query joinQuery;
      try {
        joinQuery =
            JoinUtil.createJoinQuery(
                fromField, true, toField, fromQuery, fromHolder.get(), scoreMode);
      } finally {
        fromCore.close();
        fromHolder.decref();
      }
      return joinQuery.rewrite(reader);
    }
 @Override
 public Query rewrite(IndexReader reader) throws IOException {
   SolrRequestInfo info = SolrRequestInfo.getRequestInfo();
   final Query jq =
       JoinUtil.createJoinQuery(
           fromField, true, toField, fromQuery, info.getReq().getSearcher(), scoreMode);
   return jq.rewrite(reader);
 }
 @Override
 public Query rewrite(IndexReader reader) throws IOException {
   Query newQ = q.rewrite(reader);
   if (newQ == q) return this;
   BoostedQuery bq = (BoostedQuery) this.clone();
   bq.q = newQ;
   return bq;
 }
 public Query rewrite(IndexReader reader) throws IOException {
   Query q = mainQuery.rewrite(reader);
   if (q == mainQuery) {
     return this;
   } else {
     return clone().wrap(q);
   }
 }
 @Override
 public Query rewrite(IndexReader reader) throws IOException {
   final Query rewritten = indexQuery.rewrite(reader);
   if (rewritten != indexQuery) {
     return new CompositeVerifyQuery(rewritten, predicateValueSource);
   }
   return super.rewrite(reader);
 }
 @Override
 // See TopChildrenQuery#rewrite
 public Query rewrite(IndexReader reader) throws IOException {
   if (rewrittenParentQuery == null) {
     rewrittenParentQuery = originalParentQuery.rewrite(reader);
   }
   return this;
 }
 @Override
 public Query rewrite(IndexReader reader) throws IOException {
   final Query childRewrite = childQuery.rewrite(reader);
   if (childRewrite != childQuery) {
     return new ToParentBlockJoinQuery(origChildQuery, childRewrite, parentsFilter, scoreMode);
   } else {
     return super.rewrite(reader);
   }
 }
 @Override
 public Query rewrite(IndexReader reader) throws IOException {
   Query rewritten = super.rewrite(reader);
   if (rewritten != this) {
     return rewritten;
   }
   Query newQ = subQuery.rewrite(reader);
   if (newQ == subQuery) return this;
   return new FiltersFunctionScoreQuery(
       newQ, scoreMode, filterFunctions, maxBoost, minScore, combineFunction);
 }
 @Override
 public Query rewrite(IndexReader reader) throws IOException {
   if (query != null) {
     Query rewritten = query.rewrite(reader);
     if (rewritten != query) {
       rewritten = new ConstantScoreQuery(rewritten);
       rewritten.setBoost(this.getBoost());
       return rewritten;
     }
   }
   return this;
 }
  @Override
  public void process(ResponseBuilder rb) throws IOException {
    SolrQueryRequest req = rb.req;
    if (rb.doHighlights) {
      SolrParams params = req.getParams();

      String[] defaultHighlightFields; // TODO: get from builder by default?

      if (rb.getQparser() != null) {
        defaultHighlightFields = rb.getQparser().getDefaultHighlightFields();
      } else {
        defaultHighlightFields = params.getParams(CommonParams.DF);
      }

      Query highlightQuery = rb.getHighlightQuery();
      if (highlightQuery == null) {
        if (rb.getQparser() != null) {
          try {
            highlightQuery = rb.getQparser().getHighlightQuery();
            rb.setHighlightQuery(highlightQuery);
          } catch (Exception e) {
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);
          }
        } else {
          highlightQuery = rb.getQuery();
          rb.setHighlightQuery(highlightQuery);
        }
      }

      if (highlightQuery != null) {
        boolean rewrite =
            !(Boolean.valueOf(req.getParams().get(HighlightParams.USE_PHRASE_HIGHLIGHTER, "true"))
                && Boolean.valueOf(
                    req.getParams().get(HighlightParams.HIGHLIGHT_MULTI_TERM, "true")));
        highlightQuery =
            rewrite ? highlightQuery.rewrite(req.getSearcher().getIndexReader()) : highlightQuery;
      }

      // No highlighting if there is no query -- consider q.alt="*:*
      if (highlightQuery != null) {
        NamedList sumData =
            highlighter.doHighlighting(
                rb.getResults().docList, highlightQuery, req, defaultHighlightFields);

        if (sumData != null) {
          // TODO ???? add this directly to the response?
          rb.rsp.add("highlighting", sumData);
        }
      }
    }
  }
 /**
  * Optimize our representation and our subqueries representations
  *
  * @param reader the IndexReader we query
  * @return an optimized copy of us (which may not be a copy if there is nothing to optimize)
  */
 @Override
 public Query rewrite(IndexReader reader) throws IOException {
   int numDisjunctions = disjuncts.size();
   if (numDisjunctions == 1) {
     Query singleton = disjuncts.get(0);
     Query result = singleton.rewrite(reader);
     if (getBoost() != 1.0f) {
       if (result == singleton) result = result.clone();
       result.setBoost(getBoost() * result.getBoost());
     }
     return result;
   }
   DisjunctionMaxQuery clone = null;
   for (int i = 0; i < numDisjunctions; i++) {
     Query clause = disjuncts.get(i);
     Query rewrite = clause.rewrite(reader);
     if (rewrite != clause) {
       if (clone == null) clone = this.clone();
       clone.disjuncts.set(i, rewrite);
     }
   }
   if (clone != null) return clone;
   else return this;
 }
 // Rewrite invocation logic:
 // 1) query_then_fetch (default): Rewrite is execute as part of the createWeight invocation, when
 // search child docs.
 // 2) dfs_query_then_fetch:: First rewrite and then createWeight is executed. During query phase
 // rewrite isn't
 // executed any more because searchContext#queryRewritten() returns true.
 @Override
 public Query rewrite(IndexReader reader) throws IOException {
   if (rewrittenChildQuery == null) {
     rewrittenChildQuery = originalChildQuery.rewrite(reader);
     rewriteIndexReader = reader;
   }
   // We can always return the current instance, and we can do this b/c the child query is executed
   // separately
   // before the main query (other scope) in a different IS#search() invocation than the main
   // query.
   // In fact we only need override the rewrite method because for the dfs phase, to get also
   // global document
   // frequency for the child query.
   return this;
 }
 @Override
 public Query rewrite(IndexReader reader) throws IOException {
   final Query originalQueryRewrite = originalQuery.rewrite(reader);
   if (originalQueryRewrite != originalQuery) {
     return new TermsIncludingScoreQuery(
         field,
         multipleValuesPerDocument,
         terms,
         scores,
         ords,
         originalQueryRewrite,
         originalQuery);
   } else {
     return super.rewrite(reader);
   }
 }
  @Override
  public Query rewrite(IndexReader reader) throws IOException {
    Query rewritten;
    if (rewrittenParentQuery == null) {
      rewritten = originalParentQuery.rewrite(reader);
    } else {
      rewritten = rewrittenParentQuery;
    }
    if (rewritten == rewrittenParentQuery) {
      return this;
    }

    // See TopChildrenQuery#rewrite
    ParentQuery rewrite = new ParentQuery(this, rewritten);
    int index = searchContext.rewrites().indexOf(this);
    searchContext.rewrites().set(index, rewrite);
    return rewrite;
  }
  /**
   * Optimize our representation and our subqueries representations
   *
   * @param reader the IndexReader we query
   * @return an optimized copy of us (which may not be a copy if there is nothing to optimize)
   */
  @Override
  public Query rewrite(IndexReader reader) throws IOException {
    if (disjuncts.length == 1) {
      return disjuncts[0];
    }

    boolean actuallyRewritten = false;
    List<Query> rewrittenDisjuncts = new ArrayList<>();
    for (Query sub : disjuncts) {
      Query rewrittenSub = sub.rewrite(reader);
      actuallyRewritten |= rewrittenSub != sub;
      rewrittenDisjuncts.add(rewrittenSub);
    }

    if (actuallyRewritten) {
      return new DisjunctionMaxQuery(rewrittenDisjuncts, tieBreakerMultiplier);
    }

    return super.rewrite(reader);
  }
  /*(non-Javadoc) @see org.apache.lucene.search.Query#rewrite(org.apache.lucene.index.IndexReader) */
  @Override
  public Query rewrite(IndexReader reader) throws IOException {
    CustomScoreQuery clone = null;

    final Query sq = subQuery.rewrite(reader);
    if (sq != subQuery) {
      clone = (CustomScoreQuery) clone();
      clone.subQuery = sq;
    }

    for (int i = 0; i < valSrcQueries.length; i++) {
      final ValueSourceQuery v = (ValueSourceQuery) valSrcQueries[i].rewrite(reader);
      if (v != valSrcQueries[i]) {
        if (clone == null) clone = (CustomScoreQuery) clone();
        clone.valSrcQueries[i] = v;
      }
    }

    return (clone == null) ? this : clone;
  }
 @Override
 public Query rewrite(IndexReader reader) throws IOException {
   if (query != null) {
     Query rewritten = query.rewrite(reader);
     if (rewritten != query) {
       rewritten = new ImageFeatureScoreQuery(rewritten);
       rewritten.setBoost(this.getBoost());
       return rewritten;
     }
   } else {
     assert filter != null;
     // Fix outdated usage pattern from Lucene 2.x/early-3.x:
     // because ConstantScoreQuery only accepted filters,
     // QueryWrapperFilter was used to wrap queries.
     if (filter instanceof QueryWrapperFilter) {
       final QueryWrapperFilter qwf = (QueryWrapperFilter) filter;
       final Query rewritten = new ImageFeatureScoreQuery(qwf.getQuery().rewrite(reader));
       rewritten.setBoost(this.getBoost());
       return rewritten;
     }
   }
   return this;
 }