lucene2.4测试_updateDocument替换文档,更新索引

package lucene.test.index;   
  
import java.io.File;   
import java.io.FileInputStream;   
import java.io.IOException;   
  
import org.apache.lucene.analysis.Analyzer;   
import org.apache.lucene.analysis.standard.StandardAnalyzer;   
import org.apache.lucene.document.Document;   
import org.apache.lucene.document.Field;   
import org.apache.lucene.index.IndexWriter;   
import org.apache.lucene.index.Term;   
import org.apache.lucene.index.IndexWriter.MaxFieldLength;   
import org.apache.lucene.search.IndexSearcher;   
import org.apache.lucene.search.Query;   
import org.apache.lucene.search.ScoreDoc;   
import org.apache.lucene.search.TermQuery;   
import org.apache.lucene.search.TopDocCollector;   
import org.apache.lucene.store.Directory;   
import org.apache.lucene.store.FSDirectory;   
//索引文档更新   
public class  IndexUpdate2 {   
  
    private static String IndexPath ="D:\\workshop\\TextIndex1";   
    private static String FilePath = "D:\\MajorSoft\\lucene\\temp\\FilePath\\test1.txt";   
    private static String UpdatePath ="D:\\MajorSoft\\lucene\\temp\\FilePath\\test2.txt";   
  
    public static void main(String[] args) {   
       
        try {   
            File file = new File(FilePath);  // 原始文件   
            Directory dir = FSDirectory.getDirectory(IndexPath); // 索引目录   
            Analyzer analyzer = new StandardAnalyzer();                    // 文档分析器   
            IndexWriter writer1 = new IndexWriter(dir,analyzer,true,MaxFieldLength.UNLIMITED);  // 生成索引器对象   
            writer1.setUseCompoundFile(true);   
            Document document = new Document();                              // 新建空文档   
  
            Field field_name = new Field("path", file.getName(),    
                    Field.Store.YES,Field.Index.ANALYZED);   
            document.add(field_name);                                        // 添加文件名域   
  
            FileInputStream inputfile=new FileInputStream(file);             // 文件输入流   
            int len=inputfile.available();   
            byte[] buffer = new byte[len];    
            inputfile.read(buffer);                                          // 读取文件内容   
            inputfile.close();   
               
            String content1 = new String(buffer);   
            Field field_content = new Field( "content", content1,   
                                             Field.Store.YES,Field.Index.ANALYZED );   
            document.add(field_content);                                    // 添加文件内容域   
            writer1.addDocument(document);                                // 添加索引文档   
            writer1.optimize();   
            writer1.close();   
            display(file.getName());                                        // 输出替换前结果   
               
            IndexWriter writer2 = new IndexWriter(dir,analyzer,true,MaxFieldLength.UNLIMITED); // 重建索引器   
            writer2.setUseCompoundFile(true);    
  
            File file2 = new File(UpdatePath);             
            Document document2 = new Document();                             // 生成空文档对象   
            Field field_name2 = new Field("path", file2.getName(),           // 生成文件名域    
                    Field.Store.YES,Field.Index.NOT_ANALYZED);   
            document2.add(field_name2);   
            FileInputStream inputfile2=new FileInputStream(file2);           // 读取文件内容   
            int len2=inputfile2.available();   
            byte[] buffer2 = new byte[len2];    
            inputfile2.read(buffer2);   
            inputfile2.close();   
               
            String content2 = new String(buffer2);   
            Field field_content2 = new Field( "content", content2,   
                                             Field.Store.YES,Field.Index.ANALYZED );   
            document2.add(field_content2);                                  // 添加文件内容域   
            //Term term = new Term("path", file.getName() );                  // 新建语汇单元   
            Term term = new Term("content2", "Lucene" );                  // 新建语汇单元   
            writer2.updateDocument(term, document2);                     // 替换原有匹配文档   
            writer2.optimize();   
            writer2.close();   
            display(file2.getName());                                       // 输出替换后结果   
               
        } catch (IOException e) {   
            e.printStackTrace();   
        }          
    }   
       
    @SuppressWarnings("deprecation")   
    public static void display(String words) throws IOException   
    {   // 显示结果   
        try {          
            IndexSearcher searcher = new IndexSearcher(IndexPath); // 检索器   
            Term term = new Term("path", words );                          // 单词项   
            Query query = new TermQuery(term);                             // 检索单元    
            System.out.println("Query  words****"+query.toString());   
            int hitsPerPage=100;   
            TopDocCollector collector=new TopDocCollector(hitsPerPage);   
            searcher.search(query, collector);   
            ScoreDoc[] hits=collector.topDocs().scoreDocs;                            // 提交检索   
            System.out.println("Search result:");   
            for(int i=0; i < hits.length; i++)                           // 输出结果   
            {   
                Document doc=searcher.doc(hits[i].doc);   
                if(doc.getField("content")!=null)   
                System.out.println("Content: "+":"+doc.getField("content").stringValue());   
                System.out.println("Path: "+doc.getField("path").stringValue());   
            }   
        } catch (IOException e)   
        {   
            e.printStackTrace();   
        }   
    }   
}   
/*  lucene没有全功能的更新数据的函数  
  IndexWriter中updateDocument函数,使用Term实例来查找符合条件的文档 并以制定的新文档替换索引中的旧文档   
  只能管理部分索引 在索引创建完成后市无法维护全部数据的*/  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值