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

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

Introduction

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

Prototype

public IndexWriter(Directory d, IndexWriterConfig conf) throws IOException 

Source Link

Document

Constructs a new IndexWriter per the settings given in conf.

Usage

From source file:com.bitranger.parknshop.common.fulltext.BuildIndexForShop.java

License:Open Source License

@SuppressWarnings("deprecation")
public void createIndex() throws IOException {
    // store in the directory
    Directory directory = new SimpleFSDirectory(indexFile);
    IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_45, analyzer);
    indexWriterConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
    IndexWriter writer = new IndexWriter(directory, indexWriterConfig);// 
    shoplist = shopDao.findAll();/*from  w w  w. java2 s .  co  m*/
    if (shoplist.size() > 0) {
        long time1 = System.currentTimeMillis();
        for (int i = 0; i < shoplist.size(); i++) {
            Document document = new Document();
            document.add(new Field("id", String.valueOf(shoplist.get(i).getName()), Field.Store.YES,
                    Field.Index.ANALYZED));
            document.add(new Field("psSeller", String.valueOf(shoplist.get(i).getPsSeller()), Field.Store.YES,
                    Field.Index.ANALYZED));
            document.add(new Field("name", shoplist.get(i).getName(), Field.Store.YES, Field.Index.ANALYZED));
            document.add(new Field("status", String.valueOf(shoplist.get(i).getStatus()), Field.Store.YES,
                    Field.Index.ANALYZED));
            document.add(new Field("introduction", shoplist.get(i).getIntroduction(), Field.Store.YES,
                    Field.Index.ANALYZED));
            document.add(new Field("timeCreated", String.valueOf(shoplist.get(i).getTimeCreated()),
                    Field.Store.YES, Field.Index.ANALYZED));
            document.add(new Field("vote", String.valueOf(shoplist.get(i).getVote()), Field.Store.YES,
                    Field.Index.ANALYZED));

            writer.addDocument(document);
        }
        long time2 = System.currentTimeMillis();
        System.out.println("" + writer.numDocs() + "");
        System.out.println("" + (time2 - time1) + "");
    }
    writer.close();
}

From source file:com.bizosys.hsearch.dictionary.DictionaryValues.java

License:Apache License

public int load(Set<String> wordDescription, Writer outputWriter, Analyzer analyzer) throws Exception {
    if (null != reader) {
        try {/*  w ww  . j  ava  2s .c o  m*/
            reader.close();
            if (null != searcher)
                searcher.close();
        } catch (Exception ex) {
        }
    }

    this.idx = new RAMDirectory();
    IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_35, analyzer);
    IndexWriter writer = new IndexWriter(this.idx, indexWriterConfig);

    int linesAdded = 0;
    for (String line : wordDescription) {
        writer.addDocument(createDocument(line));
        linesAdded++;
    }
    if (null != outputWriter)
        outputWriter.append("----------\n</BR>Total  Lines Loaded :  ")
                .append(new Integer(linesAdded).toString());

    writer.close();
    reader = IndexReader.open(this.idx);
    searcher = new IndexSearcher(reader);
    return linesAdded;
}

From source file:com.bizosys.hsearch.dictionary.DictionaryValues.java

License:Apache License

private Directory loadFile(String file, Writer outputWriter) throws Exception {
    RAMDirectory idx = new RAMDirectory();

    IndexWriter writer = new IndexWriter(idx,
            new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));

    loadFileAndIndex(file, writer, outputWriter);
    writer.close();/*  w  w w  . j ava2s.co  m*/
    return idx;
}

From source file:com.bluedragon.search.collection.Collection.java

License:Open Source License

/**
 * Creates an empty collection to get it up and running
 *///from   ww  w  .  jav  a 2 s  .c o  m
public synchronized void create(boolean _errorOnExists) throws IOException {
    setDirectory();

    if (directory.listAll().length > 2) {
        if (_errorOnExists) {
            throw new IOException("directory not empty; possible collection already present");
        } else {
            if (DirectoryReader.indexExists(directory)) {
                return;
            } // otherwise an index doesn't exist so allow the creation code to execute
        }
    }

    IndexWriterConfig iwc = new IndexWriterConfig(AnalyzerFactory.get(language));
    iwc.setOpenMode(OpenMode.CREATE);

    indexwriter = new IndexWriter(directory, iwc);
    indexwriter.commit();
    indexwriter.close();
    indexwriter = null;

    // throw an openbd.create file in there so we know when it was created
    created = System.currentTimeMillis();
    File touchFile = new File(collectionpath, "openbd.created");
    Writer fw = new FileWriter(touchFile);
    fw.close();
}

From source file:com.bluedragon.search.collection.Collection.java

