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

The following examples show how to use org.apache.lucene.search.Query#getClass() . 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: SpanNearClauseFactory.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void addSpanQuery(Query q) {
  if (q.getClass() == MatchNoDocsQuery.class)
    return;
  if (! (q instanceof SpanQuery))
    throw new AssertionError("Expected SpanQuery: " + q.toString(getFieldName()));
  float boost = 1f;
  if (q instanceof SpanBoostQuery) {
    SpanBoostQuery bq = (SpanBoostQuery) q;
    boost = bq.getBoost();
    q = bq.getQuery();
  }
  addSpanQueryWeighted((SpanQuery)q, boost);
}
 
Example 2
Source File: ExtendedDismaxQParser.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Recursively examines the given query list for identical structure in all queries.
 * Boosts on BoostQuery-s are ignored, and the contained queries are instead used as the basis for comparison.
 **/
private boolean allSameQueryStructure(List<Query> lst) {
  boolean allSame = true;
  Query firstQuery = lst.get(0);
  if (firstQuery instanceof BoostQuery) {
    firstQuery = ((BoostQuery)firstQuery).getQuery(); // ignore boost; compare contained query
  }
  for (int n = 1 ; n < lst.size(); ++n) {
    Query nthQuery = lst.get(n);
    if (nthQuery instanceof BoostQuery) {
      nthQuery = ((BoostQuery)nthQuery).getQuery();
    }
    if (nthQuery.getClass() != firstQuery.getClass()) {
      allSame = false;
      break;
    }
    if (firstQuery instanceof BooleanQuery) {
      List<BooleanClause> firstBooleanClauses = ((BooleanQuery)firstQuery).clauses();
      List<BooleanClause> nthBooleanClauses = ((BooleanQuery)nthQuery).clauses();
      if (firstBooleanClauses.size() != nthBooleanClauses.size()) {
        allSame = false;
        break;
      }
      for (int c = 0 ; c < firstBooleanClauses.size() ; ++c) {
        if (nthBooleanClauses.get(c).getQuery().getClass() != firstBooleanClauses.get(c).getQuery().getClass()
            || nthBooleanClauses.get(c).getOccur() != firstBooleanClauses.get(c).getOccur()) {
          allSame = false;
          break;
        }
        if (firstBooleanClauses.get(c).getQuery() instanceof BooleanQuery && ! allSameQueryStructure
            (Arrays.asList(firstBooleanClauses.get(c).getQuery(), nthBooleanClauses.get(c).getQuery()))) {
          allSame = false;
          break;
        }
      }
    }
  }
  return allSame;
}
 
Example 3
Source File: QueryExtractor.java    From solr-redis with Apache License 2.0 5 votes vote down vote up
/**
 * Method used for extracting inner queries using given collection of extractors.
 *
 * @param query Query to extract
 * @param extractors Extractors
 * @param extractedQueries Output parameter. List of extracted queries.
 *
 * @throws UnsupportedOperationException This method can trhow UnsupportedOperationException
 */
public static void extractQuery(final Query query, final Iterable<QueryExtractor<? extends Query>> extractors,
    final List<Query> extractedQueries) throws UnsupportedOperationException {
  for (final QueryExtractor extractor : extractors) {
    if (extractor.cls.isAssignableFrom(query.getClass())) {
      extractor.extract(query, extractors, extractedQueries);
      return;
    }
  }
  throw new UnsupportedOperationException("No extractor found for class: " + query.getClass());
}
 
Example 4
Source File: QueryExtractor.java    From solr-redis with Apache License 2.0 5 votes vote down vote up
/**
 ** This static method is used for extracting inner queries field names using given extractros.
 *
 * @param query Query to extract field names from
 * @param extractors Extractors
 * @param extractedFields Output field names
 *
 * @throws UnsupportedOperationException This method can trhow UnsupportedOperationException
 */
public static void extractFields(final Query query, final Iterable<QueryExtractor<? extends Query>> extractors,
    final Set<String> extractedFields) throws UnsupportedOperationException {
  for (final QueryExtractor extractor : extractors) {
    if (extractor.getCls().isAssignableFrom(query.getClass())) {
      extractor.extractSubQueriesFields(query, extractors, extractedFields);
      return;
    }
  }
  throw new UnsupportedOperationException("No extractor found for class: " + query.getClass());
}
 
