基於StandardAnalyzer讀寫索引實例詳解
lucene
基於
實戰
讀寫
前言
使用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 }
登入後複製
以上是基於StandardAnalyzer讀寫索引實例詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前
By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
擊敗分裂小說需要多長時間?
3 週前
By DDD
R.E.P.O.保存文件位置:在哪里以及如何保護它?
3 週前
By DDD

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)