Example usage for org.apache.lucene.queryparser.flexible.standard StandardQueryParser StandardQueryParser

List of usage examples for org.apache.lucene.queryparser.flexible.standard StandardQueryParser StandardQueryParser

Introduction

In this page you can find the example usage for org.apache.lucene.queryparser.flexible.standard StandardQueryParser StandardQueryParser.

Prototype

public StandardQueryParser(Analyzer analyzer) 

Source Link

Document

Constructs a StandardQueryParser object and sets an Analyzer to it.

Usage

From source file:org.xbib.elasticsearch.test.SKOSLabelFilterTest.java

License:Apache License

@Test
public void testTermQuery() throws CorruptIndexException, IOException, QueryNodeException {

    Document doc = new Document();
    doc.add(new Field("content", "I work for the united nations", TextField.TYPE_STORED));

    writer.addDocument(doc);//from  www. j  av  a 2 s.c o  m

    searcher = new IndexSearcher(DirectoryReader.open(writer, false));

    StandardQueryParser parser = new StandardQueryParser(
            new SimpleAnalyzer(SKOSAnalysisPlugin.getLuceneVersion()));

    Query query = parser.parse("united nations", "content");

    Assert.assertEquals(1, TestUtil.hitCount(searcher, query));

}

From source file:stroom.search.server.SearchExpressionQueryBuilder.java

License:Apache License

private Query getSubQuery(final Version matchVersion, final IndexField field, final String value,
        final Set<String> terms, final boolean in) {
    Query query = null;/*w  w  w.  jav a  2  s  . com*/

    // Store terms for hit highlighting.
    String highlight = value;
    highlight = NON_WORD.matcher(highlight).replaceAll(" ");
    highlight = highlight.trim();
    highlight = MULTIPLE_SPACE.matcher(highlight).replaceAll(" ");
    final String[] highlights = highlight.split(" ");
    Collections.addAll(terms, highlights);

    // If we have omitted term frequencies and positions for this field then
    // we can't expect to do a sentence match. In this case we need to
    // modify the query so that each word becomes a new term in a boolean
    // query.
    String val = value.trim();
    if (in || !AnalyzerType.KEYWORD.equals(field.getAnalyzerType())) {
        // If the field has been analysed then we need to analyse the search
        // query to create matching terms.
        final Analyzer analyzer = AnalyzerFactory.create(field.getAnalyzerType(), field.isCaseSensitive());

        if (!field.isTermPositions()) {
            val = NON_WORD_OR_WILDCARD.matcher(val).replaceAll(" ");
            val = val.trim();
            val = MULTIPLE_SPACE.matcher(val).replaceAll(" +");
            val = MULTIPLE_WILDCARD.matcher(val).replaceAll("+");
        }

        if (val.length() > 0) {
            final StandardQueryParser queryParser = new StandardQueryParser(analyzer);
            queryParser.setAllowLeadingWildcard(true);
            queryParser.setLowercaseExpandedTerms(!field.isCaseSensitive());

            try {
                query = queryParser.parse(val, field.getFieldName());
            } catch (final QueryNodeException e) {
                throw new SearchException("Unable to parse query term '" + val + "'", e);
            }
        }

    } else {
        if (val.length() > 0) {
            // As this is just indexed as a keyword we only want to search
            // for the term.
            if (!field.isCaseSensitive()) {
                val = value.toLowerCase();
            }

            final Term term = new Term(field.getFieldName(), val);
            final boolean termContainsWildcard = (val.indexOf('*') != -1) || (val.indexOf('?') != -1);
            if (termContainsWildcard) {
                query = new WildcardQuery(new Term(field.getFieldName(), val));
            } else {
                query = new TermQuery(term);
            }
        }
    }

    return query;
}

From source file:suonos.lucene.Statement.java

License:Apache License

public StandardQueryParser standardQueryParser() {
    return new StandardQueryParser(luceneIndex.getAnalyser());
}