日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Lucene实战之基于StandardAnalyzer读写索引

發(fā)布時間:2025/3/15 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Lucene实战之基于StandardAnalyzer读写索引 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

前言

? ? ? 使用lucene創(chuàng)建索引時如果指定了解析器,則需要讀寫都使用這個解析器,目前我發(fā)現(xiàn)也就是在處理中文這塊比較麻煩,像你在使用solr時如果配置了ik分詞,則需要把index清空重新創(chuàng)建才能繼續(xù)搜索。

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

?

創(chuàng)建索引

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("======索引創(chuàng)建完成,公創(chuàng)建"+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 }

?

轉(zhuǎn)載于:https://www.cnblogs.com/sword-successful/p/6961143.html

總結(jié)

以上是生活随笔為你收集整理的Lucene实战之基于StandardAnalyzer读写索引的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。