Example usage for org.apache.lucene.index IndexWriter addDocument

List of usage examples for org.apache.lucene.index IndexWriter addDocument

Introduction

In this page you can find the example usage for org.apache.lucene.index IndexWriter addDocument.

Prototype

public long addDocument(Iterable<? extends IndexableField> doc) throws IOException 

Source Link

Document

Adds a document to this index.

Usage

From source file:de.elbe5.cms.search.SearchBean.java

License:Open Source License

protected void indexSites(IndexWriter writer) throws Exception {
    Connection con = getConnection();
    PreparedStatement pst = null;
    try {//  ww  w.  ja v  a2s . com
        pst = con.prepareStatement(INDEX_SITES_SQL);
        ResultSet rs = pst.executeQuery();
        int count = 0;
        while (rs.next()) {
            SiteSearchData data = new SiteSearchData();
            getSearchData(data, rs);
            data.setDoc();
            writer.addDocument(data.getDoc());
            count++;
            if ((count % 100) == 0) {
                writer.commit();
            }
        }
        rs.close();
        writer.commit();
        Log.log("finished indexing " + count + " sites");
    } catch (SQLException se) {
        se.printStackTrace();
    } finally {
        closeStatement(pst);
        closeConnection(con);
    }
}

From source file:de.elbe5.cms.search.SearchBean.java

License:Open Source License

protected void indexSite(IndexWriter writer, int id) throws Exception {
    Connection con = getConnection();
    PreparedStatement pst = null;
    try {/*from w  w  w .j a  va2 s  .c  o  m*/
        pst = con.prepareStatement(INDEX_NODE_SQL);
        pst.setInt(1, id);
        ResultSet rs = pst.executeQuery();
        if (rs.next()) {
            SiteSearchData data = new SiteSearchData();
            getSearchData(data, rs);
            data.setDoc();
            writer.addDocument(data.getDoc());
        }
        rs.close();
        writer.commit();
        Log.log("finished indexing site");
    } catch (SQLException se) {
        se.printStackTrace();
    } finally {
        closeStatement(pst);
        closeConnection(con);
    }
}

From source file:de.elbe5.cms.search.SearchBean.java

License:Open Source License

protected void indexPages(IndexWriter writer) throws Exception {
    Connection con = getConnection();
    PreparedStatement pst = null;
    try {//from   w  w  w  . ja v  a2 s.co m
        pst = con.prepareStatement(INDEX_PAGES_SQL);
        ResultSet rs = pst.executeQuery();
        int count = 0;
        while (rs.next()) {
            PageSearchData data = new PageSearchData();
            getSearchData(data, rs);
            data.setDoc();
            writer.addDocument(data.getDoc());
            count++;
            if ((count % 100) == 0) {
                writer.commit();
            }
        }
        rs.close();
        writer.commit();
        Log.log("finished indexing " + count + " pages");
    } catch (SQLException se) {
        se.printStackTrace();
    } finally {
        closeStatement(pst);
        closeConnection(con);
    }
}

From source file:de.elbe5.cms.search.SearchBean.java

License:Open Source License

protected void indexPage(IndexWriter writer, int id) throws Exception {
    Connection con = getConnection();
    PreparedStatement pst = null;
    try {/*from   w ww  .  j  a v  a2s . c om*/
        pst = con.prepareStatement(INDEX_NODE_SQL);
        pst.setInt(1, id);
        ResultSet rs = pst.executeQuery();
        if (rs.next()) {
            PageSearchData data = new PageSearchData();
            getSearchData(data, rs);
            //todo read parts
            data.setDoc();
            writer.addDocument(data.getDoc());
        }
        rs.close();
        writer.commit();
        Log.log("finished indexing page");
    } catch (SQLException se) {
        se.printStackTrace();
    } finally {
        closeStatement(pst);
        closeConnection(con);
    }
}

From source file:de.elbe5.cms.search.SearchBean.java

License:Open Source License

protected void indexFiles(IndexWriter writer) throws Exception {
    Connection con = getConnection();
    PreparedStatement pst = null;
    try {/*from   w ww  .  j  av a 2  s .co  m*/
        pst = con.prepareStatement(INDEX_FILES_SQL);
        ResultSet rs = pst.executeQuery();
        int count = 0;
        while (rs.next()) {
            FileSearchData data = new FileSearchData();
            getSearchData(data, rs);
            data.setDoc();
            writer.addDocument(data.getDoc());
            count++;
            if ((count % 100) == 0) {
                writer.commit();
            }
        }
        rs.close();
        writer.commit();
        Log.log("finished indexing " + count + " files");
    } catch (SQLException se) {
        se.printStackTrace();
    } finally {
        closeStatement(pst);
        closeConnection(con);
    }
}

From source file:de.elbe5.cms.search.SearchBean.java

License:Open Source License

protected void indexFile(IndexWriter writer, int id) throws Exception {
    Connection con = getConnection();
    PreparedStatement pst = null;
    try {// w  ww.j a  v a  2 s  .co  m
        pst = con.prepareStatement(INDEX_NODE_SQL);
        pst.setInt(1, id);
        ResultSet rs = pst.executeQuery();
        if (rs.next()) {
            FileSearchData data = new FileSearchData();
            getSearchData(data, rs);
            data.setDoc();
            writer.addDocument(data.getDoc());
        }
        rs.close();
        writer.commit();
        Log.log("finished indexing file");
    } catch (SQLException se) {
        se.printStackTrace();
    } finally {
        closeStatement(pst);
        closeConnection(con);
    }
}

From source file:de.elbe5.cms.search.SearchBean.java

License:Open Source License

