• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

基于StandardAnalyzer读写索引实例详解

java 搞代码 4年前 (2022-01-09) 31次浏览 已收录 0个评论

前言

使用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<a style="color:transparent">来@源gao*daima.com搞@代#码网</a>             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读写索引实例详解的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:基于StandardAnalyzer读写索引实例详解
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址