lucene4.5 deleteAll deleteDocuments 不管用 ,求指教啊

xianwu13 发布于 2014/05/23 11:51
阅读 794
收藏 0

       
        /** **************建立索引************************** */
        // 索引文件目录
        Directory indexDir = new SimpleFSDirectory(new File("D:" + File.separator + "testindex"));
//        Directory indexDir =  new RAMDirectory();
       
        System.out.println("当前lucene的版本号是---" + Version.LUCENE_45);
       
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_45);
       
        // 写索引的配置
        IndexWriterConfig writerConfig = new IndexWriterConfig(Version.LUCENE_45, analyzer);
       
        // 每一次都会进行创建新的索引,第二次删掉原来的创建新的索引
        writerConfig.setOpenMode(OpenMode.CREATE_OR_APPEND);
       
        IndexWriter indexWriter = new IndexWriter(indexDir, writerConfig);
       
        Document document = new Document();
       
        Field stringField = new TextField("int1", "hello lucky boy", Store.YES);
       
        Reader reader = new StringReader("hello lucky boy");
       
        /** ****************创建索引时查看分词的结果************* ****** */
        TokenStream tokenStream = analyzer.tokenStream("", reader);
       
        tokenStream.reset();
       
        CharTermAttribute charTermAttr = tokenStream.getAttribute(CharTermAttribute.class);
       
        System.out.println("tokenStream......" + tokenStream);
       
        System.out.print("....分词开始....");
       
        while (tokenStream.incrementToken())
        {
           
            System.out.print(charTermAttr + "----");
           
        }
        System.out.print("....分词结束....");
       
        document.add(stringField);
       
        indexWriter.addDocument(document);
       
        indexWriter.close();
       
        /*******查询term********/
        IndexReader indexReader = DirectoryReader.open(indexDir);
       
        IndexSearcher searcher = new IndexSearcher(indexReader);
       
        TopDocs docs = searcher.search(new TermQuery(new Term("int1", "boy")), 10);
       
        ScoreDoc[] hits = docs.scoreDocs;
       
        System.out.println("......");
       
        if(null != hits)
        {
           
            System.out.println("...存在查询的结果..."+hits.length);
           
            for(ScoreDoc hit : hits)
            {
               
                System.out.println("命中的结果是......"+hit);
               
                Document doc = searcher.doc(hit.doc);
               
                System.out.println("查询出来的field是......"+doc.get("int1"));
               
            }
           
        }
       
       
        /** *********更新索引文件document********* */
        System.out.println("...更新索引文件开始...");
        Term term = new Term("int1", "lucky");
       
        IndexWriterConfig writerConfig2 = new IndexWriterConfig(Version.LUCENE_45, analyzer);
       
        IndexWriter indexWriter2 = new IndexWriter(indexDir, writerConfig2);
//        indexWriter2.updateDocument(term, document);
       
//        indexWriter2.deleteDocuments(new TermQuery(new Term("int1", "boy")));
        indexWriter2.deleteAll();
       
        indexWriter2.forceMergeDeletes();
       
        indexWriter2.commit();
//        indexWriter2.deleteAll();
       
        indexWriter2.close();
       
       
       
       
       
       
       
       
        System.out.println("......" + document);
       
        System.out.println("...跟新索引文件结束...");
        /** **********跟新索引文件结束**** */
       
       
        /**********update/delete之后的操作*********/
        IndexReader indexReaderUpdate = DirectoryReader.open(indexDir);
       
        IndexSearcher searcherUpdate = new IndexSearcher(indexReader);
       
        TopDocs docsUpdate = searcher.search(new TermQuery(new Term("int1", "lucky")), 10);
       
        ScoreDoc[] hitsUpdate = docsUpdate.scoreDocs;
       
        System.out.println("...跟新之后的Search操作...");
       
        if(null != hitsUpdate)
        {
           
            System.out.println("...跟新之后存在查询的结果...");
           
            for(ScoreDoc hit : hitsUpdate)
            {
               
                System.out.println("命中的结果是......"+hit);
               
                Document doc = searcher.doc(hit.doc);
               
                System.out.println("查询出来的field是......"+doc.get("int1"));
               
            }
           
        }
        
        
        
   
加载中
返回顶部
顶部