本站首页    管理页面    写新日志    退出


«August 2025»
12
3456789
10111213141516
17181920212223
24252627282930
31


公告
 本博客在此声明所有文章均为转摘,只做资料收集使用。

我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:
日志总数:1304
评论数量:2242
留言数量:5
访问次数:7586522
建立时间:2006年5月29日




[Spring]lucene-2.0.0的基本应用
软件技术

lhwork 发表于 2006/7/31 11:47:49

终于出2。0了 很兴奋,可是也很郁闷;兴奋的是有新功能了,应该去看看;郁闷的是,找了几个小时没找到一个完整的代码,在部长的强烈要求下,只好硬着头皮看文档。。。。。。。好,终于看懂了,该写代码了。首先肯定是建立索引了啊 public void creatIndex() { File indexDir = new File(getPathIndex()); try {List<Article> listArticle = getArticleDao().search(null, null,null, null, null, null, null, null, new Boolean(true)); for (int i = 0; i < listArticle.size(); i++) { Document doc = new Document();  Article article = listArticle.get(i);//我的配置是追加方式的建立索引,所以为了不重复数据,只好先删除再添加  deleteAllIndex(article);  Field fieldId = new Field("id", article.getId().toString(), Field.Store.COMPRESS, Field.Index.TOKENIZED, Field.TermVector.YES); Field fieldTitles = new Field("title", article.getTitle(), Field.Store.COMPRESS, Field.Index.TOKENIZED, Field.TermVector.YES);//我没有用任何的分析器,所以只好用HTMLParser 把HTML分析成文本在索引 String contentHtml = article.getContent(); Reader read = new StringReader(contentHtml); HTMLParser htmlParser = new HTMLParser(read); BufferedReader breader = new BufferedReader(htmlParser.getReader());String htmlContent = ""; String tempContent = breader.readLine(); while (tempContent != null && tempContent.length() > 0) {  htmlContent = htmlContent + tempContent; tempContent = breader.readLine(); }//下面的是Field  我找了半天可是没有找到存储object的方法,本想自己写,可是没时间,就把对象切开放Field fieldContents = new Field("content", htmlContent, Field.Store.COMPRESS, Field.Index.TOKENIZED,Field.TermVector.YES); Field fieldTime = new Field("time", article.getUpdateTime().toString(), Field.Store.YES, Field.Index.TOKENIZED,Field.TermVector.YES); Field fieldAuthor = new Field("author", article.getAuthor(), Field.Store.COMPRESS, Field.Index.TOKENIZED, Field.TermVector.YES); Field fieldCategory = new Field("category", article.getCategory().getOutsideName(), Field.Store.COMPRESS, Field.Index.TOKENIZED, Field.TermVector.YES); String path = "/" + article.getCategory().getCategoryUrl()+ "/" + article.getId() + ".html"; Field fieldPath = new Field("path", path, Field.Store.COMPRESS, Field.Index.TOKENIZED, Field.TermVector.YES); doc.add(fieldId); doc.add(fieldPath);doc.add(fieldCategory); doc.add(fieldTime); doc.add(fieldAuthor); doc.add(fieldContents); doc.add(fieldTitles); indexWriter.addDocument(doc);} indexWriter.optimize(); indexWriter.close();} catch (IOException e) { e.printStackTrace();}}到这里索引已经建立了 ,下面要做的就是搜索 public List<Document> searchDoc(String type, String queryString) { List<Document> fileList = new ArrayList<Document>();//其实这里是不需要的,因为lucene默认是调用它的,当然还有另外一个,我这里只是为了下面的高亮显示Analyzer analyzer = new StandardAnalyzer(); try {Directory fsDir = FSDirectory.getDirectory(getPathIndex(), false); IndexSearcher searcher = new IndexSearcher(fsDir);QueryParser queryParse = new QueryParser(type, analyzer);Hits hits = searcher.search(queryParse.parse(queryString));  for (int i = 0; i < hits.length(); i++) { Document doc = hits.doc(i); String value = doc.get(type);//对要高亮显示的字段格式化,我这里只是加红色显示和加粗 SimpleHTMLFormatter sHtmlF = new SimpleHTMLFormatter("<b><font color='red'>", "</font></b>");Highlighter highlighter = new Highlighter(sHtmlF,new QueryScorer(queryParse.parse(queryString))); highlighter.setTextFragmenter(new SimpleFragmenter(100));  if (value != null) {TokenStream tokenStream = analyzer.tokenStream(type,new StringReader(value)); Field tempField = new Field(type, highlighter.getBestFragment(tokenStream, value),Field.Store.NO,Field.Index.TOKENIZED,Field.TermVector.YES); doc.removeField(type);  doc.add(tempField);  }//这里取的是Document 对象哦,里面的东西还需要你自己抽取呵,代码我就不写了   fileList.add(doc);  }  searcher.close();  } catch (IOException e) { e.printStackTrace();  } catch (ParseException e) { e.printStackTrace();  }  return fileList;}OK,这里索引就做好了,当时我给我们前台程序员说,好了,我给你2个方法,你调用吧。以为我轻松了吧,其实没有呢,我只是加了一点必要的存储字段,那个兄弟要求高着呢,最后加了很多,后来还要我用多条件查询(网上应该有这样的教材吧,我后来用的是compass实现的,原理是一样)在这里我好象少了一个东西,呵呵 发现了么?没有么?发现了吧 呵呵 我用的是spring配置 所以没有indexwriter,下面是配置文件<bean id="indexWriter" class="org.apache.lucene.index.IndexWriter">  <constructor-arg index="0" type="java.io.File">   <bean class="java.io.File">    <constructor-arg value="E:/Projects/netSchool/indexDatas" />   </bean>  </constructor-arg>  <constructor-arg index="1" >   <bean class="org.apache.lucene.analysis.standard.StandardAnalyzer" />  </constructor-arg>  <constructor-arg index="2" type="boolean" value="true"/> </bean>好了 不知道还有什么没说的,不懂的留言呵别不厚道!!!!看帖要回帖的!


阅读全文(3234) | 回复(1) | 编辑 | 精华
 


回复:lucene-2.0.0的基本应用
软件技术

JackyLee(游客)发表评论于2006/8/11 15:53:23

呵呵 看了下 先保存 刚开始想不留言的 一看一句经典话 我就留了 :) 继续努力


个人主页 | 引用回复 | 主人回复 | 返回 | 编辑 | 删除
 


» 1 »

发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.330 second(s), page refreshed 144765340 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号