Example usage for org.apache.lucene.search RegexpQuery RegexpQuery

List of usage examples for org.apache.lucene.search RegexpQuery RegexpQuery

Introduction

In this page you can find the example usage for org.apache.lucene.search RegexpQuery RegexpQuery.

Prototype

public RegexpQuery(Term term) 

Source Link

Document

Constructs a query for terms matching term.

Usage

From source file:com.github.s4ke.moar.lucene.query.test.MoarQueryPerfTest.java

License:Open Source License

@Test
public void testComparison() throws IOException {
    this.setupComparisonData();

    try (IndexReader ir = DirectoryReader.open(d)) {
        IndexSearcher is = new IndexSearcher(ir);
        Perf perf = new Perf(true);

        for (int i = 0; i < 1000; ++i) {
            String wordOfChoice = WORDS.get(this.random.nextInt(WORDS.size()));
            wordOfChoice = wordOfChoice.substring(0, this.random.nextInt(wordOfChoice.length() - 1) + 1);
            wordOfChoice += ".*";
            System.out.println(wordOfChoice);
            {//from ww  w .j  a  v a2s . c om
                perf.pre();
                MoaPattern pattern = MoaPattern.compile(wordOfChoice);
                MoarQuery tq = new MoarQuery("tag", pattern);

                TopDocs td = is.search(tq, 10);
                System.out.println(td.totalHits + " moar query hits");
                perf.after();
                perf.report("searching with moar");
            }

            {
                RegexpQuery regexpQuery = new RegexpQuery(new Term("tag", wordOfChoice));
                perf.pre();
                TopDocs td = is.search(regexpQuery, 10);
                System.out.println(td.totalHits + " regexp query hits");
                perf.after();
                perf.report("searching with regexp");
            }
        }
    }
}

From source file:com.google.gerrit.lucene.QueryBuilder.java

License:Apache License

private Query regexQuery(IndexPredicate<ChangeData> p) {
    String re = p.getValue();/*from   www  .jav  a  2 s .  c  o m*/
    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));
}

From source file:com.o19s.solr.swan.nodes.SwanTermNode.java

License:Apache License

public Query getQuery(String field) {
    if (!wildcard)
        return schema.getField(field).getType().getFieldQuery(_parser, schema.getField(field), _term);

    if (_wildcardTerm != null)
        return new RegexpQuery(new Term(field, _wildcardTerm));

    String term = analyzeIfMultitermTermText(field, _term, schema.getFieldType(field));
    _wildcardTerm = translateWildcard(term);
    return new RegexpQuery(new Term(field, _wildcardTerm));
}

From source file:com.stratio.cassandra.index.query.RegexpCondition.java

License:Apache License

/** {@inheritDoc} */
@Override//w w  w  .  j a va  2  s .c  om
public Query query(Schema schema) {

    if (field == null || field.trim().isEmpty()) {
        throw new IllegalArgumentException("Field name required");
    }
    if (value == null || value.trim().isEmpty()) {
        throw new IllegalArgumentException("Field value required");
    }

    ColumnMapper<?> columnMapper = schema.getMapper(field);
    if (columnMapper == null) {
        throw new IllegalArgumentException("Not found mapper for field " + field);
    }
    Class<?> clazz = columnMapper.baseClass();
    Query query;
    if (clazz == String.class) {
        Term term = new Term(field, value);
        query = new RegexpQuery(term);
    } else {
        String message = String.format("Regexp queries are not supported by %s mapper", clazz.getSimpleName());
        throw new UnsupportedOperationException(message);
    }
    query.setBoost(boost);
    return query;
}

From source file:com.stratio.cassandra.lucene.search.condition.RegexpCondition.java

License:Apache License

/** {@inheritDoc} */
@Override/*from ww  w  .ja va2s.c om*/
public Query query(Schema schema) {
    SingleColumnMapper<?> columnMapper = getMapper(schema, field);
    Class<?> clazz = columnMapper.baseClass();
    Query query;
    if (clazz == String.class) {
        Term term = new Term(field, value);
        query = new RegexpQuery(term);
    } else {
        String message = String.format("Regexp queries are not supported by %s mapper", clazz.getSimpleName());
        throw new UnsupportedOperationException(message);
    }
    query.setBoost(boost);
    return query;
}

