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:aos.lucene.tools.FlexibleQueryParserTest.java

License:Apache License

public void testSimple() throws Exception {
    Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_46);
    StandardQueryParser parser = new StandardQueryParser(analyzer);
    Query q = null;//from w  w  w . jav a 2  s.  co m
    try {
        q = parser.parse("(agile OR extreme) AND methodology", "subject");
    } catch (QueryNodeException exc) {
        // TODO: handle exc
    }
    LOGGER.info("parsed " + q);
}

From source file:at.ac.univie.mminf.luceneSKOS.queryparser.flexible.standard.SKOSStandardQueryParserTest.java

License:Apache License

@Test
public void queryParserSearch() throws IOException, QueryNodeException {

    Document doc = new Document();
    doc.add(new Field("content", "The quick brown fox jumps over the lazy dog", TextField.TYPE_STORED));

    writer.addDocument(doc);//from w  ww.  j a va 2s .com

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

    Query query = new SKOSStandardQueryParser(skosAnalyzer).parse("\"fox jumps\"", "content");

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

    Assert.assertEquals("content:\"fox (jumps hops leaps)\"", query.toString());
    Assert.assertEquals("org.apache.lucene.search.MultiPhraseQuery", query.getClass().getName());

    query = new StandardQueryParser(new StandardAnalyzer(matchVersion)).parse("\"fox jumps\"", "content");
    Assert.assertEquals(1, TestUtil.hitCount(searcher, query));

    Assert.assertEquals("content:\"fox jumps\"", query.toString());
    Assert.assertEquals("org.apache.lucene.search.PhraseQuery", query.getClass().getName());

}

From source file:at.ac.univie.mminf.luceneSKOS.test.SKOSLabelFilterTest.java

License:Apache License

@Test
public void queryParserSearch() throws IOException, QueryNodeException {
    Document doc = new Document();
    doc.add(new Field("content", "The quick brown fox jumps over the lazy dog", TextField.TYPE_STORED));
    writer.addDocument(doc);/* w  ww  . j  a  va  2s.  com*/
    searcher = new IndexSearcher(DirectoryReader.open(writer, false));
    Query query = new StandardQueryParser(skosAnalyzer).parse("\"fox jumps\"", "content");
    assertEquals(1, searcher.search(query, 1).totalHits);
    assertEquals("content:\"fox (jumps hops leaps)\"", query.toString());
    assertEquals("org.apache.lucene.search.MultiPhraseQuery", query.getClass().getName());
    query = new StandardQueryParser(new StandardAnalyzer()).parse("\"fox jumps\"", "content");
    assertEquals(1, searcher.search(query, 1).totalHits);
    assertEquals("content:\"fox jumps\"", query.toString());
    assertEquals("org.apache.lucene.search.PhraseQuery", query.getClass().getName());
}

From source file:at.ac.univie.mminf.luceneSKOS.test.SKOSLabelFilterTest.java

License:Apache License

@Test
public void testTermQuery() throws IOException, QueryNodeException {
    Document doc = new Document();
    doc.add(new Field("content", "I work for the united nations", TextField.TYPE_STORED));
    writer.addDocument(doc);/*  w  ww  .ja v  a  2 s.  c om*/
    searcher = new IndexSearcher(DirectoryReader.open(writer, false));
    StandardQueryParser parser = new StandardQueryParser(new SimpleAnalyzer());
    Query query = parser.parse("united nations", "content");
    assertEquals(1, searcher.search(query, 1).totalHits);
}

From source file:at.ac.univie.mminf.luceneSKOS.test.SKOSStandardQueryParserTest.java

License:Apache License

@Test
public void queryParserSearch() throws IOException, QueryNodeException {

    Document doc = new Document();
    doc.add(new Field("content", "The quick brown fox jumps over the lazy dog", TextField.TYPE_STORED));

    writer.addDocument(doc);//  w  w w .j  ava 2  s.  c  o  m

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

    Query query = new SKOSStandardQueryParser(skosAnalyzer).parse("\"fox jumps\"", "content");

    assertEquals(1, searcher.search(query, 1).totalHits);

    assertEquals("content:\"fox (jumps hops leaps)\"", query.toString());
    assertEquals("org.apache.lucene.search.MultiPhraseQuery", query.getClass().getName());

    query = new StandardQueryParser(new StandardAnalyzer()).parse("\"fox jumps\"", "content");
    assertEquals(1, searcher.search(query, 1).totalHits);

    assertEquals("content:\"fox jumps\"", query.toString());
    assertEquals("org.apache.lucene.search.PhraseQuery", query.getClass().getName());

}

From source file:com.foundationdb.server.service.text.FullTextIndexInfo.java

License:Open Source License

public StandardQueryParser getParser() {
    StandardQueryParser parser;/*from  www  .ja v  a 2  s.c  om*/
    synchronized (shared) {
        parser = shared.getParser();
        if (parser == null) {
            parser = new StandardQueryParser(getAnalyzer());
        }
        shared.setParser(parser);
    }
    return parser;
}

From source file:com.mathworks.xzheng.tools.FlexibleQueryParserTest.java

