Lucene實戰之基於StandardAnalyzer讀寫索引,luceneanalyzer

來源:互聯網
上載者:User

Lucene實戰之基於StandardAnalyzer讀寫索引,luceneanalyzer
前言

      使用lucene建立索引時如果指定瞭解析器,則需要讀寫都使用這個解析器,目前我發現也就是在處理中文這塊比較麻煩,像你在使用solr時如果配置了ik分詞,則需要把index清空重新建立才能繼續搜尋。

      本篇引用lucene-6.4.0和4.x的幾個關鍵類會有不同的地方。

 

建立索引
 1  public void index(){ 2  3         Directory dir=null; 4         Analyzer analyzer=null; 5         IndexWriterConfig config=null; 6         IndexWriter indexWriter=null; 7         try{ 8             /** 9              * SimpleFSDirectory 不能很好支援多線程操作10              * **/11             dir =new SimpleFSDirectory(Paths.get(INDEX_URL));12 13             analyzer=new StandardAnalyzer();14             config =new IndexWriterConfig(analyzer);15             /**16              * IndexWriter(Directory d,IndexWriterConfig config)17              * **/18             indexWriter =new IndexWriter(dir,config);19 20             indexWriter.deleteAll();21             List<UploadBook> books =bookDao.listAllBooks();22             Document document=null;23 24             SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");25 26             for(UploadBook book:books){27                 document=new Document();28                 document.add(new Field("id",book.getId().toString(), TextField.TYPE_STORED));29                 document.add(new Field("ip",book.getIp(), TextField.TYPE_STORED));30                 document.add(new Field("title",book.getOriginFileName(), TextField.TYPE_STORED));31 32                 document.add(new Field("content", PdfReader.read(INDEX_PDF+book.getNewFileName()),TextField.TYPE_STORED));33                 document.add(new Field("createtime",formatter.format(book.getCreateTime()), TextField.TYPE_STORED));34 35                 indexWriter.addDocument(document);36             }37 38             indexWriter.commit();39 40             System.out.println("======索引建立完成,公建立"+books.size()+"條索引========");41         }catch (IOException ex){42             ex.printStackTrace();43         }44         catch(Exception ex){45             ex.printStackTrace();46         }finally {47             if(indexWriter !=null){48                 try{49                     indexWriter.close();50                 }catch (IOException ex){51                     System.out.println("======indexWriter close exception========");52                 }53             }54         }55 56     }

 

讀取索引
 1  public static List<Book> search2(String kw){ 2         Directory dir=null; 3         Analyzer analyzer=null; 4         List<Book> list = new ArrayList<Book>(); 5         try{ 6             dir= FSDirectory.open(Paths.get("e:\\soso\\index")); 7             analyzer=new StandardAnalyzer(); 8  9             DirectoryReader reader =DirectoryReader.open(dir);10             IndexSearcher searcher=new IndexSearcher(reader);11 12             QueryParser parser=new QueryParser("content",analyzer);13             Query query =parser.parse(kw);14 15             ScoreDoc[] docs=searcher.search(query,100).scoreDocs;16 17             for (int i = 0; i < docs.length; i++) {18                 Document firstHit = searcher.doc(docs[i].doc);19 20                 Book book=new Book();21                 book.setId(Integer.parseInt(firstHit.getField("id").stringValue()));22                 book.setIp(firstHit.getField("ip").stringValue());23 24                 String title=firstHit.getField("title").stringValue();25                 title=title.substring(0,title.lastIndexOf("."));26                 book.setTitle(title);27 28                 String content=firstHit.getField("content").stringValue();29                 if(content.length()>=500){30                     content=content.substring(0,500)+"......";31                 }32                 book.setContent(content);33 34                 SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-mm");35                 Date date =format.parse(firstHit.getField("createtime").stringValue());36                 book.setCreateTime(format.format(date));37 38                 list.add(book);39 40             }41 42         }catch(Exception ex){43 44         }finally {45             try{46                 dir.close();47 48             }catch(IOException ex){49                 ex.printStackTrace();50             }51         }52 53         return list;54     }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.