您好,欢迎访问一九零五行业门户网

基于StandardAnalyzer读写索引实例详解

前言      使用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读写索引实例详解的详细内容。
其它类似信息

推荐信息