zoukankan      html  css  js  c++  java
  • 用lucene为数据库搜索建立增量索引

    用 lucene 建立索引不可能每次都重新开始建立,而是按照新增加的记录,一次次的递增
    建立索引的IndexWriter类,有三个参数

    IndexWriter writer = new IndexWriter(path, new StandardAnalyzer(),isEmpty);

    其中第三个参数是bool型的,指定它可以确定是增量索引,还是重建索引.
    对于从数据库中读取的记录,譬如要为文章建立索引,我们可以记录文章的id号,然后下次再次建立索引的时候读取存下的id号,从此id后往下继续增加索引,逻辑如下.

    建立增量索引,主要代码如下
    public void createIndex(String path)
    {
         Statement myStatement 
    = null;
         String articleId
    ="0";
         
    //读取文件,获得文章id号码,这里只存最后一篇索引的文章id
        try { 
            FileReader fr 
    = new FileReader("**.txt");
            BufferedReader br 
    = new BufferedReader(fr);                 
            articleId
    =br.readLine();
            
    if(articleId==null||articleId=="")
            articleId
    ="0";
            br.close();
            fr.close(); 
          } 
    catch (IOException e) { 
            System.out.println(
    "error343!");
            e.printStackTrace();
          }
        
    try {
            
    //sql语句,根据id读取下面的内容
            String sqlText = "*****"+articleId;
            myStatement 
    = conn.createStatement();
            ResultSet rs 
    = myStatement.executeQuery(sqlText);
           
    //写索引
            while (rs.next()) {
             Document doc 
    = new Document();
             doc.add(Field.Keyword(
    "**", DateAdded));
             doc.add(Field.Keyword(
    "**", articleid));
             doc.add(Field.Text(
    "**", URL));    
             doc.add(Field.Text(
    "**", Content));
             doc.add(Field.Text(
    "**", Title));    
             
    try{
                writer.addDocument(doc);
              }
              
    catch(IOException e){
                e.printStackTrace();
             }
               
    //将我索引的最后一篇文章的id写入文件
              try { 
               FileWriter fw 
    = new FileWriter("**.txt");
               PrintWriter out 
    = new PrintWriter(fw);    
               out.close();
               fw.close();
               } 
    catch (IOException e) { 
                 e.printStackTrace();
               }
             }
                ind.Close();
                System.out.println(
    "ok.end");
             }
             
    catch (SQLException e){
                e.printStackTrace();
            }
            
    finally {
                
    //数据库关闭操作
            }        
        }

    然后控制是都建立增量索引的时候根据能否都到id值来设置IndexWriter的第三个参数为true 或者是false

     boolean isEmpty = true;
     
    try { 
        FileReader fr 
    = new FileReader("**.txt");
        BufferedReader br 
    = new BufferedReader(fr);                 
        
    if(br.readLine()!= null) {
            isEmpty 
    = false;
         }
         br.close();
         fr.close(); 
        } 
    catch (IOException e) { 
           e.printStackTrace();
      }
                
      writer 
    = new IndexWriter(Directory, new StandardAnalyzer(),isEmpty);
  • 相关阅读:
    SQL数据库一直显示正在还原
    jQuery获取display为none的隐藏元素的宽度和高度的解决方案
    火狐打开新标签页面不出现九宫格的设置
    【转】在C#中?,?:和??
    【转】JS字符(字母)与ASCII码转换方法
    如何为 .NET Core 安装本地化的 IntelliSense 文件
    compass typography 排版 常用排版方法[Sass和compass学习笔记]
    单元测试 逃不开的Done 与约定
    SASS+COMPASS 自适应 学习笔记
    compass tables 表格 表格常见样式[Sass和compass学习笔记]
  • 原文地址:https://www.cnblogs.com/flyfish/p/298863.html
Copyright © 2011-2022 走看看