License:Apache License

public void testSimple() throws Exception {
    Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_46);
    StandardQueryParser parser = new StandardQueryParser(analyzer);
    Query q = null;//from   ww w.  j  a  va  2 s  .  com
    try {
        q = parser.parse("(agile OR extreme) AND methodology", "subject");
    } catch (QueryNodeException exc) {
        // TODO: handle exc
    }
    System.out.println("parsed " + q);
}

From source file:com.netcrest.pado.index.provider.lucene.LuceneSearch.java

License:Open Source License

/**
 * Returns parser./*from www  .java 2  s  .co  m*/
 */
public StandardQueryParser createParser() {
    StandardQueryParser parser = new StandardQueryParser(new StandardAnalyzer(LUCENE_VERSION));
    return parser;
}

From source file:com.netcrest.pado.index.provider.lucene.LuceneSearch.java

License:Open Source License

protected Set<Object> getIdentityKeySet(String queryString, Directory dir) {
    Set<Object> identityKeySet = new HashSet<Object>();
    DirectoryReader reader;/*from   w  w w. j a  v  a 2s  .  c o m*/
    try {
        reader = DirectoryReader.open(dir);
    } catch (CorruptIndexException e1) {
        e1.printStackTrace();
        throw new RuntimeException(e1);
    } catch (IOException e1) {
        e1.printStackTrace();
        throw new RuntimeException(e1);
    }

    Query query;
    try {
        StandardQueryParser parser = new StandardQueryParser(new StandardAnalyzer(LUCENE_VERSION));
        query = parser.parse(queryString.replaceAll("\\-", "\\\\-"), "__doc");
    } catch (Exception ex) {
        // Lucene bug. Unable to serialize exception. Log the message and
        // throw a new exception with the string message.
        ex.printStackTrace();
        throw new PadoException(ex.getMessage());
    }
    IndexSearcher searcher = new IndexSearcher(reader);
    TopDocs results;
    try {
        results = searcher.search(query, null, Integer.MAX_VALUE);

        for (ScoreDoc hit : results.scoreDocs) {
            Document doc;
            try {
                doc = searcher.doc(hit.doc);
            } catch (CorruptIndexException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
            //            IndexableField field = doc.getField("IdentityKey");
            //            if (field == null) {
            //               continue;
            //            }
            //            Object identityKey = field.stringValue();
            //            if (identityKey == null) {
            //               identityKey = field.numericValue();
            //            }
            //            if (identityKey == null) {
            //               BytesRef br = field.binaryValue();
            //               if (br != null) {
            //                  byte[] blob = br.bytes;
            //                  try {
            //                     identityKey = BlobHelper.deserializeBlob(blob);
            //                     identityKeySet.add(identityKey);
            //                  } catch (Exception ex) {
            //                     Logger.warning("Identity key deserialization error", ex);
            //                  }
            //               } else {
            //                  identityKey = field.toString();
            //               }
            //            }
            LuceneField luceneField = new LuceneField();
            ITemporalKey temporalKey = luceneField.getTemporalKey(doc);
            if (temporalKey != null) {
                identityKeySet.add(temporalKey.getIdentityKey());
            }
        }
    } catch (IOException e1) {
        e1.printStackTrace();
        throw new RuntimeException(e1);
    }
    return identityKeySet;
}

From source file:com.netcrest.pado.index.provider.lucene.LuceneSearch.java

License:Open Source License

protected Set<ITemporalKey> getTemporalKeySet(String queryString, Directory dir) {
    Set<ITemporalKey> temporalKeySet = new HashSet<ITemporalKey>();
    DirectoryReader reader;/*from www.jav a  2  s .co  m*/
    try {
        reader = DirectoryReader.open(dir);
    } catch (CorruptIndexException e1) {
        e1.printStackTrace();
        throw new RuntimeException(e1);
    } catch (IOException e1) {
        e1.printStackTrace();
        throw new RuntimeException(e1);
    }
    Query query;
    try {
        StandardQueryParser parser = new StandardQueryParser(new StandardAnalyzer(LUCENE_VERSION));
        query = parser.parse(queryString.replaceAll("\\-", "\\\\-"), "__doc");
    } catch (Exception ex) {
        // Lucene 4.7 bug, internal message not serializable
        // Send message instead of nesting the cause.
        throw new RuntimeException(ex.getMessage());
    }

    IndexSearcher searcher = new IndexSearcher(reader);
    TopDocs results;
    try {
        results = searcher.search(query, null, Integer.MAX_VALUE);

        for (ScoreDoc hit : results.scoreDocs) {
            Document doc;
            try {
                doc = searcher.doc(hit.doc);
            } catch (CorruptIndexException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
            LuceneField luceneField = new LuceneField();
            ITemporalKey temporalKey = luceneField.getTemporalKey(doc);
            if (temporalKey != null) {
                temporalKeySet.add(temporalKey);
            }
        }
    } catch (IOException e1) {
        e1.printStackTrace();
        throw new RuntimeException(e1);
    }
    return temporalKeySet;
}