下面列出了怎么用org.apache.lucene.search.AutomatonQuery的API类实例代码及写法,或者点击链接到github查看源代码。
/** 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);
}
}
@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';
}
@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");
}
}
}
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);
}
}
@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)) {
};
}
@Override
@SuppressWarnings("unchecked")
Query makeQuery(String fname, Iterator<BytesRef> it) {
Automaton union = Automata.makeStringUnion(IteratorUtils.toList(it));
return new AutomatonQuery(new Term(fname), union);
}
@Override
@SuppressWarnings("unchecked")
Query makeQuery(String fname, Iterator<BytesRef> it) {
Automaton union = Automata.makeStringUnion(IteratorUtils.toList(it));
return new AutomatonQuery(new Term(fname), union);
}
/**
* 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;
}