Example usage for org.apache.lucene.index IndexReader document

List of usage examples for org.apache.lucene.index IndexReader document

Introduction

In this page you can find the example usage for org.apache.lucene.index IndexReader document.

Prototype




public final Document document(int docID) throws IOException 

Source Link

Document

Returns the stored fields of the nth Document in this index.

Usage

From source file:org.exist.indexing.lucene.LuceneIndexWorker.java

License:Open Source License

public String getFieldContent(int docId, String field) throws IOException {
    BytesRef bytes = new BytesRef(NumericUtils.BUF_SIZE_INT);
    NumericUtils.intToPrefixCoded(docId, 0, bytes);
    Term dt = new Term(FIELD_DOC_ID, bytes);

    IndexReader reader = null;
    try {/*from   w w  w.j ava 2  s .  c  o m*/
        reader = index.getReader();

        List<AtomicReaderContext> leaves = reader.leaves();
        for (AtomicReaderContext context : leaves) {
            DocsEnum docs = context.reader().termDocsEnum(dt);
            if (docs != null && docs.nextDoc() != DocsEnum.NO_MORE_DOCS) {
                Document doc = reader.document(docs.docID());
                return doc.get(field);
            }
        }
    } finally {
        index.releaseReader(reader);
    }
    return null;
}

From source file:org.exoplatform.services.jcr.impl.core.query.lucene.AbstractExcerpt.java

License:Apache License

/**
 * {@inheritDoc}//from w  w  w. ja va  2s.c  o  m
 */
public String getExcerpt(String id, int maxFragments, int maxFragmentSize) throws IOException {
    IndexReader reader = index.getIndexReader();
    try {
        checkRewritten(reader);
        Term idTerm = new Term(FieldNames.UUID, id);
        TermDocs tDocs = reader.termDocs(idTerm);
        int docNumber;
        Document doc;
        try {
            if (tDocs.next()) {
                docNumber = tDocs.doc();
                doc = reader.document(docNumber);
            } else {
                // node not found in index
                return null;
            }
        } finally {
            tDocs.close();
        }
        Fieldable[] fields = doc.getFieldables(FieldNames.FULLTEXT);

        if (fields == null) {
            LOG.debug("Fulltext field not stored, using {}", SimpleExcerptProvider.class.getName());
            SimpleExcerptProvider exProvider = new SimpleExcerptProvider();
            exProvider.init(query, index);
            return exProvider.getExcerpt(id, maxFragments, maxFragmentSize);
        }
        StringBuffer text = new StringBuffer();
        String separator = "";
        for (int i = 0; i < fields.length; i++) {
            if (fields[i].stringValue().length() > 0) {
                text.append(separator);
                text.append(fields[i].stringValue());
                separator = " ";
            }

        }
        TermFreqVector tfv = reader.getTermFreqVector(docNumber, FieldNames.FULLTEXT);
        if (tfv instanceof TermPositionVector) {
            return createExcerpt((TermPositionVector) tfv, text.toString(), maxFragments, maxFragmentSize);
        } else {
            LOG.debug("No TermPositionVector on Fulltext field.");
            return null;
        }
    } finally {
        Util.closeOrRelease(reader);
    }
}

From source file:org.frontcache.cache.impl.LuceneIndexManager.java

License:Apache License

/**
 * /*from w  w w. j av a  2s  .  co  m*/
 * @return
 */
