Lucene add、updateDocument添加、更新與search查詢(轉)


package com.lucene;
 
import java.io.IOException;
 
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.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
 
public class UpdateDocument {
     
     private static String path = "d:/index" ;
     
     
     public static void main(String[] args){
//        addIndex();
         updateIndex();
         search( "李四" );
         search( "王五" );
     }
     
     public static void addIndex(){
         try {
             IndexWriter write = new IndexWriter(path, new StandardAnalyzer(), true );
             
             Document doc = new Document();
             doc.add( new Field( "id" , "123456" ,Field.Store.YES,Field.Index.UN_TOKENIZED));
             doc.add( new Field( "userName" , "張三" ,Field.Store.YES,Field.Index.TOKENIZED));
             doc.add( new Field( "comefrom" , "北京" ,Field.Store.YES,Field.Index.TOKENIZED));
             
             write.addDocument(doc);
             
             write.close();
             
         } catch (IOException e) {
             e.printStackTrace();
         }
     }
     
     
     public static void updateIndex(){
         try {
             
             IndexWriter write = new IndexWriter(path, new StandardAnalyzer(), false );
             Document docNew = new Document();
             docNew.add( new Field( "id" , "123456" ,Field.Store.YES,Field.Index.UN_TOKENIZED));
             docNew.add( new Field( "userName" , "王五" ,Field.Store.YES,Field.Index.TOKENIZED));
             Term term = new Term( "id" , "123456" );
             /**
               調用updateDocument的方法,傳給它一個新的doc來更新數據,
               Term term = new Term("id","1234567");
               先去索引文件里查找id為1234567的Doc,如果有就更新它(如果有多條,最后更新后只有一條)。如果沒有就新增.
             
               數據庫更新的時候,我們可以只針對某個列來更新,而lucene只能針對一行數據更新。
              */
             write.updateDocument(term, docNew);
             
             write.close();
             
         } catch (IOException e) {
             e.printStackTrace();
         }
     }
 
     public static Query queryParser(String str){
         QueryParser queryParser = new QueryParser( "userName" , new StandardAnalyzer());
         try {
             Query query =  queryParser.parse(str);
             return query;
         } catch (Exception e) {
             e.printStackTrace();
         }
         return null ;
     }
     
     public static void search(String str){
         try {
             IndexSearcher search = new IndexSearcher(path);
             
             Query query = queryParser(str);
             
             Hits hits = search.search(query);
             if (hits== null ){
                 return ;
             }
             if (hits.length() == 0 ){
                 System.out.println( " 沒有搜索到'" + str+ "'" );
                 return ;
             }
             for ( int i = 0 ; i < hits.length(); i++) {
                 Document doc = hits.doc(i);
                 System.out.println( "id = " +hits.id(i));
                 System.out.println( "own id = " + doc.get( "id" ));
                 System.out.println( "userName = " +doc.get( "userName" ));
                 System.out.println( "come from  = " +doc.get( "comefrom" ));
                 System.out.println( "" );
             }
             
         } catch (Exception e) {
             e.printStackTrace();
         }
     }
 
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM