@Test public void testWildcardStarRewritten () throws IOException { ki = new KrillIndex(); ki.addDoc(createFieldDoc1()); ki.commit(); // meine* /+w1:2,s0 &Erfahrung // rewritten into meine.* RegexpQuery wcquery = new RegexpQuery(new Term("tokens", "s:meine.*")); SpanMultiTermQueryWrapper<RegexpQuery> mtq = new SpanMultiTermQueryWrapper<RegexpQuery>(wcquery); SpanMultipleDistanceQuery mdsq = new SpanMultipleDistanceQuery( new SpanClassQuery(mtq, (byte) 129), new SpanClassQuery(sq, (byte) 129), constraints, true, true); kr = ki.search(mdsq, (short) 10); assertEquals(4, kr.getMatches().size()); }
@Test public void testWildcardQuestionMarkRewritten () throws IOException { ki = new KrillIndex(); ki.addDoc(createFieldDoc1()); ki.commit(); // meine? /+w1:2,s0 &Erfahrung // meine? rewritten into meine. SpanMultiTermQueryWrapper<RegexpQuery> mtq = new SpanMultiTermQueryWrapper<RegexpQuery>( new RegexpQuery(new Term("tokens", "s:meine."))); SpanMultipleDistanceQuery mdsq = new SpanMultipleDistanceQuery( new SpanClassQuery(mtq, (byte) 129), new SpanClassQuery(sq, (byte) 129), constraints, true, true); kr = ki.search(mdsq, (short) 10); assertEquals(3, kr.getMatches().size()); }
@Test public void testWildcardPlusRewritten () throws IOException { ki = new KrillIndex(); ki.addDoc(createFieldDoc1()); ki.commit(); // C2 meine+ /+w1:2,s0 &Erfahrung // meine+ rewritten into meine.? SpanMultiTermQueryWrapper<RegexpQuery> mtq = new SpanMultiTermQueryWrapper<RegexpQuery>( new RegexpQuery(new Term("tokens", "s:meine.?"))); SpanMultipleDistanceQuery mdsq = new SpanMultipleDistanceQuery( new SpanClassQuery(mtq, (byte) 129), new SpanClassQuery(sq, (byte) 129), constraints, true, true); kr = ki.search(mdsq, (short) 10); assertEquals(4, kr.getMatches().size()); }
@Test public void testWildcardPlusRewritten2 () throws IOException { ki = new KrillIndex(); ki.addDoc(createFieldDoc1()); ki.commit(); // C2 mein+ /+w1:2,s0 &Erfahrung // mein+ rewritten into mein.? SpanMultiTermQueryWrapper<RegexpQuery> mtq = new SpanMultiTermQueryWrapper<RegexpQuery>( new RegexpQuery(new Term("tokens", "s:mein.?"))); SpanMultipleDistanceQuery mdsq = new SpanMultipleDistanceQuery( new SpanClassQuery(mtq, (byte) 129), new SpanClassQuery(sq, (byte) 129), constraints, true, true); kr = ki.search(mdsq, (short) 10); assertEquals(2, kr.getMatches().size()); }
@Override public RegexpQuery build(QueryNode queryNode) throws QueryNodeException { RegexpQueryNode regexpNode = (RegexpQueryNode) queryNode; // TODO: make the maxStates configurable w/ a reasonable default (QueryParserBase uses 10000) RegexpQuery q = new RegexpQuery(new Term(regexpNode.getFieldAsString(), regexpNode.textToBytesRef())); MultiTermQuery.RewriteMethod method = (MultiTermQuery.RewriteMethod) queryNode .getTag(MultiTermRewriteMethodProcessor.TAG_ID); if (method != null) { q.setRewriteMethod(method); } return q; }
public void testNoSuchMultiTermsInSpanFirst() throws Exception { //this hasn't been a problem FuzzyQuery fuzzyNoSuch = new FuzzyQuery(new Term("field", "noSuch"), 1, 0, 1, false); SpanQuery spanNoSuch = new SpanMultiTermQueryWrapper<>(fuzzyNoSuch); SpanQuery spanFirst = new SpanFirstQuery(spanNoSuch, 10); assertEquals(0, searcher.search(spanFirst, 10).totalHits); WildcardQuery wcNoSuch = new WildcardQuery(new Term("field", "noSuch*")); SpanQuery spanWCNoSuch = new SpanMultiTermQueryWrapper<>(wcNoSuch); spanFirst = new SpanFirstQuery(spanWCNoSuch, 10); assertEquals(0, searcher.search(spanFirst, 10).totalHits); RegexpQuery rgxNoSuch = new RegexpQuery(new Term("field", "noSuch")); SpanQuery spanRgxNoSuch = new SpanMultiTermQueryWrapper<>(rgxNoSuch); spanFirst = new SpanFirstQuery(spanRgxNoSuch, 10); assertEquals(0, searcher.search(spanFirst, 10).totalHits); PrefixQuery prfxNoSuch = new PrefixQuery(new Term("field", "noSuch")); SpanQuery spanPrfxNoSuch = new SpanMultiTermQueryWrapper<>(prfxNoSuch); spanFirst = new SpanFirstQuery(spanPrfxNoSuch, 10); assertEquals(0, searcher.search(spanFirst, 10).totalHits); }
@Override protected Query newWildcardQuery(Term t) { if(t.text().equals("*")) { BooleanQuery bQuery = new BooleanQuery(); bQuery.add(createTermQuery(FIELD_FIELDS, t.field()), Occur.SHOULD); bQuery.add(createTermQuery(FIELD_PROPERTIES, t.field()), Occur.SHOULD); return bQuery; } else if (t.text().contains("\\")) { String regexp = SearchLanguageConversion.convert(SearchLanguageConversion.DEF_LUCENE, SearchLanguageConversion.DEF_REGEX, t.text()); return new RegexpQuery(new Term(t.field(), regexp)); } else { org.apache.lucene.search.WildcardQuery query = new org.apache.lucene.search.WildcardQuery(t); query.setRewriteMethod(new MultiTermQuery.TopTermsScoringBooleanQueryRewrite(topTermSpanRewriteLimit)); return query; } }
@Override public final Query regexpQuery(String value, int flags, int maxDeterminizedStates, MultiTermQuery.RewriteMethod method, QueryShardContext context) { failIfNotIndexed(); RegexpQuery query = new RegexpQuery(new Term(name(), indexedValueForSearch(value)), flags, maxDeterminizedStates); if (method != null) { query.setRewriteMethod(method); } return query; }
public void testToQueryRegExpQuery() throws Exception { assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); Query query = queryStringQuery("/foo*bar/").defaultField(STRING_FIELD_NAME) .maxDeterminizedStates(5000) .toQuery(createShardContext()); assertThat(query, instanceOf(RegexpQuery.class)); RegexpQuery regexpQuery = (RegexpQuery) query; assertTrue(regexpQuery.toString().contains("/foo*bar/")); }
public void testRegexpQuery() { MappedFieldType ft = createDefaultFieldType(); ft.setName("field"); ft.setIndexOptions(IndexOptions.DOCS); assertEquals(new RegexpQuery(new Term("field","foo.*")), ft.regexpQuery("foo.*", 0, 10, null, null)); ft.setIndexOptions(IndexOptions.NONE); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> ft.regexpQuery("foo.*", 0, 10, null, null)); assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage()); }
@Override public RegexpQuery build(QueryNode queryNode) throws QueryNodeException { RegexpQueryNode regexpNode = (RegexpQueryNode) queryNode; RegexpQuery q = new RegexpQuery(new Term(regexpNode.getFieldAsString(), regexpNode.textToBytesRef())); MultiTermQuery.RewriteMethod method = (MultiTermQuery.RewriteMethod) queryNode .getTag(MultiTermRewriteMethodProcessor.TAG_ID); if (method != null) { q.setRewriteMethod(method); } return q; }
public SpanRegexQueryWrapper (String field, String re, int flags, boolean caseinsensitive) { if (caseinsensitive) { if (re.startsWith("s:")) { re = re.replaceFirst("s:", "i:"); }; re = re.toLowerCase(); }; RegexpQuery requery = new RegexpQuery(new Term(field, re), flags); query = new SpanMultiTermQueryWrapper<RegexpQuery>(requery); }
public static Query createRegExpQuery(final String field, final String value) { return new Query() { @Override public org.apache.lucene.search.Query getLuceneQuery(Schema schema) { validateType(field, schema, JsonNode.Type.STRING); return new RegexpQuery(new Term(field, value)); } }; }
public void testNullPointerException() throws IOException { RegexpQuery regex = new RegexpQuery(new Term("field", "worl.")); SpanQuery wrappedquery = new SpanMultiTermQueryWrapper<>(regex); MemoryIndex mindex = new MemoryIndex(random().nextBoolean(), random().nextInt(50) * 1024 * 1024); mindex.addField("field", new MockAnalyzer(random()).tokenStream("field", "hello there")); // This throws an NPE assertEquals(0, mindex.search(wrappedquery), 0.00001f); }
public void testPassesIfWrapped() throws IOException { RegexpQuery regex = new RegexpQuery(new Term("field", "worl.")); SpanQuery wrappedquery = new SpanOrQuery(new SpanMultiTermQueryWrapper<>(regex)); MemoryIndex mindex = new MemoryIndex(random().nextBoolean(), random().nextInt(50) * 1024 * 1024); mindex.addField("field", new MockAnalyzer(random()).tokenStream("field", "hello there")); // This passes though assertEquals(0, mindex.search(wrappedquery), 0.00001f); }
public void testSimpleRegex() throws ParseException { String[] fields = new String[] {"a", "b"}; MultiFieldQueryParser mfqp = new MultiFieldQueryParser(fields, new MockAnalyzer(random())); BooleanQuery bq = new BooleanQuery(true); bq.add(new RegexpQuery(new Term("a", "[a-z][123]")), Occur.SHOULD); bq.add(new RegexpQuery(new Term("b", "[a-z][123]")), Occur.SHOULD); assertEquals(bq, mfqp.parse("/[a-z][123]/")); }
public void testNoSuchMultiTermsInNear() throws Exception { //test to make sure non existent multiterms aren't throwing null pointer exceptions FuzzyQuery fuzzyNoSuch = new FuzzyQuery(new Term("field", "noSuch"), 1, 0, 1, false); SpanQuery spanNoSuch = new SpanMultiTermQueryWrapper<>(fuzzyNoSuch); SpanQuery term = new SpanTermQuery(new Term("field", "brown")); SpanQuery near = new SpanNearQuery(new SpanQuery[]{term, spanNoSuch}, 1, true); assertEquals(0, searcher.search(near, 10).totalHits); //flip order near = new SpanNearQuery(new SpanQuery[]{spanNoSuch, term}, 1, true); assertEquals(0, searcher.search(near, 10).totalHits); WildcardQuery wcNoSuch = new WildcardQuery(new Term("field", "noSuch*")); SpanQuery spanWCNoSuch = new SpanMultiTermQueryWrapper<>(wcNoSuch); near = new SpanNearQuery(new SpanQuery[]{term, spanWCNoSuch}, 1, true); assertEquals(0, searcher.search(near, 10).totalHits); RegexpQuery rgxNoSuch = new RegexpQuery(new Term("field", "noSuch")); SpanQuery spanRgxNoSuch = new SpanMultiTermQueryWrapper<>(rgxNoSuch); near = new SpanNearQuery(new SpanQuery[]{term, spanRgxNoSuch}, 1, true); assertEquals(0, searcher.search(near, 10).totalHits); PrefixQuery prfxNoSuch = new PrefixQuery(new Term("field", "noSuch")); SpanQuery spanPrfxNoSuch = new SpanMultiTermQueryWrapper<>(prfxNoSuch); near = new SpanNearQuery(new SpanQuery[]{term, spanPrfxNoSuch}, 1, true); assertEquals(0, searcher.search(near, 10).totalHits); //test single noSuch near = new SpanNearQuery(new SpanQuery[]{spanPrfxNoSuch}, 1, true); assertEquals(0, searcher.search(near, 10).totalHits); //test double noSuch near = new SpanNearQuery(new SpanQuery[]{spanPrfxNoSuch, spanPrfxNoSuch}, 1, true); assertEquals(0, searcher.search(near, 10).totalHits); }
public void testNoSuchMultiTermsInNotNear() throws Exception { //test to make sure non existent multiterms aren't throwing non-matching field exceptions FuzzyQuery fuzzyNoSuch = new FuzzyQuery(new Term("field", "noSuch"), 1, 0, 1, false); SpanQuery spanNoSuch = new SpanMultiTermQueryWrapper<>(fuzzyNoSuch); SpanQuery term = new SpanTermQuery(new Term("field", "brown")); SpanNotQuery notNear = new SpanNotQuery(term, spanNoSuch, 0,0); assertEquals(1, searcher.search(notNear, 10).totalHits); //flip notNear = new SpanNotQuery(spanNoSuch, term, 0,0); assertEquals(0, searcher.search(notNear, 10).totalHits); //both noSuch notNear = new SpanNotQuery(spanNoSuch, spanNoSuch, 0,0); assertEquals(0, searcher.search(notNear, 10).totalHits); WildcardQuery wcNoSuch = new WildcardQuery(new Term("field", "noSuch*")); SpanQuery spanWCNoSuch = new SpanMultiTermQueryWrapper<>(wcNoSuch); notNear = new SpanNotQuery(term, spanWCNoSuch, 0,0); assertEquals(1, searcher.search(notNear, 10).totalHits); RegexpQuery rgxNoSuch = new RegexpQuery(new Term("field", "noSuch")); SpanQuery spanRgxNoSuch = new SpanMultiTermQueryWrapper<>(rgxNoSuch); notNear = new SpanNotQuery(term, spanRgxNoSuch, 1, 1); assertEquals(1, searcher.search(notNear, 10).totalHits); PrefixQuery prfxNoSuch = new PrefixQuery(new Term("field", "noSuch")); SpanQuery spanPrfxNoSuch = new SpanMultiTermQueryWrapper<>(prfxNoSuch); notNear = new SpanNotQuery(term, spanPrfxNoSuch, 1, 1); assertEquals(1, searcher.search(notNear, 10).totalHits); }
public void testNoSuchMultiTermsInOr() throws Exception { //test to make sure non existent multiterms aren't throwing null pointer exceptions FuzzyQuery fuzzyNoSuch = new FuzzyQuery(new Term("field", "noSuch"), 1, 0, 1, false); SpanQuery spanNoSuch = new SpanMultiTermQueryWrapper<>(fuzzyNoSuch); SpanQuery term = new SpanTermQuery(new Term("field", "brown")); SpanOrQuery near = new SpanOrQuery(new SpanQuery[]{term, spanNoSuch}); assertEquals(1, searcher.search(near, 10).totalHits); //flip near = new SpanOrQuery(new SpanQuery[]{spanNoSuch, term}); assertEquals(1, searcher.search(near, 10).totalHits); WildcardQuery wcNoSuch = new WildcardQuery(new Term("field", "noSuch*")); SpanQuery spanWCNoSuch = new SpanMultiTermQueryWrapper<>(wcNoSuch); near = new SpanOrQuery(new SpanQuery[]{term, spanWCNoSuch}); assertEquals(1, searcher.search(near, 10).totalHits); RegexpQuery rgxNoSuch = new RegexpQuery(new Term("field", "noSuch")); SpanQuery spanRgxNoSuch = new SpanMultiTermQueryWrapper<>(rgxNoSuch); near = new SpanOrQuery(new SpanQuery[]{term, spanRgxNoSuch}); assertEquals(1, searcher.search(near, 10).totalHits); PrefixQuery prfxNoSuch = new PrefixQuery(new Term("field", "noSuch")); SpanQuery spanPrfxNoSuch = new SpanMultiTermQueryWrapper<>(prfxNoSuch); near = new SpanOrQuery(new SpanQuery[]{term, spanPrfxNoSuch}); assertEquals(1, searcher.search(near, 10).totalHits); near = new SpanOrQuery(new SpanQuery[]{spanPrfxNoSuch}); assertEquals(0, searcher.search(near, 10).totalHits); near = new SpanOrQuery(new SpanQuery[]{spanPrfxNoSuch, spanPrfxNoSuch}); assertEquals(0, searcher.search(near, 10).totalHits); }
/** * Builds a new RegexpQuery instance * @param regexp Regexp term * @return new RegexpQuery instance */ protected Query newRegexpQuery(Term regexp) { RegexpQuery query = new RegexpQuery(regexp); SchemaField sf = schema.getField(regexp.field()); query.setRewriteMethod(sf.getType().getRewriteMethod(parser, sf)); return query; }
private Query regexQuery(IndexPredicate<V> p) { String re = p.getValue(); if (re.startsWith("^")) { re = re.substring(1); } if (re.endsWith("$") && !re.endsWith("\\$")) { re = re.substring(0, re.length() - 1); } return new RegexpQuery(new Term(p.getField().getName(), re)); }
public String toString(Query query, String field) { StringBuilder buffer = new StringBuilder(); if (query instanceof TermQuery) { buffer.append(toString((TermQuery) query, field)); } else if (query instanceof BooleanQuery) { buffer.append(toString((BooleanQuery) query, field)); } else if (query instanceof WildcardQuery) { buffer.append(toString((WildcardQuery) query, field)); } else if (query instanceof PhraseQuery) { buffer.append(toString((PhraseQuery) query, field)); } else if (query instanceof PrefixQuery) { buffer.append(toString((PrefixQuery) query, field)); } else if (query instanceof MultiPhraseQuery) { buffer.append(toString((MultiPhraseQuery) query, field)); } else if (query instanceof FuzzyQuery) { buffer.append(toString((FuzzyQuery) query, field)); } else if (query instanceof RegexpQuery) { buffer.append(toString((RegexpQuery) query, field)); } else if (query instanceof TermRangeQuery) { buffer.append(toString((TermRangeQuery) query, field)); } else if (query instanceof ConstantScoreQuery) { buffer.append(toString((ConstantScoreQuery) query, field)); } else if (query instanceof DisjunctionMaxQuery) { buffer.append(toString((DisjunctionMaxQuery) query, field)); } else if (query instanceof MatchAllDocsQuery) { buffer.append(toString((MatchAllDocsQuery) query, field)); } else if (query instanceof SynonymQuery) { buffer.append(toString((SynonymQuery) query, field)); } else { buffer.append(query.toString(field)); } return buffer.toString(); }
protected String toString(RegexpQuery regexpQuery, String field) { StringBuilder buffer = new StringBuilder(); Term term = regexpQuery.getRegexp(); if (!term.field().equals(field)) { buffer.append(term.field()); buffer.append(":"); } buffer.append('/'); buffer.append(term.text()); buffer.append('/'); return buffer.toString(); }
@Override protected Query newPrefixQuery(Term prefix) { if (prefix.text().contains("\\")) { String regexp = SearchLanguageConversion.convert(SearchLanguageConversion.DEF_LUCENE, SearchLanguageConversion.DEF_REGEX, prefix.text()); return new RegexpQuery(new Term(prefix.field(), regexp)); } else { return super.newPrefixQuery(prefix); } }
public void testNullPointerException() throws IOException { RegexpQuery regex = new RegexpQuery(new Term("field", "worl.")); SpanQuery wrappedquery = new SpanMultiTermQueryWrapper<RegexpQuery>(regex); MemoryIndex mindex = new MemoryIndex(random().nextBoolean(), random().nextInt(50) * 1024 * 1024); mindex.addField("field", new MockAnalyzer(random()).tokenStream("field", new StringReader("hello there"))); // This throws an NPE assertEquals(0, mindex.search(wrappedquery), 0.00001f); }
public void testPassesIfWrapped() throws IOException { RegexpQuery regex = new RegexpQuery(new Term("field", "worl.")); SpanQuery wrappedquery = new SpanOrQuery(new SpanMultiTermQueryWrapper<RegexpQuery>(regex)); MemoryIndex mindex = new MemoryIndex(random().nextBoolean(), random().nextInt(50) * 1024 * 1024); mindex.addField("field", new MockAnalyzer(random()).tokenStream("field", new StringReader("hello there"))); // This passes though assertEquals(0, mindex.search(wrappedquery), 0.00001f); }
public void testSimpleRegex() throws ParseException { String[] fields = new String[] {"a", "b"}; MultiFieldQueryParser mfqp = new MultiFieldQueryParser(TEST_VERSION_CURRENT, fields, new MockAnalyzer(random())); BooleanQuery bq = new BooleanQuery(true); bq.add(new RegexpQuery(new Term("a", "[a-z][123]")), Occur.SHOULD); bq.add(new RegexpQuery(new Term("b", "[a-z][123]")), Occur.SHOULD); assertEquals(bq, mfqp.parse("/[a-z][123]/")); }
public static void assertEqualsQuery(Query expected, Query actual) { assertEquals(expected.getClass(), actual.getClass()); if (expected instanceof BooleanQuery) { assertEqualsBooleanQuery((BooleanQuery) expected, (BooleanQuery) actual); } else if (expected instanceof SuperQuery) { assertEqualsSuperQuery((SuperQuery) expected, (SuperQuery) actual); } else if (expected instanceof TermQuery) { assertEqualsTermQuery((TermQuery) expected, (TermQuery) actual); } else if (expected instanceof PrefixQuery) { assertEqualsPrefixQuery((PrefixQuery) expected, (PrefixQuery) actual); } else if (expected instanceof WildcardQuery) { assertEqualsWildcardQuery((WildcardQuery) expected, (WildcardQuery) actual); } else if (expected instanceof FuzzyQuery) { assertEqualsFuzzyQuery((FuzzyQuery) expected, (FuzzyQuery) actual); } else if (expected instanceof RegexpQuery) { assertEqualsRegexpQuery((RegexpQuery) expected, (RegexpQuery) actual); } else if (expected instanceof TermRangeQuery) { assertEqualsTermRangeQuery((TermRangeQuery) expected, (TermRangeQuery) actual); } else if (expected instanceof MatchAllDocsQuery) { assertEqualsMatchAllDocsQuery((MatchAllDocsQuery) expected, (MatchAllDocsQuery) actual); } else if (expected instanceof MultiPhraseQuery) { assertEqualsMultiPhraseQuery((MultiPhraseQuery) expected, (MultiPhraseQuery) actual); } else if (expected instanceof PhraseQuery) { assertEqualsPhraseQuery((PhraseQuery) expected, (PhraseQuery) actual); } else if (expected instanceof NumericRangeQuery<?>) { assertEqualsNumericRangeQuery((NumericRangeQuery<?>) expected, (NumericRangeQuery<?>) actual); } else { fail("Type [" + expected.getClass() + "] not supported"); } }
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); } }
public void testNullPointerException() throws IOException { RegexpQuery regex = new RegexpQuery(new Term("field", "worl.")); SpanQuery wrappedquery = new SpanMultiTermQueryWrapper<RegexpQuery>(regex); MemoryIndex mindex = new MemoryIndex(random().nextBoolean(), random().nextInt(50) * 1024 * 1024); mindex.addField("field", new MockAnalyzer(random()).tokenStream("field", "hello there")); // This throws an NPE assertEquals(0, mindex.search(wrappedquery), 0.00001f); }
public void testPassesIfWrapped() throws IOException { RegexpQuery regex = new RegexpQuery(new Term("field", "worl.")); SpanQuery wrappedquery = new SpanOrQuery(new SpanMultiTermQueryWrapper<RegexpQuery>(regex)); MemoryIndex mindex = new MemoryIndex(random().nextBoolean(), random().nextInt(50) * 1024 * 1024); mindex.addField("field", new MockAnalyzer(random()).tokenStream("field", "hello there")); // This passes though assertEquals(0, mindex.search(wrappedquery), 0.00001f); }