From source file:com.tuplejump.stargate.lucene.query.RegexpCondition.java

License:Apache License

/**
 * {@inheritDoc}/*from  w  w w  .  j  a  va  2  s.  c o m*/
 */
@Override
public Query query(Options schema) {

    if (field == null || field.trim().isEmpty()) {
        throw new IllegalArgumentException("Field name required");
    }
    if (value == null || value.trim().isEmpty()) {
        throw new IllegalArgumentException("Field value required");
    }

    Query query;
    Properties properties = schema.getProperties(field);
    Properties.Type fieldType = properties != null ? properties.getType() : Properties.Type.text;
    if (fieldType.isCharSeq()) {
        Term term = new Term(field, value);
        query = new RegexpQuery(term);
    } else {
        String message = String.format("Regexp queries are not supported by %s mapper", fieldType);
        throw new UnsupportedOperationException(message);
    }
    query.setBoost(boost);
    return query;
}

From source file:de.unihildesheim.iw.lucene.query.IPCClassQuery.java

License:Open Source License

/**
 * Get a Query object for matching all IPC-codes that are like the one passed
 * in. If a partial IPC-code (like {@code H05}) is given, all IPC-codes
 * matching {@code H05.*} will be matched by the query. If a full code (like
 * {@code H05K0005-04}) is given, the query will only search for IPC-codes
 * matching exactly this code.//from w  w  w .j av a 2  s.co  m
 *
 * @param record IPC record to search for
 * @param separator Separator for main- and sub-group
 * @return Query to search for IPC-codes
 */
public static Query get(@NotNull final IPCRecord record, final char separator) {
    final Query q = new RegexpQuery(new Term(LUCENE_CONF.FLD_IPC, record.toRegExpString(separator)));

    if (LOG.isDebugEnabled()) {
        LOG.debug("IPCq: {}", q);
    }

    return q;
}

From source file:de.uni_koeln.spinfo.maalr.lucene.config.interpreter.modifier.InfixQueryBuilder.java

License:Apache License

@Override
public List<Query> transform(String value) {
    value = TokenizerHelper.tokenizeString(analyzer, value);
    TermQuery q1 = new TermQuery(new Term(getFieldName("first"), value));
    q1.setBoost(1000f);/* w  w  w.  j  a  v a 2  s .c o  m*/
    Query q2 = new RegexpQuery(new Term(getFieldName("second"), ".*" + value + ".*"));
    TermQuery q3 = new TermQuery(new Term(getFieldName("first"), ".*" + value + ".*"));
    return Arrays.asList(q1, q2, q3);
}

From source file:de.uni_koeln.spinfo.maalr.lucene.config.interpreter.modifier.SuffixQueryBuilder.java

License:Apache License

@Override
public List<Query> transform(String value) {
    value = TokenizerHelper.tokenizeString(analyzer, value);
    TermQuery q1 = new TermQuery(new Term(getFieldName("first"), value));
    q1.setBoost(1000f);/* w  w  w.j  a  va  2s  . c o m*/
    Query q2 = new RegexpQuery(new Term(getFieldName("second"), ".*" + value));
    TermQuery q3 = new TermQuery(new Term(getFieldName("first"), ".*" + value));
    return Arrays.asList(q1, q2, q3);
}

From source file:nl.inl.blacklab.search.lucene.TextPatternTranslatorSpanQuery.java

License:Apache License

@Override
public SpanQuery regex(QueryExecutionContext context, String value) {
    String valueNoStartEndMatch = value.replaceAll("\\^|\\$", "");
    return new BLSpanMultiTermQueryWrapper<RegexpQuery>(
            new RegexpQuery(new Term(context.luceneField(), context.optDesensitize(valueNoStartEndMatch))));
}