Example 5
Source File: QueryWritableMapper.java    From incubator-retired-blur with Apache License 2.0 5 votes vote down vote up
public static QueryWritableMapper lookup(Query query) {
  QueryWritableMapper type = queryToType.get(query.getClass());
  if (type == null) {
    throw new RuntimeException("Type [" + query.getClass() + "] for query [" + query + "] not found");
  }
  return type;
}
 
Example 6
Source File: QueryHelper.java    From fess with Apache License 2.0 5 votes vote down vote up
protected QueryBuilder convertQuery(final QueryContext context, final Query query, final float boost) {
    if (query instanceof TermQuery) {
        return convertTermQuery(context, (TermQuery) query, boost);
    } else if (query instanceof TermRangeQuery) {
        return convertTermRangeQuery(context, (TermRangeQuery) query, boost);
    } else if (query instanceof PhraseQuery) {
        return convertPhraseQuery(context, (PhraseQuery) query, boost);
    } else if (query instanceof FuzzyQuery) {
        return convertFuzzyQuery(context, (FuzzyQuery) query, boost);
    } else if (query instanceof PrefixQuery) {
        return convertPrefixQuery(context, (PrefixQuery) query, boost);
    } else if (query instanceof WildcardQuery) {
        return convertWildcardQuery(context, (WildcardQuery) query, boost);
    } else if (query instanceof BooleanQuery) {
        final BooleanQuery booleanQuery = (BooleanQuery) query;
        return convertBooleanQuery(context, booleanQuery, boost);
    } else if (query instanceof MatchAllDocsQuery) {
        return QueryBuilders.matchAllQuery();
    } else if (query instanceof BoostQuery) {
        final BoostQuery boostQuery = (BoostQuery) query;
        return convertQuery(context, boostQuery.getQuery(), boostQuery.getBoost());
    }
    throw new InvalidQueryException(messages -> messages.addErrorsInvalidQueryUnknown(UserMessages.GLOBAL_PROPERTY_KEY), "Unknown q: "
            + query.getClass() + " => " + query);
}
 
Example 7
Source File: DirectUpdateHandler2.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public void deleteByQuery(DeleteUpdateCommand cmd) throws IOException {
  TestInjection.injectDirectUpdateLatch();
  deleteByQueryCommands.increment();
  deleteByQueryCommandsCumulative.mark();
  boolean madeIt=false;
  try {
    if ((cmd.getFlags() & UpdateCommand.IGNORE_INDEXWRITER) != 0) {
      if (ulog != null) ulog.deleteByQuery(cmd);
      madeIt = true;
      return;
    }
    Query q = getQuery(cmd);
    
    boolean delAll = MatchAllDocsQuery.class == q.getClass();

    // currently for testing purposes.  Do a delete of complete index w/o worrying about versions, don't log, clean up most state in update log, etc
    if (delAll && cmd.getVersion() == -Long.MAX_VALUE) {
      synchronized (solrCoreState.getUpdateLock()) {
        deleteAll();
        ulog.deleteAll();
        return;
      }
    }

    //
    // synchronized to prevent deleteByQuery from running during the "open new searcher"
    // part of a commit.  DBQ needs to signal that a fresh reader will be needed for
    // a realtime view of the index.  When a new searcher is opened after a DBQ, that
    // flag can be cleared.  If those thing happen concurrently, it's not thread safe.
    // Also, ulog.deleteByQuery clears caches and is thus not safe to be called between
    // preSoftCommit/postSoftCommit and thus we use the updateLock to prevent this (just
    // as we use around ulog.preCommit... also see comments in ulog.postSoftCommit)
    //
    synchronized (solrCoreState.getUpdateLock()) {

      // We are reopening a searcher before applying the deletes to overcome LUCENE-7344.
      // Once LUCENE-7344 is resolved, we can consider removing this.
      if (ulog != null) ulog.openRealtimeSearcher();

      if (delAll) {
        deleteAll();
      } else {
        RefCounted<IndexWriter> iw = solrCoreState.getIndexWriter(core);
        try {
          iw.get().deleteDocuments(new DeleteByQueryWrapper(q, core.getLatestSchema()));
        } finally {
          iw.decref();
        }
      }

      if (ulog != null) ulog.deleteByQuery(cmd);  // this needs to be protected by the update lock
    }

    madeIt = true;

    updateDeleteTrackers(cmd);

  } finally {
    if (!madeIt) {
      numErrors.increment();
      numErrorsCumulative.mark();
    }
  }
}