• 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();
            }
        }
     
    }
  • 相关阅读:
    服务器RAID5阵列掉了两块盘恢复数据过程
    sql server数据库错误数据恢复过程
    raid崩溃导致存储虚拟化平台数据丢失
    详解MBR分区结构以及GPT分区结构
    存储硬件故障如何恢复数据库
    服务器raid常见故障解决方案
    误删除VMware虚拟机vmdk文件的恢复方法
    HP P2000 服务器数据恢复+服务器数据恢复通用办法
    DELL EqualLogic PS6100存储数据丢失的解决方案
    IBM DS4800服务器RAID信息丢失数据恢复方法
  • 原文地址:https://www.cnblogs.com/1130136248wlxk/p/5037162.html
Copyright © 2020-2023  润新知