org.apache.lucene.search.AutomatonQuery Java Examples

The following examples show how to use org.apache.lucene.search.AutomatonQuery. 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: TestTermsEnum2.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** tests a pre-intersected automaton against the original */
public void testFiniteVersusInfinite() throws Exception {

  for (int i = 0; i < numIterations; i++) {
    String reg = AutomatonTestUtil.randomRegexp(random());
    Automaton automaton = Operations.determinize(new RegExp(reg, RegExp.NONE).toAutomaton(),
      DEFAULT_MAX_DETERMINIZED_STATES);
    final List<BytesRef> matchedTerms = new ArrayList<>();
    for(BytesRef t : terms) {
      if (Operations.run(automaton, t.utf8ToString())) {
        matchedTerms.add(t);
      }
    }

    Automaton alternate = Automata.makeStringUnion(matchedTerms);
    //System.out.println("match " + matchedTerms.size() + " " + alternate.getNumberOfStates() + " states, sigma=" + alternate.getStartPoints().length);
    //AutomatonTestUtil.minimizeSimple(alternate);
    //System.out.println("minimize done");
    AutomatonQuery a1 = new AutomatonQuery(new Term("field", ""), automaton);
    AutomatonQuery a2 = new AutomatonQuery(new Term("field", ""), alternate, Integer.MAX_VALUE);

    ScoreDoc[] origHits = searcher.search(a1, 25).scoreDocs;
    ScoreDoc[] newHits = searcher.search(a2, 25).scoreDocs;
    CheckHits.checkEqual(a1, origHits, newHits);
  }
}
 
Example #2
Source File: GraphEdgeCollector.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public Query getResultQuery(SchemaField matchField, boolean useAutomaton) {
  if (collectorTerms == null || collectorTerms.size() == 0) {
    // return null if there are no terms (edges) to traverse.
    return null;
  } else {
    // Create a query
    Query q = null;

    // TODO: see if we should dynamically select this based on the frontier size.
    if (useAutomaton) {
      // build an automaton based query for the frontier.
      Automaton autn = buildAutomaton(collectorTerms);
      AutomatonQuery autnQuery = new AutomatonQuery(new Term(matchField.getName()), autn);
      q = autnQuery;
    } else {
      List<BytesRef> termList = new ArrayList<>(collectorTerms.size());
      for (int i = 0; i < collectorTerms.size(); i++) {
        BytesRef ref = new BytesRef();
        collectorTerms.get(i, ref);
        termList.add(ref);
      }
      q = (matchField.hasDocValues() && !matchField.indexed())
              ? new DocValuesTermsQuery(matchField.getName(), termList)
              : new TermInSetQuery(matchField.getName(), termList);
    }

    return q;
  }
}
 
Example #3
Source File: TestReversedWildcardFilterFactory.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** fragile assert: depends on our implementation, but cleanest way to check for now */ 
private boolean wasReversed(SolrQueryParser qp, String query) throws Exception {
  Query q = qp.parse(query);
  if (!(q instanceof AutomatonQuery)) {
    return false;
  }
  Automaton automaton = ((AutomatonQuery) q).getAutomaton();
  String prefix = Operations.getCommonPrefix(Operations.determinize(automaton,
    Operations.DEFAULT_MAX_DETERMINIZED_STATES));
  return prefix.length() > 0 && prefix.charAt(0) == '\u0001';
}
 
Example #4
Source File: MtasJoinQParser.java    From mtas with Apache License 2.0 5 votes vote down vote up
@Override
public Query parse() throws SyntaxError {
  if (id == null) {
    throw new SyntaxError("no " + MTAS_JOIN_QPARSER_COLLECTION);
  } else if (fields == null) {
    throw new SyntaxError("no " + MTAS_JOIN_QPARSER_FIELD);
  } else {

    BooleanQuery.Builder booleanQueryBuilder = new BooleanQuery.Builder();

    MtasSolrCollectionCache mtasSolrJoinCache = null;
    for (PluginHolder<SearchComponent> item : req.getCore()
        .getSearchComponents().getRegistry().values()) {
      if (item.get() instanceof MtasSolrSearchComponent) {
        mtasSolrJoinCache = ((MtasSolrSearchComponent) item.get())
            .getCollectionCache();
      }
    }
    if (mtasSolrJoinCache != null) {
      Automaton automaton;
      try {
        automaton = mtasSolrJoinCache.getAutomatonById(id);
        if (automaton != null) {
          for (String field : fields) {
            booleanQueryBuilder.add(
                new AutomatonQuery(new Term(field), automaton), Occur.SHOULD);
          }
        } else {
          throw new IOException("no data for collection '" + id + "'");
        }
      } catch (IOException e) {
        throw new SyntaxError(
            "could not construct automaton: " + e.getMessage(), e);
      }
      return booleanQueryBuilder.build();
    } else {
      throw new SyntaxError("no MtasSolrSearchComponent found");
    }
  }
}
 
Example #5
Source File: SuperParserTest.java    From incubator-retired-blur with Apache License 2.0 5 votes vote down vote up
private static Term getTerm(RegexpQuery regexpQuery) {
  try {
    Field field = AutomatonQuery.class.getDeclaredField("term");
    field.setAccessible(true);
    return (Term) field.get(regexpQuery);
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}
 
Example #6
Source File: XJoinQParserPlugin.java    From BioSolr with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
Filter makeFilter(String fname, Iterator<BytesRef> it) {
  Automaton union = Automata.makeStringUnion(IteratorUtils.toList(it));
  return new MultiTermQueryWrapperFilter<AutomatonQuery>(new AutomatonQuery(new Term(fname), union)) {
  };
}
 
Example #7
Source File: XJoinQParserPlugin.java    From BioSolr with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
Query makeQuery(String fname, Iterator<BytesRef> it) {
  Automaton union = Automata.makeStringUnion(IteratorUtils.toList(it));
  return new AutomatonQuery(new Term(fname), union);
}
 
Example #8
Source File: XJoinQParserPlugin.java    From BioSolr with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
Query makeQuery(String fname, Iterator<BytesRef> it) {
  Automaton union = Automata.makeStringUnion(IteratorUtils.toList(it));
  return new AutomatonQuery(new Term(fname), union);
}
 
Example #9
Source File: MultiTermHighlighting.java    From lucene-solr with Apache License 2.0 2 votes vote down vote up
/**
 * Indicates if the the leaf query (from {@link QueryVisitor#visitLeaf(Query)}) is a type of query that
 * we can extract automata from.
 */
public static boolean canExtractAutomataFromLeafQuery(Query query) {
  return query instanceof AutomatonQuery || query instanceof FuzzyQuery;
}