/** 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); } }
/** 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 = new RegExp(reg, RegExp.NONE).toAutomaton(); final List<BytesRef> matchedTerms = new ArrayList<BytesRef>(); for(BytesRef t : terms) { if (BasicOperations.run(automaton, t.utf8ToString())) { matchedTerms.add(t); } } Automaton alternate = BasicAutomata.makeStringUnion(matchedTerms); //System.out.println("match " + matchedTerms.size() + " " + alternate.getNumberOfStates() + " states, sigma=" + alternate.getStartPoints().length); //AutomatonTestUtil.minimizeSimple(alternate); //System.out.println("minmize done"); AutomatonQuery a1 = new AutomatonQuery(new Term("field", ""), automaton); AutomatonQuery a2 = new AutomatonQuery(new Term("field", ""), alternate); CheckHits.checkEqual(a1, searcher.search(a1, 25).scoreDocs, searcher.search(a2, 25).scoreDocs); } }
/** 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'; }
/** 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; // this is a hack to get the protected Automaton field in AutomatonQuery, // may break in later lucene versions - we have no getter... for good reasons. final Field automatonField = AutomatonQuery.class.getDeclaredField("automaton"); automatonField.setAccessible(true); Automaton automaton = (Automaton) automatonField.get(q); String prefix = SpecialOperations.getCommonPrefix(automaton); return prefix.length() > 0 && prefix.charAt(0) == '\u0001'; }
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 Filter makeFilter(String fname, BytesRef[] byteRefs) { Automaton union = Automata.makeStringUnion(Arrays.asList(byteRefs)); 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); }