License:Open Source License

private void setIndexWriter() throws IOException {
    if (indexwriter != null)
        return;// w  ww  . java 2 s  . c o m

    setDirectory();
    IndexWriterConfig iwc = new IndexWriterConfig(AnalyzerFactory.get(language));
    iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
    indexwriter = new IndexWriter(directory, iwc);
}

From source file:com.browseengine.bobo.geosearch.index.impl.GeoQueryTest.java

License:Apache License

private Directory buildEmptyDirectory() throws IOException {
    RAMDirectory directory = new RAMDirectory();
    Version version = Version.LUCENE_CURRENT;
    Analyzer analyzer = new StandardAnalyzer(version);
    IndexWriterConfig indexWriterConfig = new IndexWriterConfig(version, analyzer);
    IndexWriter writer = new IndexWriter(directory, indexWriterConfig);
    writer.close();/*from w w  w.j  ava2s. c  om*/
    return directory;
}

From source file:com.browseengine.bobo.geosearch.query.GeoScorerTest.java

License:Apache License

private Directory buildEmptyDirectory() throws IOException {
    RAMDirectory directory = new RAMDirectory();

    Version version = Version.LUCENE_CURRENT;
    Analyzer analyzer = new StandardAnalyzer(version);
    IndexWriterConfig indexWriterConfig = new IndexWriterConfig(version, analyzer);
    IndexWriter writer = new IndexWriter(directory, indexWriterConfig);

    writer.close();/* www.j ava  2 s  .  c  om*/

    return directory;
}

From source file:com.bsiag.smartfield.server.services.custom.lucene.indexwriter.IndexWriterService.java

License:Open Source License

public void createZipIndex(RemoteFile file) {
    try {/*from   w  ww  .j  a v a 2  s.  co m*/
        Set<String> zipCache = new HashSet<String>();

        String path = BundleContextUtility
                .resolve("${workspace_loc}/com.bsiag.smartfield.server/resources/index");
        IOUtility.deleteDirectory(path);
        Directory index = new SimpleFSDirectory(new File(path));
        IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_34,
                new StandardAnalyzer(Version.LUCENE_34));

        IndexWriter w;
        w = new IndexWriter(index, config);

        file.setCharsetName("UTF-8");
        BufferedReader reader = new BufferedReader(file.getDecompressedReader());
        String line;
        while ((line = reader.readLine()) != null) {
            String[] tokens = line.split("\t");
            if (tokens.length > 1) {
                String zip = tokens[0].trim();
                String city = tokens[1].trim();
                if (StringUtility.hasText(city) && StringUtility.hasText(zip)) {
                    w.addDocument(createDocoment(zip, city));
                    zipCache.add(zip + city);
                }
            }
        }

        w.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.bugull.mongo.lucene.cache.IndexWriterCache.java

License:Apache License

public IndexWriter get(String name) {
    IndexWriter writer = cache.get(name);
    if (writer != null) {
        return writer;
    }//w w  w .j  a v  a 2 s .c  o  m

    synchronized (this) {
        writer = cache.get(name);
        if (writer == null) {
            BuguIndex index = BuguIndex.getInstance();
            IndexWriterConfig conf = new IndexWriterConfig(index.getVersion(), index.getAnalyzer());
            double bufferSizeMB = index.getBufferSizeMB();
            conf.setRAMBufferSizeMB(bufferSizeMB);
            try {
                String path = index.getDirectoryPath();
                Directory directory = FSDirectory.open(new File(path + "/" + name));
                writer = new IndexWriter(directory, conf);
            } catch (IOException ex) {
                logger.error("Something is wrong when create IndexWriter for " + name, ex);
            }
            cache.put(name, writer);
        }
    }
    return writer;
}

From source file:com.bugull.mongo.lucene.holder.IndexWriterHolder.java

License:Apache License

public IndexWriter get(String name) {
    IndexWriter writer = map.get(name);/* w ww .ja v a  2s  .  co m*/
    if (writer != null) {
        return writer;
    }

    synchronized (this) {
        writer = map.get(name);
        if (writer == null) {
            BuguIndex index = BuguIndex.getInstance();
            IndexWriterConfig conf = new IndexWriterConfig(index.getVersion(), index.getAnalyzer());
            double bufferSizeMB = index.getBufferSizeMB();
            conf.setRAMBufferSizeMB(bufferSizeMB);
            try {
                String path = index.getDirectoryPath();
                Directory directory = FSDirectory.open(new File(path + "/" + name));
                writer = new IndexWriter(directory, conf);
            } catch (IOException ex) {
                logger.error("Something is wrong when create IndexWriter for " + name, ex);
            }
            map.put(name, writer);
        }
    }
    return writer;
}