Java Code Examples for org.apache.lucene.search.Query#rewrite()

The following examples show how to use org.apache.lucene.search.Query#rewrite() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FieldQuery.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
void saveTerms( Collection<Query> flatQueries, IndexReader reader ) throws IOException{
  for( Query query : flatQueries ){
    while (query instanceof BoostQuery) {
      query = ((BoostQuery) query).getQuery();
    }
    Set<String> termSet = getTermSet( query );
    if( query instanceof TermQuery )
      termSet.add( ((TermQuery)query).getTerm().text() );
    else if( query instanceof PhraseQuery ){
      for( Term term : ((PhraseQuery)query).getTerms() )
        termSet.add( term.text() );
    }
    else if (query instanceof MultiTermQuery && reader != null) {
      BooleanQuery mtqTerms = (BooleanQuery) query.rewrite(reader);
      for (BooleanClause clause : mtqTerms) {
        termSet.add (((TermQuery) clause.getQuery()).getTerm().text());
      }
    }
    else
      throw new RuntimeException( "query \"" + query.toString() + "\" must be flatten first." );
  }
}
 
Example 2
Source File: SearchImpl.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public Query parseQuery(String expression, String defField, Analyzer analyzer,
                        QueryParserConfig config, boolean rewrite) {
  Objects.requireNonNull(expression);
  Objects.requireNonNull(defField);
  Objects.requireNonNull(analyzer);
  Objects.requireNonNull(config);

  Query query = config.isUseClassicParser() ?
      parseByClassicParser(expression, defField, analyzer, config) :
      parseByStandardParser(expression, defField, analyzer, config);

  if (rewrite) {
    try {
      query = query.rewrite(reader);
    } catch (IOException e) {
      throw new LukeException(String.format(Locale.ENGLISH, "Failed to rewrite query: %s", query.toString()), e);
    }
  }

  return query;
}
 
Example 3
Source File: DrillSidewaysQuery.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public Query rewrite(IndexReader reader) throws IOException {
  Query newQuery = baseQuery;
  while(true) {
    Query rewrittenQuery = newQuery.rewrite(reader);
    if (rewrittenQuery == newQuery) {
      break;
    }
    newQuery = rewrittenQuery;
  }
  if (newQuery == baseQuery) {
    return super.rewrite(reader);
  } else {
    return new DrillSidewaysQuery(newQuery, drillDownCollector, drillSidewaysCollectors, drillDownQueries, scoreSubDocsAtOnce);
  }
}
 
Example 4
Source File: HighlighterTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void doSearching(Query unReWrittenQuery) throws Exception {
  searcher = newSearcher(reader);
  // for any multi-term queries to work (prefix, wildcard, range,fuzzy etc)
  // you must use a rewritten query!
  query = unReWrittenQuery.rewrite(reader);
  if (VERBOSE) System.out.println("Searching for: " + query.toString(FIELD_NAME));
  hits = searcher.search(query, 1000);
}
 
Example 5
Source File: RankerQuery.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
@Override
public Query rewrite(IndexReader reader) throws IOException {
    List<Query> rewrittenQueries = new ArrayList<>(queries.size());
    boolean rewritten = false;
    for (Query query : queries) {
        Query rewrittenQuery = query.rewrite(reader);
        rewritten |= rewrittenQuery != query;
        rewrittenQueries.add(rewrittenQuery);
    }
    return rewritten ? new RankerQuery(rewrittenQueries, features, ranker) : this;
}
 
Example 6
Source File: TestCoreParser.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
protected Query rewrite(Query q) throws IOException {
  return q.rewrite(reader());
}