Example usage for org.apache.lucene.index DirectoryReader open

List of usage examples for org.apache.lucene.index DirectoryReader open

Introduction

In this page you can find the example usage for org.apache.lucene.index DirectoryReader open.

Prototype

public static DirectoryReader open(final IndexCommit commit, Map<String, String> readerAttributes)
        throws IOException 

Source Link

Document

Expert: returns an IndexReader reading the index in the given IndexCommit .

Usage

From source file:api.reader.LuceneIndexReader.java

License:Open Source License

public boolean initializeIndexReader(IndexWriter writer) {
    try {/* w  ww .  ja  v a  2s . co  m*/
        READER = DirectoryReader.open(writer, true);
    } catch (IOException e) {
        READER = null;
        log.error("Failed to instantiate index reader from writer");
    }
    return isInitialized();
}

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 ww  w . jav  a  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.queryparser.flexible.standard.SKOSStandardQueryParserTest.java

License:Apache License

@Test
public void queryParserSearchWithBoosts() 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);//ww w  .  jav  a 2  s . c om

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

    SKOSStandardQueryParser parser = new SKOSStandardQueryParser(skosAnalyzer);
    parser.setBoost(SKOSType.ALT, 0.5f);

    Query query = parser.parse("\"fox jumps\"", "content");

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

    // boosts do not work in phrase queries
    Assert.assertEquals("content:\"fox (jumps hops leaps)\"", query.toString());
    Assert.assertEquals("org.apache.lucene.search.MultiPhraseQuery", query.getClass().getName());

    query = parser.parse("fox jumps", "content");

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

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

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

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

}

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

License:Apache License

@Test
public void termQuerySearch() throws IOException {
    Document doc = new Document();
    doc.add(new Field("content", "The quick brown fox jumps over the lazy dog", TextField.TYPE_STORED));
    writer.addDocument(doc);//  www  . j a  v  a 2 s  .  c  om
    searcher = new IndexSearcher(DirectoryReader.open(writer, false));
    TermQuery tq = new TermQuery(new Term("content", "hops"));
    assertEquals(1, searcher.search(tq, 1).totalHits);
}

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

License:Apache License

@Test
public void phraseQuerySearch() throws IOException {
    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 www. ja v a  2s.  com*/
    searcher = new IndexSearcher(DirectoryReader.open(writer, false));
    PhraseQuery.Builder builder = new PhraseQuery.Builder();
    builder.add(new Term("content", "fox")).add(new Term("content", "hops"));
    assertEquals(1, searcher.search(builder.build(), 1).totalHits);
}

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);/*from www. ja va2s .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 a2  s  . c  o m*/
    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);/*from   w w  w.  j  av  a 2  s.  co 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:at.ac.univie.mminf.luceneSKOS.test.SKOSStandardQueryParserTest.java

License:Apache License

@Test
public void queryParserSearchWithBoosts() 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  . ja va 2  s  .  c om*/

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

    SKOSStandardQueryParser parser = new SKOSStandardQueryParser(skosAnalyzer);
    parser.setBoost(SKOSType.ALT, 0.5f);

    Query query = parser.parse("\"fox jumps\"", "content");

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

    // boosts do not work in phrase queries
    assertEquals("content:\"fox (jumps hops leaps)\"", query.toString());
    assertEquals("org.apache.lucene.search.MultiPhraseQuery", query.getClass().getName());

    query = parser.parse("fox jumps", "content");

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

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

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

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

}

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

License:Apache License

@Test
public void singleUriExpansionWithStoredField() throws IOException {
    Document doc = new Document();
    doc.add(new Field("subject", "http://example.com/concept/1", TextField.TYPE_STORED));
    writer.addDocument(doc);//  ww w  .jav  a 2  s  .  c o m
    searcher = new IndexSearcher(DirectoryReader.open(writer, false));
    Query query = new TermQuery(new Term("subject", "leaps"));
    TopDocs results = searcher.search(query, 10);
    assertEquals(1, results.totalHits);

    Document indexDoc = searcher.doc(results.scoreDocs[0].doc);
    String[] fieldValues = indexDoc.getValues("subject");
    assertEquals(1, fieldValues.length);
    assertEquals(fieldValues[0], "http://example.com/concept/1");
}