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

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

Introduction

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

Prototype

@Override
public final long commit() throws IOException 

Source Link

Document

Commits all pending changes (added and deleted documents, segment merges, added indexes, etc.) to the index, and syncs all referenced index files, such that a reader will see the changes and the index updates will survive an OS or machine crash or power loss.

Usage

From source file:net.sf.lucis.core.impl.EmptyDirectory.java

License:Apache License

private static synchronized void create() throws IOException {
    if (directory != null) {
        return;/*from   w  w  w. j  av a  2  s  .  co m*/
    }
    final RAMDirectory ram = new RAMDirectory();
    IndexWriter w = new IndexWriter(ram, Factory.get().writerConfig());
    w.commit();
    w.close();
    directory = ram;
}

From source file:net.sf.lucis.core.impl.ReindexingFSStore.java

License:Apache License

public ReindexingFSStore(final String indexDir) {
    try {//from   w  w  w .  j ava2s  .c  o m
        this.file = new File(indexDir);
        checkArgument(file.exists() && file.isDirectory(), "Invalid index directory");
        this.statusFile = file(file, STATUS_FILE, "Status file");
        this.checkpointFile = file(file, CHECKPOINT_FILE, "Checkpoint file");
        this.copy01 = new Copy(file, COPY01);
        this.copy02 = new Copy(file, COPY02);
        final Status status = readStatus(this.statusFile);
        if (Status.NULL.equals(status)) {
            IndexWriter w = new IndexWriter(copy01.directory, Factory.get().writerConfig());
            w.commit();
            w.close();
            state = new AtomicReference<State>(STATE);
            writeStatus();
            writeCheckpoint(null);
        } else {
            state = new AtomicReference<State>(new State(status));
        }
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}

From source file:net.simpleframework.ado.lucene.AbstractLuceneManager.java

License:Apache License

@Override
public void rebuildIndex() {
    final IDataQuery<?> dq = queryAll();
    if (dq == null) {
        return;/* w w w  .  jav  a 2s.  co  m*/
    }
    IndexWriter iWriter = null;
    try {
        iWriter = createIndexWriter();
        if (indexExists()) {
            iWriter.deleteAll();
        }
        dq.setFetchSize(0);
        for (Object obj; (obj = dq.next()) != null;) {
            final LuceneDocument document = new LuceneDocument();
            if (objectToDocument(obj, document)) {
                iWriter.addDocument(document.doc);
            }
        }
        iWriter.commit();
    } catch (final IOException e) {
        throw ADOException.of(e);
    } finally {
        closeWriter(iWriter);
    }
}

From source file:net.skyatlas.icd.dao.daoImpl.AnsjAnalysisTest.java

@Test
public void indexTest() throws CorruptIndexException, LockObtainFailedException, IOException {
    HashSet<String> hs = new HashSet<String>();
    //        BufferedReader reader2 = IOUtil.getReader(ResourceBundle.getBundle("library").getString("stopLibrary"), "UTF-8");
    //        String word = null;
    //        while ((word = reader2.readLine()) != null) {
    //            hs.add(word);
    //        }//from ww w  . j a va2s . c om
    Analyzer analyzer = new AnsjAnalysis(hs, false);
    Directory directory = null;
    IndexWriter iwriter = null;

    //        BufferedReader reader = IOUtil.getReader("/Users/ansj/Desktop/??/indextest.txt", "UTF-8");
    //        String temp = null;
    //        StringBuilder sb = new StringBuilder();
    //        while ((temp = reader.readLine()) != null) {
    //            sb.append(temp);
    //            sb.append("\n");
    //        }
    //        reader.close();
    String text = "????????????  ??? ????????";

    IndexWriterConfig ic = new IndexWriterConfig(Version.LUCENE_32, analyzer);
    // 
    directory = new RAMDirectory();
    iwriter = new IndexWriter(directory, ic);
    // BufferedReader reader =
    // IOUtil.getReader("/Users/ansj/Documents//?//1998?_.txt",
    // "GBK");
    // String temp = null;
    // while ((temp = reader.readLine()) != null) {
    // addContent(iwriter, temp);
    // }
    addContent(iwriter, text);
    addContent(iwriter, text);
    addContent(iwriter, text);
    addContent(iwriter, text);
    iwriter.commit();
    iwriter.close();

    System.out.println("");

    //        search(analyzer, directory, "");
}

From source file:net.skyatlas.icd.test.AnsegTest.java

static public void main(String[] args)
        throws IOException, CorruptIndexException, ParseException, InvalidTokenOffsetsException {
    AnsegTest inst = new AnsegTest();
    Token nt = new Token();
    Analyzer ca = new AnsjAnalysis();
    Reader sentence = new StringReader(
            "\n\n\n\n\n\n\n????, ????????????????????????????"
                    + "???????????????????"
                    + "??????????? ??????????????2????"
                    + ""
                    + "? ?????????????  ??? ????????");
    TokenStream ts = ca.tokenStream("sentence", sentence);

    System.out.println("start: " + (new Date()));
    long before = System.currentTimeMillis();
    while (ts.incrementToken()) {
        System.out.println(ts.getAttribute(CharTermAttribute.class));
    }//from   ww w .j av  a  2  s.  com
    ts.close();
    long now = System.currentTimeMillis();
    System.out.println("time: " + (now - before) / 1000.0 + " s");

    HashSet<String> hs = new HashSet<String>();
    BufferedReader reader2 = IOUtil.getReader(ResourceBundle.getBundle("library").getString("stopLibrary"),
            "UTF-8");
    String word = null;
    while ((word = reader2.readLine()) != null) {
        hs.add(word);
    }
    Analyzer analyzer = new AnsjAnalysis(hs, false);
    Directory directory = null;
    IndexWriter iwriter = null;

    BufferedReader reader = IOUtil.getReader("/Users/changzhenghe/Downloads/hy_statspack01.txt", "UTF-8");
    String temp = null;
    StringBuilder sb = new StringBuilder();
    while ((temp = reader.readLine()) != null) {
        sb.append(temp);
        sb.append("\n");
    }
    reader.close();
    String text = sb.toString();

    text = "????????????  ??? ????????";

    IndexWriterConfig ic = new IndexWriterConfig(Version.LUCENE_32, analyzer);
    // 
    directory = new RAMDirectory();
    iwriter = new IndexWriter(directory, ic);
    // BufferedReader reader =
    // IOUtil.getReader("/Users/ansj/Documents//?//1998?_.txt",
    // "GBK");
    // String temp = null;
    // while ((temp = reader.readLine()) != null) {
    // addContent(iwriter, temp);
    // }
    inst.addContent(iwriter, "?   ?()   (?)");
    inst.addContent(iwriter, "   ?()   (?)");
    inst.addContent(iwriter, "?   ?   (?)");
    inst.addContent(iwriter, "   ??NEC   ");
    inst.addContent(iwriter, "?");
    iwriter.commit();
    iwriter.close();

    System.out.println("");

    inst.search(analyzer, directory, "");
    inst.search(analyzer, directory, "");
    inst.search(analyzer, directory, "");
    inst.search(analyzer, directory, "?");

    /*
     KeyWordComputer kwc = new KeyWordComputer(5);
     String title = "??";
     String content = "9??"
     + "?????????"
     + "????"
     + "??"
     + "?????"
     + "???"
     + "??????"
     + "???"
     + "????20??"
     + "????"
     + "?"
     + "???]??"
     + "???";
     Collection<Keyword> result = kwc.computeArticleTfidf(title, content);
     System.out.println(result);
            
     AnsegTest t = new AnsegTest();
     List<Term> parse = ToAnalysis.parse("?");
     System.out.println(parse);
     System.out.println("*********** ? ************");
     //        UserDefineLibrary.insertWord("", "userDefine", 1000);
     //        UserDefineLibrary.insertWord("?", "userDefine", 1000);
     UserDefineLibrary.insertWord("?", "userDefine", 1000);
     parse = ToAnalysis.parse("???");
     System.out.println(parse);
     */
}

From source file:net.skyatlas.icd.util.MemoryGrid.java

public void init()
        throws IOException, CorruptIndexException, InvalidTokenOffsetsException, ParseException, SQLException {
    System.err.println("--------------\t\t Memory Grid Initializing -------------");
    this.allDiseases = this.dao.getAllDisease(); // Vol1 ?

    this.allIndexItems = this.dao.getAllDiseaseIndexes(); // Vol3 ?
    this.allIndexRelations = this.dao.getAllIcdDiseaseIdxRelations(); //Vol3 ?
    this.allDisRelations = this.icdDisRelationDao.getAllDiseaseRelation();//Vol1 ?
    // ?//  w w  w  .  j  a v a2s .co  m
    if (this.allRootIndexItems == null) {
        this.allRootIndexItems = new ArrayList();
    }
    System.err.println("All Index Items size:" + allIndexItems.size());
    System.err.println("All Disease Items size:" + allDiseases.size());
    //Vol1 Relation
    for (int i = 0; i < this.allDisRelations.size(); i++) {
        IcdDiseaseRelation r = this.allDisRelations.get(i);
        this.getVol1IDRelationIndexedItems().put(r.getId(), r);
        List<IcdDiseaseRelation> list = this.getVol1mainIDRelationIndexedItems().get(r.getMainID());
        if (list == null || list.size() == 0) {
            list = new ArrayList();
            list.add(r);
            this.getVol1mainIDRelationIndexedItems().put(r.getMainID(), list);
        } else {
            list.add(r);
        }
    }
    // Vol 1
    for (int i = 0; i < this.allDiseases.size(); i++) {
        IcdDisease d = this.allDiseases.get(i);

        //set Relations
        List<IcdDiseaseRelation> icdDisRelations = new ArrayList();
        icdDisRelations = this.getVol1mainIDRelationIndexedItems().get(d.getId());
        HashMap<String, List<IcdDiseaseRelation>> refRelations = new HashMap();
        List<IcdDiseaseRelation> noIncludeList = new ArrayList();//d.getRefRelations().get("?");
        List<IcdDiseaseRelation> otherCodeList = new ArrayList();//d.getRefRelations().get("??");
        List<IcdDiseaseRelation> swordList = new ArrayList();//d.getRefRelations().get("");
        List<IcdDiseaseRelation> includeList = new ArrayList();//d.getRefRelations().get("");
        if (icdDisRelations == null || icdDisRelations.size() == 0) {

        } else {
            for (int j = 0; j < icdDisRelations.size(); j++) {
                IcdDiseaseRelation idr = icdDisRelations.get(j);
                if (idr.getRelationType().equals("?")) {
                    noIncludeList.add(idr);
                } else if (idr.getRelationType().equals("??")) {
                    otherCodeList.add(idr);
                } else if (idr.getRelationType().equals("")) {
                    swordList.add(idr);
                } else if (idr.getRelationType().equals("")) {
                    includeList.add(idr);
                }
            }
            if (noIncludeList.size() > 0) {
                refRelations.put("?", noIncludeList);
            }
            if (otherCodeList.size() > 0) {
                refRelations.put("??", otherCodeList);
            }
            if (swordList.size() > 0) {
                refRelations.put("", swordList);
            }
            if (includeList.size() > 0) {
                refRelations.put("", includeList);
            }
        }
        d.setRefRelations(refRelations);

        // ID Unique Index
        this.getVol1IDIndexedItems().put(d.getId(), d);

        //            System.err.println(" ? IcdDisease, ICD Code :"+d.getIcdCode());
        // ICD Code 
        if (d.getIcdCode() != null && d.getIcdCode().length() > 0) { // ICD ??
            List<IcdDisease> alIndexes;
            if (this.getVol1IcdIndexedItems().containsKey(d.getIcdCode().toUpperCase())) {
                alIndexes = this.getVol1IcdIndexedItems().get(d.getIcdCode().toUpperCase());
            } else {
                alIndexes = new ArrayList();
            }
            //                System.err.println("Index Disease :"+d.getIcdCode());
            alIndexes.add(d);
            this.getVol1IcdIndexedItems().put(d.getIcdCode().replaceAll("$", "").toUpperCase(), alIndexes);
        }
        // Vol1 Name 
        List<IcdDisease> alIndexes2;
        if (this.getVol1NameIndexedItems().containsKey(d.getCodeNameCh())) {
            alIndexes2 = this.getVol1NameIndexedItems().get(d.getCodeNameCh());
        } else {
            alIndexes2 = new ArrayList();
        }
        alIndexes2.add(d);
        this.getVol1NameIndexedItems().put(d.getCodeNameCh(), alIndexes2);

        // Vol1  
        List<IcdDisease> alIndexes3;
        if (this.getVol1PinYinIndexedItems().containsKey(d.getPy())) {
            alIndexes3 = this.getVol1PinYinIndexedItems().get(d.getPy());
        } else {
            alIndexes3 = new ArrayList();
        }
        alIndexes3.add(d);
        this.getVol1PinYinIndexedItems().put(d.getPy(), alIndexes3);

    }

    /*
    Vol1parent-children 
    */
    for (int i = 0; i < this.allDiseases.size(); i++) {
        IcdDisease d = this.allDiseases.get(i);
        if (d.getParentID() != null && d.getParentID() != 0) {
            IcdDisease parent = this.getVol1IDIndexedItems().get(d.getParentID());
            d.setParentDisease(parent);
            if (parent == null) {
                System.err.println(" parent is null :" + d.getId() + "\t" + d.getParentID());
                continue;
            }
            if (parent.getChildren() == null) {
                parent.setChildren(new ArrayList());
            }
            parent.getChildren().add(d);
        }
    }

    // Vol 3
    for (int i = 0; i < this.allIndexItems.size(); i++) {
        IcdDiseaseIndex e = this.allIndexItems.get(i);
        this.getVol3IDIndexedItems().put(e.getId(), e); // ID  Unique

        // ??
        List<IcdDiseaseIndex> alIdxes;
        if (this.getVol3NameIndexedItems().containsKey(e.getNameCh())) {
            alIdxes = this.getVol3NameIndexedItems().get(e.getNameCh());
        } else {
            alIdxes = new ArrayList();
        }
        alIdxes.add(e);
        this.getVol3NameIndexedItems().put(e.getNameCh(), alIdxes);

        // 
        List<IcdDiseaseIndex> alIdxes2;
        if (this.getVol3PinYinIndexedItems().containsKey(e.getPy())) {
            alIdxes2 = this.getVol3PinYinIndexedItems().get(e.getPy());
        } else {
            alIdxes2 = new ArrayList();
        }
        alIdxes2.add(e);
        this.getVol3PinYinIndexedItems().put(e.getPy(), alIdxes2);

        // ?????
        if (this.dealAlias) {
            List<String> slist = new ArrayList();
            slist = dao.getAliasesByIndexid(e.getId());
            if (slist.size() != 0) {
                String[] aliases = new String[slist.size()];
                slist.toArray(aliases);
                e.setAliases(aliases);
            }
        }

        if (e.getDepth() == 0) { // Vol3 Item ?

            this.allRootIndexItems.add(e);
            //                Pattern p = Pattern.compile("[\\[]");
            //                Matcher m = p.matcher(e.getNameCh());
            //                while (m.find()) {
            //                    System.err.println("[]??:\t"+e.getNameCh()+"-> startIndex:"+m.start()+"-> endIndex:"+m.end()+" Match:"+m.group());
            //                }
            //                Pattern p2 = Pattern.compile("[]\\)");
            //                Matcher m2 = p2.matcher(e.getNameCh());
            //                while (m2.find()) {
            //                    System.err.println("(?)??:\t"+e.getNameCh()+"-> startIndex:"+m2.start()+"-> endIndex:"+m2.end()+" Match:"+m2.group());
            //                }

            //                p2 = Pattern.compile(pstr1);
            //                m2 = p2.matcher(e.getNameCh());
            //                while (m2.find()) {
            //                    System.err.println(" ()   [] "+m2.group()+"-->"+e.getNameCh()+"->"+m2.group(1));
            //                }
            //                
            //                p2 = Pattern.compile(pstr2);
            //                m2 = p2.matcher(e.getNameCh());
            //                while (m2.find()) {
            //                    System.err.println(" []   () "+m2.group()+"-->"+e.getNameCh());
            //                }
            //                
            //                
            //                p2 = Pattern.compile(pstr3);
            //                m2 = p2.matcher(e.getNameCh());
            //                while (m2.find()) {
            //                    System.err.println(" []  ? "+m2.group()+"-->"+e.getNameCh());
            //                }
            //                
            //                
            //                p2 = Pattern.compile(pstr4);
            //                m2 = p2.matcher(e.getNameCh());
            //                while (m2.find()) {
            //                    System.err.println(" ()  ? "+m2.group()+"-->"+e.getNameCh());
            //                }
            //                 ??
            /*
                 parseRootIndex(e);
                 e.getParseTreeRoot().setDepth(0);
                 System.out.println("? " + e.getNameCh() + " \n Parse Result :" + e.getParseTreeRoot().toString());
                         
                 */
        }
        // ???

    }
    //Vol3 parent-children
    for (int i = 0; i < this.allIndexItems.size(); i++) {
        IcdDiseaseIndex idi = this.allIndexItems.get(i);
        if (idi.getParentID() != null && idi.getParentID() != 0) {
            IcdDiseaseIndex parent = this.getVol3IDIndexedItems().get(idi.getParentID());
            idi.setParentDiseaseIndex(parent);
            if (parent == null) {
                System.err.println(" parent is null :" + idi.getId() + "\t" + idi.getParentID());
                continue;
            }
            if (parent.getChildren() == null) {
                parent.setChildren(new ArrayList());
            }
            parent.getChildren().add(idi);
        }
    }
    // ? Vol3
    for (IcdDiseaseIndex idi : this.allIndexItems) {
        // ICD ??
        if (idi.getIcdCode() != null && idi.getIcdCode().length() > 0) {
            Stack<IcdDiseaseIndex> v = new Stack();
            createIdexSearchPath(v, idi);

            // ?
            SearchPath path = new SearchPath();
            Iterator<IcdDiseaseIndex> itr = v.iterator();
            while (itr.hasNext()) {
                IcdDiseaseIndex i = itr.next();
                path.getNodeList().add(i);

                //  IcdDiseaseIndex Parse
                if (idi.getDepth() > 0) {
                    /*
                    parseRootIndex(idi);
                    */
                }
            }
            path.setNodes(path.getNodeList().size());
            path.setStartPoint(path.getNodeList().get(0));

            // Get Terminal ICD Disease
            /*
             M72.5   Vol1  ?
             D54     Vol1  ?
             W13     Vol1  
             */

            List<IcdDisease> ds = this.getVol1IcdIndexedItems()
                    .get(idi.getIcdCode().replaceAll("$", "").toUpperCase());
            if (ds == null) { // Maybe the icd code in Vol3 cannot be found in Vol1, Skip this item...
                ds = this.getVol1IcdIndexedItems().get(idi.getIcdCode().replaceAll("$", "")
                        .replaceAll("\\.$", "").replace(".-", "").toUpperCase());
                if (ds == null) {
                    System.err.println("ICD CODE:" + idi.getIcdCode() + " Indexed ?"
                            + this.getVol1IcdIndexedItems().containsKey(idi.getIcdCode().toUpperCase()) + "--"
                            + idi.getIcdCode().replaceAll("$", "").replaceAll("\\.$", "")
                                    .replace(".-", "").toUpperCase());
                    continue;
                }

            }
            for (IcdDisease dis : ds) {
                if (dis.getCodeType().equals("") || dis.getCodeType().equals("")) {
                    continue;
                }
                path.setTerminal(dis);
            }

            //  ???
            setDimension(path, path.getTerminal());

            // 
            this.getAllSearchPaths().add(path);

            this.getGuidIndexedSearchPaths().put(path.getPathHashCode(), path);

            // ??ICD Code
            List<SearchPath> al1 = this.getIcdIndexedSearchPaths()
                    .get(path.getTerminal().getIcdCode().toUpperCase());
            if (al1 == null) {
                al1 = new ArrayList<SearchPath>();
            }
            al1.add(path);
            this.getIcdIndexedSearchPaths().put(path.getTerminal().getIcdCode().toUpperCase(), al1);

            // ??(ICDDiseaseIndex)
            List<SearchPath> al2 = this.getRootIndexedSearchPaths().get(path.getStartPoint());
            //  SearchPath al2 = this.getRootIndexedSearchPaths().get(path.getStartPoint());
            if (al2 == null || al2.size() == 0) {
                //al2 = new SearchPath();
                al2 = new ArrayList();
            }
            al2.add(path);

            this.getRootIndexedSearchPaths().put(path.getStartPoint(), al2);

        }
    }
    /*2014-06-11 15:28:22
     *  Vol1parent-children 
            
    for(int i =0;i<this.allIndexItems.size();i++){
    IcdDiseaseIndex d = this.allIndexItems.get(i);
    if(d.getParentID()!=null&&d.getParentID()!=0){
        IcdDiseaseIndex parent = this.getVol3IDIndexedItems().get(d.getParentID());
        d.setParentDiseaseIndex(parent);
        if(parent == null){
            System.err.println(" parent is null :"+d.getId()+"\t"+d.getParentID());
            continue;
        }
        if(parent.getChildren() ==null){
            parent.setChildren(new ArrayList());
        }
        parent.getChildren().add(d);
    }
    } */
    // ? End!

    /*
    ?:
        1.   ICDDisease ? children   --> ?
        2.   ICDDisease ?Lucene      --> ?
        3.   search  ?
            
    */

    System.err.println("Search Path #:" + this.getAllSearchPaths().size());
    System.err.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
    //        for (SearchPath sp : this.getAllSearchPaths()) {
    //            System.err.println(sp);
    //        }
    System.err.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
    /*
    for (IcdDiseaseIndex idi : this.allRootIndexItems) {
    System.err.println("? :" + idi.getNameCh() + "->" + idi.getPage() + "-" + idi.getPathStr() + "-" + idi.getDepth());
    List<SearchPath> paths = this.getRootIndexedSearchPaths().get(idi);
    if (paths == null) {
        System.err.println("\t Search Path");
    } else {
        for (SearchPath sp : paths) {
        System.err.println("\t "+sp.getIndexString());
        }
    }
            
    }
    */

    System.err.println("******************* Begin create Idnex *********************");
    HashSet<String> hs = new HashSet<String>();
    BufferedReader reader2 = IOUtil.getReader(ResourceBundle.getBundle("library").getString("stopLibrary"),
            "UTF-8");
    String word = null;
    while ((word = reader2.readLine()) != null) {
        hs.add(word);
    }

    //  MemoryGrid  Lucene ??
    directory = new RAMDirectory();
    Version matchVersion = Version.LUCENE_48;
    analyzer = new AnsjAnalysis(hs, false);

    /*
    TokenStream ts = analyzer.tokenStream("", "?");
    //        CharTermAttribute term = ts.addAttribute(CharTermAttribute.class);
            
    System.err.println(" Test ?");
    while (ts.incrementToken()) {
    System.out.println("?" + ts.getAttribute(CharTermAttribute.class) + "");
    }
    ts.close();
    */

    IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_48, analyzer);
    iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
    IndexWriter writer = new IndexWriter(directory, iwc);

    // ?
    for (SearchPath path : this.getAllSearchPaths()) {
        //            System.err.println(" ? :"+path.getIndexString());
        addContent(writer, path.getIndexString(), path.getPathHashCode(), path.getNodes(), "vol3");

    }

    writer.commit();
    writer.close();

    System.err.println("***************** index created *******************");

    System.out.println("");

    //        search(analyzer,directory,"?");

    //DEBUG:   HIS?? ?ICD??

    /*
    try {
    OracleDataSource ods = new OracleDataSource();
    ods.setURL("jdbc:oracle:thin:@//192.168.1.115:1521/omac");
    ods.setUser("oracle");
    ods.setPassword("quickhis");
            
    Connection conn = ods.getConnection();
    String sql = "select main_icd,diag_name from fir_out_diag_tab where diag_date > (sysdate-1000) and rownum < 1000";
    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery(sql);
            
    while (rs.next()) {
        String mainIcd = rs.getString(1);
        String diagName = rs.getString(2);
        if (diagName==null || "".equals(diagName)) {
            continue;
        }
        diagName = diagName.replaceAll("", "(").replaceAll("", ")");
        System.err.println(" ICD_CODE:"+mainIcd+"\t >>"+diagName);
        search(analyzer,directory,diagName);
    }
            
    rs.close();
    stmt.close();
    conn.close();
    } catch (SQLException ex) {
    ex.printStackTrace();
    }
            
    */
}