protected void indexUsers(IndexWriter writer) throws Exception {
    Connection con = getConnection();
    PreparedStatement pst = null;
    try {/* w w w. j  av a 2 s  .c om*/
        int count = 0;
        pst = con.prepareStatement(INDEX_USERS_SQL);
        ResultSet rs = pst.executeQuery();
        while (rs.next()) {
            SearchData data = getUserSearchData(rs);
            writer.addDocument(data.getDoc());
            count++;
            if ((count % 100) == 0) {
                writer.commit();
            }
        }
        rs.close();
        writer.commit();
        Log.log("finished indexing " + count + " users");
    } catch (SQLException se) {
        se.printStackTrace();
    } finally {
        closeStatement(pst);
        closeConnection(con);
    }
}

From source file:de.elbe5.cms.search.SearchBean.java

License:Open Source License

protected void indexUser(IndexWriter writer, int id) throws Exception {
    Connection con = getConnection();
    PreparedStatement pst = null;
    try {/*from  www .j av  a2s.  c  o m*/
        pst = con.prepareStatement(INDEX_USER_SQL);
        pst.setInt(1, id);
        ResultSet rs = pst.executeQuery();
        while (rs.next()) {
            SearchData data = getUserSearchData(rs);
            writer.addDocument(data.getDoc());
        }
        rs.close();
        writer.commit();
        Log.log("finished indexing user");
    } catch (SQLException se) {
        se.printStackTrace();
    } finally {
        closeStatement(pst);
        closeConnection(con);
    }
}

From source file:de.hsmannheim.ss15.alr.searchengine.DefaultLuceneController.java

/**
 * Indexes a single document/* w ww.jav a  2s .c  om*/
 */
static void indexDoc(IndexWriter writer, Path file, long lastModified) throws IOException {
    try (InputStream stream = Files.newInputStream(file)) {
        // make a new, empty document
        org.apache.lucene.document.Document doc = new org.apache.lucene.document.Document();

        // Add the path of the file as a field named "path".  Use a
        // field that is indexed (i.e. searchable), but don't tokenize 
        // the field into separate words and don't index term frequency
        // or positional information:
        Field pathField = new StringField("path", file.toString(), Field.Store.YES);
        doc.add(pathField);

        // Add the last modified date of the file a field named "modified".
        // Use a LongField that is indexed (i.e. efficiently filterable with
        // NumericRangeFilter).  This indexes to milli-second resolution, which
        // is often too fine.  You could instead create a number based on
        // year/month/day/hour/minutes/seconds, down the resolution you require.
        // For example the long value 2011021714 would mean
        // February 17, 2011, 2-3 PM.
        doc.add(new LongField("modified", lastModified, Field.Store.NO));

        // Add the contents of the file to a field named "contents".  Specify a Reader,
        // so that the text of the file is tokenized and indexed, but not stored.
        // Note that FileReader expects the file to be in UTF-8 encoding.
        // If that's not the case searching for special characters will fail.
        BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));

        List<String> lines = new ArrayList<>();
        while (reader.ready()) {
            lines.add(reader.readLine());
        }
        if (lines.size() > 0) {
            String urlLine = lines.remove(0);
            if (urlLine != null && urlLine.startsWith("URL:")) {
                urlLine = urlLine.substring(4);
                doc.add(new TextField("URL", urlLine, Field.Store.YES));
            }
        }
        if (lines.size() > 0) {
            String dataType = lines.remove(0);
            if (dataType != null && dataType.startsWith("DataType:")) {
                dataType = dataType.substring(9);
                doc.add(new TextField("DataType", dataType, Field.Store.YES));
            }
        }
        if (lines.size() > 0) {
            String title = lines.remove(0);
            if (title != null && title.startsWith("Title:")) {
                title = title.substring(6);
                doc.add(new TextField("title", title, Field.Store.YES));
            }
        }
        String content = "";
        for (String s : lines) {
            content = content + s;
        }
        doc.add(new TextField("contents", content, Field.Store.NO));

        if (writer.getConfig().getOpenMode() == IndexWriterConfig.OpenMode.CREATE) {
            // New index, so we just add the document (no old document can be there):
            writer.addDocument(doc);
        } else {
            // Existing index (an old copy of this document may have been indexed) so 
            // we use updateDocument instead to replace the old one matching the exact 
            // path, if present:
            writer.updateDocument(new Term("path", file.toString()), doc);
        }
    }
}

From source file:de.hybris.platform.lucenesearch.jalo.LuceneTest.java

License:Open Source License

@Before
public void setUp() throws Exception {
    directory = new RAMDirectory();
    final IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_40,
            new StandardAnalyzer(Version.LUCENE_40)).setOpenMode(OpenMode.CREATE_OR_APPEND);
    final IndexWriter writer = new IndexWriter(directory, indexWriterConfig);
    docA = new Document();
    docA.add(new Field("key", "a", Field.Store.YES, Field.Index.NOT_ANALYZED));
    docA.add(new Field("text", "text zum ersten document", Field.Store.YES, Field.Index.ANALYZED));
    writer.addDocument(docA);
    docB = new Document();
    docB.add(new Field("key", "b", Field.Store.YES, Field.Index.NOT_ANALYZED));
    docB.add(new Field("text", "text zum zweiten document", Field.Store.YES, Field.Index.ANALYZED));
    writer.addDocument(docB);//from   w w  w .j a  va  2 s .c  o m
    docC = new Document();
    docC.add(new Field("key", "c", Field.Store.YES, Field.Index.NOT_ANALYZED));
    docC.add(new Field("text", "text zum dritten document", Field.Store.YES, Field.Index.ANALYZED));
    writer.addDocument(docC);
    //writer.optimize();
    writer.close();
}