Examples of RegexpQuery

The supported syntax is documented in the {@link RegExp} class.Note this might be different than other regular expression implementations. For some alternatives with different syntax, look under the sandbox.

Note this query can be slow, as it needs to iterate over many terms. In order to prevent extremely slow RegexpQueries, a Regexp term should not start with the expression .* @see RegExp @lucene.experimental


Examples of org.apache.lucene.search.RegexpQuery

  }
 
  public void testRegexpQuery() throws Exception {
    makeIndexStrMV();
    Term term = new Term(F, "d[a-z].g");
    defgMultiTermQueryTest(new RegexpQuery (term));
  }
View Full Code Here

Examples of org.apache.lucene.search.RegexpQuery

  }
 
  public void testRegexps() throws Exception {
    StandardQueryParser qp = new StandardQueryParser();
    final String df = "field" ;
    RegexpQuery q = new RegexpQuery(new Term("field", "[a-z][123]"));
    assertEquals(q, qp.parse("/[a-z][123]/", df));
    qp.setLowercaseExpandedTerms(true);
    assertEquals(q, qp.parse("/[A-Z][123]/", df));
    q.setBoost(0.5f);
    assertEquals(q, qp.parse("/[A-Z][123]/^0.5", df));
    qp.setMultiTermRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE);
    q.setRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE);
    assertTrue(qp.parse("/[A-Z][123]/^0.5", df) instanceof RegexpQuery);
    assertEquals(q, qp.parse("/[A-Z][123]/^0.5", df));
    assertEquals(MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE, ((RegexpQuery)qp.parse("/[A-Z][123]/^0.5", df)).getRewriteMethod());
    qp.setMultiTermRewriteMethod(MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT);
   
    Query escaped = new RegexpQuery(new Term("field", "[a-z]\\/[123]"));
    assertEquals(escaped, qp.parse("/[a-z]\\/[123]/", df));
    Query escaped2 = new RegexpQuery(new Term("field", "[a-z]\\*[123]"));
    assertEquals(escaped2, qp.parse("/[a-z]\\*[123]/", df));
   
    BooleanQuery complex = new BooleanQuery();
    complex.add(new RegexpQuery(new Term("field", "[a-z]\\/[123]")), Occur.MUST);
    complex.add(new TermQuery(new Term("path", "/etc/init.d/")), Occur.MUST);
    complex.add(new TermQuery(new Term("field", "/etc/init[.]d/lucene/")), Occur.SHOULD);
    assertEquals(complex, qp.parse("/[a-z]\\/[123]/ AND path:\"/etc/init.d/\" OR \"/etc\\/init\\[.\\]d/lucene/\" ", df));
   
    Query re = new RegexpQuery(new Term("field", "http.*"));
    assertEquals(re, qp.parse("field:/http.*/", df));
    assertEquals(re, qp.parse("/http.*/", df));
   
    re = new RegexpQuery(new Term("field", "http~0.5"));
    assertEquals(re, qp.parse("field:/http~0.5/", df));
    assertEquals(re, qp.parse("/http~0.5/", df));
   
    re = new RegexpQuery(new Term("field", "boo"));
    assertEquals(re, qp.parse("field:/boo/", df));
    assertEquals(re, qp.parse("/boo/", df));
   
    assertEquals(new TermQuery(new Term("field", "/boo/")), qp.parse("\"/boo/\"", df));
    assertEquals(new TermQuery(new Term("field", "/boo/")), qp.parse("\\/boo\\/", df));
   
    BooleanQuery two = new BooleanQuery();
    two.add(new RegexpQuery(new Term("field", "foo")), Occur.SHOULD);
    two.add(new RegexpQuery(new Term("field", "bar")), Occur.SHOULD);
    assertEquals(two, qp.parse("field:/foo/ field:/bar/", df));
    assertEquals(two, qp.parse("/foo/ /bar/", df));
  }
View Full Code Here

