@Test public void testRegexQueries() throws Exception { NodeRegexpQuery regexp = new NodeRegexpQuery(new Term(SirenTestCase.DEFAULT_TEST_FIELD, "s*e")); regexp.setDatatype("http://www.w3.org/2001/XMLSchema#string"); final Query reg = new LuceneProxyNodeQuery(regexp); this._assertSirenQuery(reg, "/s*e/"); }
public NodeQuery build(final QueryNode queryNode) throws QueryNodeException { final RegexpQueryNode regexpNode = (RegexpQueryNode) queryNode; final NodeRegexpQuery q = new NodeRegexpQuery(new Term(regexpNode.getFieldAsString(), regexpNode.textToBytesRef())); // assign the datatype. We must always have a datatype assigned. q.setDatatype((String) queryNode.getTag(DatatypeQueryNode.DATATYPE_TAGID)); final MultiNodeTermQuery.RewriteMethod method = (MultiNodeTermQuery.RewriteMethod) queryNode.getTag(MultiNodeTermRewriteMethodProcessor.TAG_ID); if (method != null) { q.setRewriteMethod(method); } // if it is tagged as a span query if (regexpNode.getTag(QueryTypeProcessor.QUERYTYPE_TAG) == QueryTypeProcessor.SPAN_QUERYTYPE) { return new MultiTermSpanQuery<>(q); } else { return q; } }
/** Prints a user-readable version of this query. */ @Override public String toString(final String field) { final StringBuilder buffer = new StringBuilder(); buffer.append('/'); buffer.append(term.text()); buffer.append('/'); buffer.append(ToStringUtils.boost(this.getBoost())); return this.wrapToStringWithDatatype(buffer).toString(); }
final NodeRegexpQuery q = new NodeRegexpQuery(new Term(SirenTestCase.DEFAULT_TEST_FIELD, "[a-z][123]")); q.setDatatype("http://www.w3.org/2001/XMLSchema#string"); this._assertSirenQuery(config, q, "/[a-z][123]/"); config.put(ConfigurationKeys.LOWERCASE_EXPANDED_TERMS, true); this._assertSirenQuery(config, q, "/[A-Z][123]/"); q.setBoost(0.5f); this._assertSirenQuery(config, q, "/[A-Z][123]/^0.5"); q.setRewriteMethod(MultiNodeTermQuery.SCORING_BOOLEAN_QUERY_REWRITE); config.put(KeywordConfigurationKeys.MULTI_NODE_TERM_REWRITE_METHOD, MultiNodeTermQuery.SCORING_BOOLEAN_QUERY_REWRITE); this._assertSirenQuery(config, q, "/[A-Z][123]/^0.5"); config.put(KeywordConfigurationKeys.MULTI_NODE_TERM_REWRITE_METHOD, MultiNodeTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT); final NodeRegexpQuery escaped = new NodeRegexpQuery(new Term(df, "[a-z]\\/[123]")); escaped.setDatatype("http://www.w3.org/2001/XMLSchema#string"); this._assertSirenQuery(config, escaped, "/[a-z]\\/[123]/"); final NodeRegexpQuery escaped2 = new NodeRegexpQuery(new Term(df, "[a-z]\\*[123]")); escaped2.setDatatype("http://www.w3.org/2001/XMLSchema#string"); this._assertSirenQuery(config, escaped2, "/[a-z]\\*[123]/"); NodeRegexpQuery nestedRegexp = new NodeRegexpQuery(new Term(df, "[a-z]\\/[123]")); nestedRegexp.setDatatype("http://www.w3.org/2001/XMLSchema#string"); complex.add(nestedRegexp, NodeBooleanClause.Occur.MUST); complex.add(ntq("/etc/init.d/").getQuery(), Occur.MUST); NodeRegexpQuery re = new NodeRegexpQuery(new Term(df, "http.*")); re.setDatatype("http://www.w3.org/2001/XMLSchema#string"); this._assertSirenQuery(config, re, "/http.*/"); re = new NodeRegexpQuery(new Term(df, "http~0.5"));
private int regexQueryNrHits(final String regex) throws IOException { final NodeRegexpQuery query = new NodeRegexpQuery(this.newTerm(regex)); return searcher.search(dq(query), 5).totalHits; }
public void testCustomProvider() throws IOException { final AutomatonProvider myProvider = new AutomatonProvider() { // automaton that matches quick or brown private final Automaton quickBrownAutomaton = BasicOperations.union(Arrays .asList(BasicAutomata.makeString("quick"), BasicAutomata.makeString("brown"), BasicAutomata.makeString("bob"))); public Automaton getAutomaton(final String name) { if (name.equals("quickBrown")) return quickBrownAutomaton; else return null; } }; final NodeRegexpQuery query = new NodeRegexpQuery(this.newTerm("<quickBrown>"), RegExp.ALL, myProvider); assertEquals(1, searcher.search(dq(query), 5).totalHits); }
@Test public void testRegexQueries() throws Exception { ConciseKeywordQueryParser parser = new ConciseKeywordQueryParser(); parser.setAttribute("aaa"); NodeRegexpQuery q = new NodeRegexpQuery(new Term(SirenTestCase.DEFAULT_TEST_FIELD, "aaa:b*d")); q.setDatatype("http://www.w3.org/2001/XMLSchema#string"); assertEquals(q, parser.parse("/b*d/", SirenTestCase.DEFAULT_TEST_FIELD)); // with no attribute defined parser = new ConciseKeywordQueryParser(); q = new NodeRegexpQuery(new Term(SirenTestCase.DEFAULT_TEST_FIELD, "b*d")); q.setDatatype("http://www.w3.org/2001/XMLSchema#string"); assertEquals(q, parser.parse("/b*d/", SirenTestCase.DEFAULT_TEST_FIELD)); }
near = new NearSpanQuery(new SpanQuery[] { term, spanWCNoSuch }, 1, true); NodeRegexpQuery rgxNoSuch = new NodeRegexpQuery(new Term(DEFAULT_TEST_FIELD, "noSuch")); SpanQuery spanRgxNoSuch = new MultiTermSpanQuery<NodeRegexpQuery>(rgxNoSuch); near = new NearSpanQuery(new SpanQuery[] { term, spanRgxNoSuch }, 1, true);
@Test public void testQNameWithRegexp() throws Exception { final String qnames = "./src/test/resources/conf/qnames"; final HashMap<ConfigurationKey, Object> config = new HashMap<ConfigurationKey, Object>(); config.put(KeywordConfigurationKeys.QNAMES, this.loadQNamesFile(qnames)); final Map<String, Analyzer> dts = new HashMap<String, Analyzer>(); dts.put("uri", new AnyURIAnalyzer(LuceneTestCase.TEST_VERSION_CURRENT)); config.put(KeywordConfigurationKeys.DATATYPES_ANALYZERS, dts); final NodeRegexpQuery wq = new NodeRegexpQuery(new Term(SirenTestCase.DEFAULT_TEST_FIELD, "http://xmlns.com/foaf/0.1/nam*")); wq.setDatatype("uri"); Query q = new LuceneProxyNodeQuery(wq); assertEquals(q, parse(config, "uri(/foaf:nam*/)")); // do not try to parse the output, as it generates a URI that needs to be escaped -> non valid query }
assertEquals(1, searcher.search(query, 10).totalHits); NodeRegexpQuery rgxNoSuch = new NodeRegexpQuery(new Term(DEFAULT_TEST_FIELD, "noSuch")); SpanQuery spanRgxNoSuch = new MultiTermSpanQuery<NodeRegexpQuery>(rgxNoSuch); notNear = new NotSpanQuery(term, spanRgxNoSuch, 1, 1);
@Test public void testRegexQueries() throws Exception { BooleanSpanQuery bsq = new BooleanSpanQuery(0, false); bsq.add(tsq("aaa").getQuery(), NodeBooleanClause.Occur.MUST); NodeRegexpQuery regexp = new NodeRegexpQuery(new Term(SirenTestCase.DEFAULT_TEST_FIELD, "s*e")); regexp.setDatatype("http://www.w3.org/2001/XMLSchema#string"); bsq.add(new MultiTermSpanQuery<>(regexp), NodeBooleanClause.Occur.MUST); this._assertSirenQuery(new LuceneProxyNodeQuery(bsq), "(aaa AND /s*e/)~0"); }
assertEquals(1, searcher.search(query, 10).totalHits); NodeRegexpQuery rgxNoSuch = new NodeRegexpQuery(new Term(DEFAULT_TEST_FIELD, "noSuch")); SpanQuery spanRgxNoSuch = new MultiTermSpanQuery<NodeRegexpQuery>(rgxNoSuch); near = new OrSpanQuery(new SpanQuery[] { term, spanRgxNoSuch });