类org.apache.lucene.search.AutomatonQuery源码实例Demo

下面列出了怎么用org.apache.lucene.search.AutomatonQuery的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: lucene-solr   文件: TestTermsEnum2.java
/** 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);
  }
}
 
源代码2 项目: lucene-solr   文件: GraphEdgeCollector.java
@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;
  }
}
 
/** 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';
}
 
源代码4 项目: mtas   文件: MtasJoinQParser.java
@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");
    }
  }
}
 
源代码5 项目: incubator-retired-blur   文件: SuperParserTest.java
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);
  }
}
 
源代码6 项目: BioSolr   文件: XJoinQParserPlugin.java
@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)) {
  };
}
 
源代码7 项目: BioSolr   文件: XJoinQParserPlugin.java
@Override
@SuppressWarnings("unchecked")
Query makeQuery(String fname, Iterator<BytesRef> it) {
  Automaton union = Automata.makeStringUnion(IteratorUtils.toList(it));
  return new AutomatonQuery(new Term(fname), union);
}
 
源代码8 项目: BioSolr   文件: XJoinQParserPlugin.java
@Override
@SuppressWarnings("unchecked")
Query makeQuery(String fname, Iterator<BytesRef> it) {
  Automaton union = Automata.makeStringUnion(IteratorUtils.toList(it));
  return new AutomatonQuery(new Term(fname), union);
}
 
源代码9 项目: lucene-solr   文件: MultiTermHighlighting.java
/**
 * 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;
}
 
 类所在包
 同包方法