From source file:net.tooan.ynpay.third.mongodb.lucene.BuguIndex.java

License:Apache License

public void close() {
    try {//from w w w.  j a  va  2  s  . c  o  m
        if (executor != null) {
            executor.shutdown();
            executor.awaitTermination(5, TimeUnit.SECONDS);
        }
        if (scheduler != null) {
            scheduler.shutdown();
            scheduler.awaitTermination(5, TimeUnit.SECONDS);
        }
        if (clusterConfig != null) {
            clusterConfig.invalidate();
        }
    } catch (InterruptedException ex) {
        logger.error(ex.getMessage(), ex);
    }

    Map<String, IndexWriter> map = IndexWriterCache.getInstance().getAll();
    for (IndexWriter writer : map.values()) {
        if (writer != null) {
            Directory dir = writer.getDirectory();
            try {
                writer.commit();
                writer.close(true);
            } catch (CorruptIndexException ex) {
                logger.error("Can not commit and close the lucene index", ex);
            } catch (IOException ex) {
                logger.error("Can not commit and close the lucene index", ex);
            } finally {
                try {
                    if (dir != null && IndexWriter.isLocked(dir)) {
                        IndexWriter.unlock(dir);
                    }
                } catch (IOException ex) {
                    logger.error("Can not unlock the lucene index", ex);
                }
            }
        }
    }
}

