public NodeWildcardQuery build(final QueryNode queryNode) throws QueryNodeException { final WildcardQueryNode wildcardNode = (WildcardQueryNode) queryNode; final NodeWildcardQuery q = new NodeWildcardQuery(new Term(wildcardNode.getFieldAsString(), wildcardNode.getTextAsString())); final MultiNodeTermQuery.RewriteMethod method = (MultiNodeTermQuery.RewriteMethod)queryNode.getTag(MultiNodeTermRewriteMethodProcessor.TAG_ID); if (method != null) { q.setRewriteMethod(method); } return q; }
/** * Tests Wildcard queries with a question mark. * * @throws IOException if an error occurs */ public void testQuestionmark() throws IOException { this.addDocuments("<metal>", "<metals>", "<mXtals>", "<mXtXls>"); final NodePrimitiveQuery query1 = new NodeWildcardQuery(new Term(DEFAULT_TEST_FIELD, "m?tal")); final NodePrimitiveQuery query2 = new NodeWildcardQuery(new Term(DEFAULT_TEST_FIELD, "metal?")); final NodePrimitiveQuery query3 = new NodeWildcardQuery(new Term(DEFAULT_TEST_FIELD, "metals?")); final NodePrimitiveQuery query4 = new NodeWildcardQuery(new Term(DEFAULT_TEST_FIELD, "m?t?ls")); final NodePrimitiveQuery query5 = new NodeWildcardQuery(new Term(DEFAULT_TEST_FIELD, "M?t?ls")); final NodePrimitiveQuery query6 = new NodeWildcardQuery(new Term(DEFAULT_TEST_FIELD, "meta??")); this.assertMatches(searcher, query1, 1); this.assertMatches(searcher, query2, 1); this.assertMatches(searcher, query3, 0); this.assertMatches(searcher, query4, 3); this.assertMatches(searcher, query5, 0); this.assertMatches(searcher, query6, 1); // Query: 'meta??' matches 'metals' not 'metal' }
@Test public void testWildcardQuery2() throws Exception { final NodeQuery q1 = new NodeWildcardQuery(new Term(SirenTestCase.DEFAULT_TEST_FIELD, "st*e.ca?as")); this._assertSirenQuery(new LuceneProxyNodeQuery(q1), "st*e.ca?as"); }
public void testEquals() { final NodeWildcardQuery wq1 = new NodeWildcardQuery(new Term(DEFAULT_TEST_FIELD, "b*a")); final NodeWildcardQuery wq2 = new NodeWildcardQuery(new Term(DEFAULT_TEST_FIELD, "b*a")); final NodeWildcardQuery wq3 = new NodeWildcardQuery(new Term(DEFAULT_TEST_FIELD, "b*a")); // reflexive? assertEquals(wq1, wq2); assertEquals(wq2, wq1); // transitive? assertEquals(wq2, wq3); assertEquals(wq1, wq3); assertFalse(wq1.equals(null)); final NodeFuzzyQuery fq = new NodeFuzzyQuery(new Term(DEFAULT_TEST_FIELD, "b*a")); assertFalse(wq1.equals(fq)); assertFalse(fq.equals(wq1)); }
/** * Tests Wildcard queries with an asterisk. */ public void testAsterisk() throws IOException { this.addDocuments("<metal>", "<metals>"); final NodePrimitiveQuery query1 = new NodeTermQuery(new Term(DEFAULT_TEST_FIELD, "metal")); final NodePrimitiveQuery query2 = new NodeWildcardQuery(new Term(DEFAULT_TEST_FIELD, "metal*")); final NodePrimitiveQuery query3 = new NodeWildcardQuery(new Term(DEFAULT_TEST_FIELD, "m*tal")); final NodePrimitiveQuery query4 = new NodeWildcardQuery(new Term(DEFAULT_TEST_FIELD, "m*tal*")); final NodePrimitiveQuery query5 = new NodeWildcardQuery(new Term(DEFAULT_TEST_FIELD, "m*tals")); final NodeBooleanQuery query6 = new NodeBooleanQuery(); query6.add(query5, NodeBooleanClause.Occur.SHOULD); final NodeBooleanQuery query7 = new NodeBooleanQuery(); query7.add(query3, NodeBooleanClause.Occur.SHOULD); query7.add(query5, NodeBooleanClause.Occur.SHOULD); // Queries do not automatically lower-case search terms: final NodePrimitiveQuery query8 = new NodeWildcardQuery(new Term(DEFAULT_TEST_FIELD, "M*tal*")); this.assertMatches(searcher, query1, 1); this.assertMatches(searcher, query2, 2); this.assertMatches(searcher, query3, 1); this.assertMatches(searcher, query4, 2); this.assertMatches(searcher, query5, 1); this.assertMatches(searcher, query6, 1); this.assertMatches(searcher, query7, 2); this.assertMatches(searcher, query8, 0); this.assertMatches(searcher, new NodeWildcardQuery(new Term(DEFAULT_TEST_FIELD, "*tall")), 0); this.assertMatches(searcher, new NodeWildcardQuery(new Term(DEFAULT_TEST_FIELD, "*tal")), 1); this.assertMatches(searcher, new NodeWildcardQuery(new Term(DEFAULT_TEST_FIELD, "*tal*")), 2); }
/** * Tests if the ConstantScore filter rewrite return an exception */ @Test(expected=UnsupportedOperationException.class) public void testFilterRewrite() throws IOException { this.addDocument("<nowildcard> <nowildcardx>"); final MultiNodeTermQuery wq = new NodeWildcardQuery(new Term(DEFAULT_TEST_FIELD, "nowildcard")); this.assertMatches(searcher, wq, 1); wq.setRewriteMethod(MultiNodeTermQuery.CONSTANT_SCORE_FILTER_REWRITE); wq.setBoost(0.2F); searcher.rewrite(wq); }
/** * Tests if a SirenWildcardQuery with an empty term is rewritten to an empty * SirenBooleanQuery */ public void testEmptyTerm() throws IOException { this.addDocument("<nowildcard> <nowildcardx>"); final MultiNodeTermQuery wq = new NodeWildcardQuery(new Term(DEFAULT_TEST_FIELD, "")); wq.setRewriteMethod(MultiNodeTermQuery.SCORING_BOOLEAN_QUERY_REWRITE); this.assertMatches(searcher, wq, 0); final Query q = searcher.rewrite(wq); assertTrue(q instanceof NodeBooleanQuery); assertEquals(0, ((NodeBooleanQuery) q).clauses().size()); }
this.addDocuments("<prefix>", "<prefixx>"); MultiNodeTermQuery wq = new NodeWildcardQuery(new Term(DEFAULT_TEST_FIELD, "prefix*")); this.assertMatches(searcher, wq, 2); final Terms terms = MultiFields.getTerms(searcher.getIndexReader(), DEFAULT_TEST_FIELD); assertEquals(searcher.rewrite(expected), searcher.rewrite(wq)); wq = new NodeWildcardQuery(new Term(DEFAULT_TEST_FIELD, "*")); this.assertMatches(searcher, wq, 2); assertFalse(wq.getTermsEnum(terms) instanceof PrefixTermsEnum);
/** * Tests if a SirenWildcardQuery that has no wildcard in the term is rewritten to a single * TermQuery. The boost should be preserved, and the rewrite should return * a SirenConstantScoreQuery if the SirenWildcardQuery had a ConstantScore rewriteMethod. */ public void testTermWithoutWildcard() throws IOException { this.addDocument("<nowildcard> <nowildcardx>"); final MultiNodeTermQuery wq = new NodeWildcardQuery(new Term(DEFAULT_TEST_FIELD, "nowildcard")); this.assertMatches(searcher, wq, 1); wq.setRewriteMethod(MultiNodeTermQuery.SCORING_BOOLEAN_QUERY_REWRITE); wq.setBoost(0.1F); Query q = searcher.rewrite(wq); assertTrue(q instanceof NodeTermQuery); assertEquals(q.getBoost(), wq.getBoost(), 0); wq.setRewriteMethod(MultiNodeTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT); wq.setBoost(0.3F); q = searcher.rewrite(wq); assertTrue(q instanceof NodeConstantScoreQuery); assertEquals(q.getBoost(), wq.getBoost(), 0.1); wq.setRewriteMethod(MultiNodeTermQuery.CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE); wq.setBoost(0.4F); q = searcher.rewrite(wq); assertTrue(q instanceof NodeConstantScoreQuery); assertEquals(q.getBoost(), wq.getBoost(), 0.1); }