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/")); }
/** check that the # of hits is the same as from a very simple regexpquery implementation. */ private void assertSame(String regexp) throws IOException { RegexpQuery smart = new RegexpQuery(new Term("field", regexp), RegExp.NONE); DumbRegexpQuery dumb = new DumbRegexpQuery(new Term("field", regexp), RegExp.NONE); // we can't compare the two if automaton rewrites to a simpler enum. // for example: "a\uda07\udcc7?.*?" gets rewritten to a simpler query: // a\uda07* prefixquery. Prefixquery then does the "wrong" thing, which // isn't really wrong as the query was undefined to begin with... but not // automatically comparable. if (!(smart.getTermsEnum(searcher.getIndexReader()) instanceof AutomatonTermsEnum)) return; TopDocs smartDocs = searcher.search(smart, 25); TopDocs dumbDocs = searcher.search(dumb, 25); CheckHits.checkEqual(smart, smartDocs.scoreDocs, dumbDocs.scoreDocs); }