From source file:net.ymate.platform.module.search.Searchs.java

License:Apache License

public static void indexCreate(final ISearchable searchable, final ICallbackHandler handler) {
    __doCheckModuleInited();//  w  w w.  ja v  a 2 s. c  o m
    __executor.execute(new Runnable() {

        public void run() {
            IndexedMeta _meta = getIndexedMeta(searchable);
            IndexWriter _writer = getIndexWriter(_meta.getIndexName());
            Document _doc = __doIndexDocumentCreate(searchable);
            try {
                _writer.addDocument(_doc);
                if (handler != null) {
                    handler.onIndexCreated(searchable);
                }
            } catch (IOException ex) {
                _LOG.error("IndexWriter Add Document Error:", ex);
            } finally {
                try {
                    _writer.commit();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    });
}

From source file:net.ymate.platform.module.search.support.IndexHelper.java

License:Apache License

public void release() {
    Searchs.__doStopSafed(__scheduler);//from  w  ww  . ja  v  a  2  s  . c o  m
    //
    _LOG.debug("Release IndexHelper");
    for (IndexWriter writer : Searchs.__WRITER_CACHES.values()) {
        if (writer != null) {
            Directory dir = writer.getDirectory();
            try {
                writer.commit();
                writer.close(true);
            } catch (Exception ex) {
                _LOG.error("Commit And Close IndexWriter Error", RuntimeUtils.unwrapThrow(ex));
            } finally {
                try {
                    if (dir != null && IndexWriter.isLocked(dir)) {
                        IndexWriter.unlock(dir);
                    }
                } catch (IOException ex) {
                    _LOG.error("Unlock IndexWriter", RuntimeUtils.unwrapThrow(ex));
                }
            }
        }
    }
}

From source file:nl.strohalm.cyclos.utils.lucene.IndexOperationRunner.java

License:Open Source License

private void commit(final Class<? extends Indexable> entityType, final IndexWriter writer) {
    try {//from  ww w.j  ava  2  s . c  o  m
        writer.commit();
    } catch (CorruptIndexException e) {
        handleIndexCorrupted(entityType);
    } catch (Exception e) {
        LOG.warn("Error while committing index writer for " + ClassHelper.getClassName(entityType), e);
    }
}