Examples of org.apache.lucene.search.RegexpQuery

    parser.setMultiFields(fields);
    parser.setDefaultOperator(StandardQueryConfigHandler.Operator.AND);
    parser.setAnalyzer(new MockAnalyzer(random()));

    BooleanQuery exp = new BooleanQuery();
    exp.add(new BooleanClause(new RegexpQuery(new Term("b", "ab.+")), BooleanClause.Occur.SHOULD));//TODO spezification? was "MUST"
    exp.add(new BooleanClause(new RegexpQuery(new Term("t", "ab.+")), BooleanClause.Occur.SHOULD));//TODO spezification? was "MUST"

    assertEquals(exp, parser.parse("/ab.+/", null));

    RegexpQuery regexpQueryexp = new RegexpQuery(new Term("test", "[abc]?[0-9]"));

    assertEquals(regexpQueryexp, parser.parse("test:/[abc]?[0-9]/", null));

  }
View Full Code Here

Examples of org.apache.lucene.search.RegexpQuery

  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]/"));
  }
View Full Code Here

Examples of org.apache.lucene.search.RegexpQuery

    }
  }

  // LUCENE-3831
  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"));

View Full Code Here

Examples of org.apache.lucene.search.RegexpQuery

    assertEquals(0, mindex.search(wrappedquery), 0.00001f);
  }
   
  // LUCENE-3831
  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"));

View Full Code Here

Examples of org.apache.lucene.search.RegexpQuery

      @Override
      protected Analyzer getIndexAnalyzer(String field) {
        return analyzer;
      }
    };
    Query query = new RegexpQuery(new Term("body", "te.*"));
    TopDocs topDocs = searcher.search(query, null, 10, Sort.INDEXORDER);
    assertEquals(2, topDocs.totalHits);
    String snippets[] = highlighter.highlight("body", query, searcher, topDocs);
    assertEquals(2, snippets.length);
    assertEquals("This is a <b>test</b>.", snippets[0]);
    assertEquals("<b>Test</b> a one sentence document.", snippets[1]);
   
    // wrong field
    BooleanQuery bq = new BooleanQuery();
    bq.add(new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD);
    bq.add(new RegexpQuery(new Term("bogus", "te.*")), BooleanClause.Occur.SHOULD);
    topDocs = searcher.search(bq, null, 10, Sort.INDEXORDER);
    assertEquals(2, topDocs.totalHits);
    snippets = highlighter.highlight("body", bq, searcher, topDocs);
    assertEquals(2, snippets.length);
    assertEquals("This is a test.", snippets[0]);
View Full Code Here

Examples of org.apache.lucene.search.RegexpQuery

    WildcardQuery wcNoSuch = new WildcardQuery(new Term("field", "noSuch*"));
    SpanQuery spanWCNoSuch = new SpanMultiTermQueryWrapper<WildcardQuery>(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<RegexpQuery>(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"));
View Full Code Here

Examples of org.apache.lucene.search.RegexpQuery

    WildcardQuery wcNoSuch = new WildcardQuery(new Term("field", "noSuch*"));
    SpanQuery spanWCNoSuch = new SpanMultiTermQueryWrapper<WildcardQuery>(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<RegexpQuery>(rgxNoSuch);
    notNear = new SpanNotQuery(term, spanRgxNoSuch, 1, 1);
    assertEquals(1, searcher.search(notNear, 10).totalHits);
   
    PrefixQuery prfxNoSuch = new PrefixQuery(new Term("field", "noSuch"));
View Full Code Here

Examples of org.apache.lucene.search.RegexpQuery

    WildcardQuery wcNoSuch = new WildcardQuery(new Term("field", "noSuch*"));
    SpanQuery spanWCNoSuch = new SpanMultiTermQueryWrapper<WildcardQuery>(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<RegexpQuery>(rgxNoSuch);
    near = new SpanOrQuery(new SpanQuery[]{term, spanRgxNoSuch});
    assertEquals(1, searcher.search(near, 10).totalHits);
   
    PrefixQuery prfxNoSuch = new PrefixQuery(new Term("field", "noSuch"));
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.