public List<String> getKeys() {

    IndexWriter iWriter = null;
    try {
        iWriter = getIndexWriter();
        if (iWriter == null) {
            return Collections.emptyList();
        }
    } catch (Exception e1) {
        logger.debug("Error during getting indexWriter. " + e1.getMessage());
        return Collections.emptyList();
    }

    List<String> keys = new ArrayList<String>();

    IndexReader reader = null;
    try {
        reader = DirectoryReader.open(iWriter);

        for (int i = 0; i < reader.numDocs(); i++) {
            Document doc = reader.document(i);
            if (null != doc) {
                if (null != doc.get(URL_FIELD))
                    keys.add(doc.get(URL_FIELD));
                else
                    logger.error("URL is null for doc (probably corrupted after/during index time) " + doc);
            }

        }
    } catch (Exception e) {
        logger.error("Error during loading urls/keys from index", e);
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return keys;
}

From source file:org.hibernate.search.test.compression.CompressionTest.java

License:Open Source License

/**
 * Verifies the fields are really stored in compressed format
 *
 * @throws Exception in case the test fails
 *//*  ww w . j a v a 2  s .  c  om*/
public void testFieldWasCompressed() throws Exception {
    IndexReader indexReader = getSearchFactory().openIndexReader(LargeDocument.class);
    try {
        IndexSearcher searcher = new IndexSearcher(indexReader);
        TopDocs topDocs = searcher.search(new MatchAllDocsQuery(), null, 10);
        Assert.assertEquals(1, topDocs.totalHits);

        ScoreDoc doc = topDocs.scoreDocs[0];
        Document document = indexReader.document(doc.doc);
        {
            Field[] fields = document.getFields("title");
            assertEquals(1, fields.length);
            assertTrue(fields[0].isIndexed());
            assertTrue(fields[0].isStored());
            assertFalse(isCompressed(fields[0]));
            assertEquals("Hibernate in Action, third edition", fields[0].stringValue());
        }
        {
            Field[] fields = document.getFields("abstract");
            assertEquals(1, fields.length);
            assertTrue(isCompressed(fields[0]));
            assertEquals("<b>JPA2 with Hibernate</b>", restoreValue(fields[0]));
        }
        {
            Field[] fields = document.getFields("text");
            assertEquals(1, fields.length);
            assertTrue(isCompressed(fields[0]));
            assertEquals("This is a placeholder for the new text that you should write",
                    restoreValue(fields[0]));
        }
    } finally {
        getSearchFactory().closeIndexReader(indexReader);
    }
}

From source file:org.hibernate.search.test.directoryProvider.FSDirectoryTest.java

License:LGPL

/**
 * Project a field as a String from a Lucene Document matching the provided term.
 * The method asserts that one match is found, and no more.
 *//*from   w ww.  j  a v  a  2 s . c o m*/
private String projectSingleField(IndexReader reader, String fieldName, Term term) throws IOException {
    String projection = null;
    for (LeafReaderContext leaf : reader.leaves()) {
        final LeafReader atomicReader = leaf.reader();
        final DocsEnum termDocsEnum = atomicReader.termDocsEnum(term);
        while (termDocsEnum.nextDoc() != DocsEnum.NO_MORE_DOCS) {
            final int docID = termDocsEnum.docID();
            org.apache.lucene.document.Document document = reader.document(docID);
            String value = document.get(fieldName);
            Assert.assertNull(
                    "duplicate matches found! This method assumes a single document will match the Term.",
                    projection);
            projection = value;
        }
    }
    Assert.assertNotNull(projection);
    return projection;
}

From source file:org.hibernate.search.test.FSDirectoryTest.java

License:Open Source License

public void testEventIntegration() throws Exception {

    Session s = getSessions().openSession();
    s.getTransaction().begin();/*from  ww  w.  j a  v a2s  . c o m*/
    s.persist(
            new Document("Hibernate in Action", "Object/relational mapping with Hibernate", "blah blah blah"));
    s.getTransaction().commit();
    s.close();

    Directory dir = FSDirectory.open(new File(getBaseIndexDir(), "Documents"));
    try {
        IndexReader reader = IndexReader.open(dir, true);
        try {
            int num = reader.numDocs();
            assertEquals(1, num);
            TermDocs docs = reader.termDocs(new Term("Abstract", "hibernate"));
            assertTrue(docs.next());
            org.apache.lucene.document.Document doc = reader.document(docs.doc());
            assertFalse(docs.next());
            docs = reader.termDocs(new Term("title", "action"));
            assertTrue(docs.next());
            doc = reader.document(docs.doc());
            assertFalse(docs.next());
            assertEquals("1", doc.getField("id").stringValue());
        } finally {
            reader.close();
        }

        s = getSessions().openSession();
        s.getTransaction().begin();
        Document entity = (Document) s.get(Document.class, Long.valueOf(1));
        entity.setSummary("Object/relational mapping with EJB3");
        s.persist(new Document("Seam in Action", "", "blah blah blah blah"));
        s.getTransaction().commit();
        s.close();

        reader = IndexReader.open(dir, true);
        try {
            int num = reader.numDocs();
            assertEquals(2, num);
            TermDocs docs = reader.termDocs(new Term("Abstract", "ejb"));
            assertTrue(docs.next());
            org.apache.lucene.document.Document doc = reader.document(docs.doc());
            assertFalse(docs.next());
        } finally {
            reader.close();
        }

        s = getSessions().openSession();
        s.getTransaction().begin();
        s.delete(entity);
        s.getTransaction().commit();
        s.close();

        reader = IndexReader.open(dir, true);
        try {
            int num = reader.numDocs();
            assertEquals(1, num);
            TermDocs docs = reader.termDocs(new Term("title", "seam"));
            assertTrue(docs.next());
            org.apache.lucene.document.Document doc = reader.document(docs.doc());
            assertFalse(docs.next());
            assertEquals("2", doc.getField("id").stringValue());
        } finally {
            reader.close();
        }
    } finally {
        dir.close();
    }

    s = getSessions().openSession();
    s.getTransaction().begin();
    s.delete(s.createCriteria(Document.class).uniqueResult());
    s.getTransaction().commit();
    s.close();
}

From source file:org.hibernate.search.test.shards.ShardsTest.java

License:Open Source License

public void testInternalSharding() throws Exception {
    Session s = openSession();/*from   w ww .  ja v a  2  s.  co m*/
    Transaction tx = s.beginTransaction();
    Animal a = new Animal();
    a.setId(1);
    a.setName("Elephant");
    s.persist(a);
    a = new Animal();
    a.setId(2);
    a.setName("Bear");
    s.persist(a);
    tx.commit();

    s.clear();

    FSDirectory animal00Directory = FSDirectory.open(new File(getBaseIndexDir(), "Animal00"));
    try {
        IndexReader reader = IndexReader.open(animal00Directory);
        try {
            int num = reader.numDocs();
            assertEquals(1, num);
        } finally {
            reader.close();
        }
    } finally {
        animal00Directory.close();
    }

    FSDirectory animal01Directory = FSDirectory.open(new File(getBaseIndexDir(), "Animal.1"));
    try {
        IndexReader reader = IndexReader.open(animal01Directory);
        try {
            int num = reader.numDocs();
            assertEquals(1, num);
        } finally {
            reader.close();
        }
    } finally {
        animal01Directory.close();
    }

    tx = s.beginTransaction();
    a = (Animal) s.get(Animal.class, 1);
    a.setName("Mouse");
    tx.commit();

    s.clear();

    animal01Directory = FSDirectory.open(new File(getBaseIndexDir(), "Animal.1"));
    try {
        IndexReader reader = IndexReader.open(animal01Directory);
        try {
            int num = reader.numDocs();
            assertEquals(1, num);
            TermDocs docs = reader.termDocs(new Term("name", "mouse"));
            assertTrue(docs.next());
            org.apache.lucene.document.Document doc = reader.document(docs.doc());
            assertFalse(docs.next());
        } finally {
            reader.close();
        }
    } finally {
        animal01Directory.close();
    }

    tx = s.beginTransaction();
    FullTextSession fts = Search.getFullTextSession(s);
    QueryParser parser = new QueryParser(getTargetLuceneVersion(), "id", SearchTestCase.stopAnalyzer);

    List results = fts.createFullTextQuery(parser.parse("name:mouse OR name:bear")).list();
    assertEquals("Either double insert, single update, or query fails with shards", 2, results.size());
    for (Object o : results) {
        s.delete(o);
    }
    tx.commit();
    s.close();
}

From source file:org.hyperimage.service.search.HIIndexer.java

License:Open Source License

public Vector<Long> simpleSearch(String text, HIProject project, String language) {
    Vector<Long> results = new Vector<Long>();

    HIHitCollector collector = new HIHitCollector();
    QueryParser parser = new QueryParser("all", new StandardAnalyzer());
    try {/*ww  w.j ava2 s. c o m*/

        prepDir(project);
        Query query = parser.parse("all." + language + ":\"" + text + "\"");
        IndexSearcher searcher = new IndexSearcher(getDir(project));
        searcher.search(query, collector);

        IndexReader reader = IndexReader.open(getDir(project));
        for (int doc : collector.getDocs()) {
            long baseID = Long.parseLong(reader.document(doc).get("id"));
            if (!results.contains(baseID))
                results.addElement(baseID);
        }
        searcher.close();
        reader.close();
    } catch (ParseException e) {
        e.printStackTrace();
    } catch (CorruptIndexException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return results;
}

From source file:org.hyperimage.service.search.HIIndexer.java

License:Open Source License

public Vector<Long> fieldSearch(List<String> fields, List<String> contents, HIProject project,
        String language) {//from   ww w . ja  va  2 s  . c  o  m
    Vector<Long> results = new Vector<Long>();

    HIHitCollector collector = new HIHitCollector();
    QueryParser parser = new QueryParser("all", new StandardAnalyzer());

    String queryString = "";
    for (int i = 0; i < fields.size(); i++) {
        // sanitize user input
        String field = fields.get(i);
        String content = contents.get(i);
        field = field.replaceAll("[^a-zA-Z0-9.]", "");
        content = content.replaceAll("\"", "\\\\\"");
        // convert displayable ids to DB id for id search
        if (field.equalsIgnoreCase("id"))
            content = content.replaceAll("[^0-9]", "");

        if (!field.equalsIgnoreCase("project")) {
            if (queryString.length() > 0)
                queryString = queryString + " AND ";
            if (field.equalsIgnoreCase("id")) // dont add language to id search
                queryString = queryString + field + ":\"" + content + "\"";
            else
                queryString = queryString + field + "." + language + ":\"" + content + "\"";
        }
    }
    queryString = queryString.trim();
    if (queryString.length() == 0)
        return results;

    try {
        prepDir(project);
        Query query = parser.parse(queryString);
        IndexSearcher searcher = new IndexSearcher(getDir(project));
        searcher.search(query, collector);

        IndexReader reader = IndexReader.open(getDir(project));
        for (int doc : collector.getDocs()) {
            long baseID = Long.parseLong(reader.document(doc).get("id"));
            if (!results.contains(baseID))
                results.addElement(baseID);
        }
        searcher.close();
        reader.close();
    } catch (ParseException e) {
        e.printStackTrace();
    } catch (CorruptIndexException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return results;
}

From source file:org.hyperimage.service.search.HIIndexer.java

License:Open Source License

private Document getOrCreateDocument(HIBase base, HIProject project) {
    Document baseDoc = null;/*from ww  w .  j  a v  a  2  s .  c  o  m*/

    try {
        prepDir(project);
        IndexReader reader = IndexReader.open(getDir(project));

        TermDocs docs = reader.termDocs(new Term("id", Long.toString(base.getId())));
        if (docs.next())
            baseDoc = reader.document(docs.doc());
        else
            baseDoc = new Document();

        reader.close();

    } catch (CorruptIndexException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return baseDoc;
}