Lucene add、updateDocument添加、更新与search查询(转)

本文介绍了一个使用Java实现的Lucene应用案例,演示了如何创建、更新索引及进行文档搜索的过程。通过具体代码展示了如何利用StandardAnalyzer分析器、IndexWriter类创建和更新索引,以及使用QueryParser解析搜索条件并返回搜索结果。
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();
        }
    }
 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值