Example usage for org.apache.lucene.index IndexWriterConfig setOpenMode

List of usage examples for org.apache.lucene.index IndexWriterConfig setOpenMode

Introduction

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

Prototype

public IndexWriterConfig setOpenMode(OpenMode openMode) 

Source Link

Document

Specifies OpenMode of the index.

Usage

From source file:com.bdaum.zoom.lal.internal.LireActivator.java

License:Open Source License

public IndexWriter createIndexWriter(Directory dir, boolean create)
        throws IOException, LockObtainFailedException {
    IndexWriterConfig config = new IndexWriterConfig(getLuceneAnalyzer());
    config.setOpenMode(create ? IndexWriterConfig.OpenMode.CREATE : IndexWriterConfig.OpenMode.APPEND);
    return new IndexWriter(dir, config);
}

From source file:com.bewsia.script.LuceneHandler.java

License:Open Source License

protected void createEntity(SEntity src) {
    if (src.getId().length() == 0)
        return;// w ww .  j  a v a 2  s . c o  m
    if (src.getKind().length() == 0)
        return;

    try {
        if (!src.getKind().equals(KIND_QUOTA)) {
            if (!quotaCreate(src))
                return;
        }
        backup(src);
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
        IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_36, analyzer);
        iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
        IndexWriter writer = new IndexWriter(FSDirectory.open(new File(dirIndex)), iwc);
        Document doc = new Document();
        write(src, doc);
        writer.addDocument(doc);
        writer.close();
    } catch (Exception e) {
    }
}

From source file:com.bewsia.script.LuceneHandler.java

License:Open Source License

protected void updateEntity(SEntity src) {
    if (src.getId().length() == 0)
        return;// w  w  w .  ja va2s.com
    if (src.getKind().length() == 0)
        return;

    try {
        if (!src.getKind().equals(KIND_QUOTA)) {
            if (!quotaUpdate(src))
                return;
        }
        backup(src);
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
        IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_36, analyzer);
        iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
        IndexWriter writer = new IndexWriter(FSDirectory.open(new File(dirIndex)), iwc);
        Document doc = new Document();
        write(src, doc);
        writer.updateDocument(new Term(SEntity.ID, src.getId()), doc);
        writer.close();
    } catch (Exception e) {
    }
}

From source file:com.bewsia.script.LuceneHandler.java

License:Open Source License

protected void deleteEntity(String id) {
    if (id.length() == 0)
        return;//from ww  w.java2  s .  co m
    String kind = "";

    try {
        IndexReader reader = IndexReader.open(FSDirectory.open(new File(dirIndex)));
        IndexSearcher searcher = new IndexSearcher(reader);
        TopDocs td = searcher.search(new TermQuery(new Term(SEntity.ID, id)), 1);
        if (td.totalHits > 0) {
            Document doc = searcher.doc(td.scoreDocs[0].doc);
            kind = doc.get(SEntity.KIND);
        }
        searcher.close();
        reader.close();
    } catch (Exception e) {
    }
    if (kind.length() == 0)
        return;
    if (!allowDelete(id, kind))
        return;

    try {
        if (!kind.equals(KIND_QUOTA)) {
            if (!quotaDelete(id, kind))
                return;
        }
        removeBackup(id, kind);
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
        IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_36, analyzer);
        iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
        IndexWriter writer = new IndexWriter(FSDirectory.open(new File(dirIndex)), iwc);
        writer.deleteDocuments(new Term(SEntity.ID, id));
        writer.close();
    } catch (Exception e) {
    }
}

From source file:com.bitranger.parknshop.common.fulltext.BuildIndexForCustomer.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);// 
    FetchOption option = new FetchOption();
    customerlist = customerDao.findAll(option);
    if (customerlist.size() > 0) {
        long time1 = System.currentTimeMillis();
        for (int i = 0; i < customerlist.size(); i++) {
            Document document = new Document();
            document.add(new Field("nickname", customerlist.get(i).getNickname(), Field.Store.YES,
                    Field.Index.ANALYZED));
            document.add(// w ww .j a  v  a 2s.c  o m
                    new Field("email", customerlist.get(i).getEmail(), Field.Store.YES, Field.Index.ANALYZED));
            document.add(new Field("password", customerlist.get(i).getPassword(), Field.Store.YES,
                    Field.Index.ANALYZED));
            document.add(new Field("gender", String.valueOf(customerlist.get(i).getGender()), Field.Store.YES,
                    Field.Index.ANALYZED));
            document.add(
                    new Field("name", customerlist.get(i).getName(), 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.bitranger.parknshop.common.fulltext.BuildIndexForItem.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);// 
    itemlist = itemDao.findAll();/*from   www .  jav  a2 s . co  m*/
    if (itemlist.size() > 0) {
        long time1 = System.currentTimeMillis();
        for (int i = 0; i < itemlist.size(); i++) {
            Document document = new Document();
            document.add(new Field("name", itemlist.get(i).getName(), Field.Store.YES, Field.Index.ANALYZED));
            document.add(new Field("introduction", itemlist.get(i).getIntroduction(), Field.Store.YES,
                    Field.Index.ANALYZED));
            document.add(new Field("price", String.valueOf(itemlist.get(i).getPrice()), Field.Store.YES,
                    Field.Index.ANALYZED));
            document.add(new Field("urlPicture", itemlist.get(i).getUrlPicture(), Field.Store.YES,
                    Field.Index.ANALYZED));
            document.add(new Field("extra1", String.valueOf(itemlist.get(i).getExtra1()), Field.Store.YES,
                    Field.Index.ANALYZED));
            document.add(new Field("extra2", String.valueOf(itemlist.get(i).getExtra2()), Field.Store.YES,
                    Field.Index.ANALYZED));
            document.add(new Field("countPurchase", String.valueOf(itemlist.get(i).getCountPurchase()),
                    Field.Store.YES, Field.Index.ANALYZED));
            document.add(new Field("countFavourite", String.valueOf(itemlist.get(i).getCountFavourite()),
                    Field.Store.YES, Field.Index.ANALYZED));
            document.add(new Field("countClick", String.valueOf(itemlist.get(i).getCountClick()),
                    Field.Store.YES, Field.Index.ANALYZED));
            document.add(new Field("vote", String.valueOf(itemlist.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.bitranger.parknshop.common.fulltext.BuildIndexForOrder.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);// 
    Date from = new Date(), to = new Date();
    orderlist = orderDao.findAll(from, to);
    if (orderlist.size() > 0) {
        long time1 = System.currentTimeMillis();
        for (int i = 0; i < orderlist.size(); i++) {
            Document document = new Document();
            document.add(new Field("id", String.valueOf(orderlist.get(i).getId()), Field.Store.YES,
                    Field.Index.ANALYZED));
            document.add(new Field("psCustomer", String.valueOf(orderlist.get(i).getPsCustomer()),
                    Field.Store.YES, Field.Index.ANALYZED));
            document.add(new Field("idShop", String.valueOf(orderlist.get(i).getId()), Field.Store.YES,
                    Field.Index.ANALYZED));
            document.add(new Field("status", String.valueOf(orderlist.get(i).getStatus()), Field.Store.YES,
                    Field.Index.ANALYZED));
            document.add(new Field("trackingNumber", orderlist.get(i).getTrackingNumber(), Field.Store.YES,
                    Field.Index.ANALYZED));
            document.add(new Field("address", orderlist.get(i).getPsRecipient().getAddresss(), Field.Store.YES,
                    Field.Index.ANALYZED));
            document.add(new Field("postalCode", orderlist.get(i).getTrackingNumber(), Field.Store.YES,
                    Field.Index.ANALYZED));
            document.add(new Field("nameRecipient", orderlist.get(i).getPsRecipient().getPsCustomer().getName(),
                    Field.Store.YES, Field.Index.ANALYZED));
            document.add(new Field("phoneRecipient", orderlist.get(i).getPsRecipient().getPhoneNumber(),
                    Field.Store.YES, Field.Index.ANALYZED));
            document.add(new Field("priceTotal", String.valueOf(orderlist.get(i).getPriceTotal()),
                    Field.Store.YES, Field.Index.ANALYZED));
            document.add(new Field("timeCreated", String.valueOf(orderlist.get(i).getTimeCreated()),
                    Field.Store.YES, Field.Index.ANALYZED));

            writer.addDocument(document);
        }//from   w  ww  .j  a v  a2s.  c  o  m
        long time2 = System.currentTimeMillis();
        System.out.println("" + writer.numDocs() + "");
        System.out.println("" + (time2 - time1) + "");
    }
    writer.close();
}

From source file:com.bitranger.parknshop.common.fulltext.BuildIndexForSeller.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);// 
    FetchOption option = new FetchOption();
    sellerlist = sellerDao.findAll(new FetchOption().limit(100));
    if (sellerlist.size() > 0) {
        long time1 = System.currentTimeMillis();
        for (int i = 0; i < sellerlist.size(); i++) {
            Document document = new Document();
            document.add(new Field("id", String.valueOf(sellerlist.get(i).getId()), Field.Store.YES,
                    Field.Index.ANALYZED));
            document.add(new Field("nickname", sellerlist.get(i).getNickname(), Field.Store.YES,
                    Field.Index.ANALYZED));
            document.add(new Field("personIdNum", String.valueOf(sellerlist.get(i).getPersonIdNum()),
                    Field.Store.YES, Field.Index.ANALYZED));
            document.add(//from   w w w  . ja  v  a2 s . c  om
                    new Field("email", sellerlist.get(i).getEmail(), Field.Store.YES, Field.Index.ANALYZED));
            document.add(new Field("password", sellerlist.get(i).getPassword(), Field.Store.YES,
                    Field.Index.ANALYZED));
            document.add(new Field("status", String.valueOf(sellerlist.get(i).getStatus()), Field.Store.YES,
                    Field.Index.ANALYZED));
            document.add(new Field("timeCreated", String.valueOf(sellerlist.get(i).getTimeCreated()),
                    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.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 ww w. jav  a 2  s.c  om
    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.bluedragon.search.collection.Collection.java

License:Open Source License

/**
 * Creates an empty collection to get it up and running
 *//*from  ww w .j a v a  2  s  .  c om*/
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();
}