`
out_println
  • 浏览: 14134 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

lucene应用摘要

阅读更多
Lucene做为一个开源的搜索引擎框架,它不但易于学习,而且为开发人员提供了丰富的API来完成用为开发人员不同的需求,下面是Lucene一些主要应用的代码:
建索引:
try{
			//将索引写入文件,标准分词器,field长度为有限的
			writer = new IndexWriter(FSDirectory.open(index),
					new StandardAnalyzer(Version.LUCENE_CURRENT), create,
					IndexWriter.MaxFieldLength.LIMITED);
			
			if(obj!=null){
				List list=(List)obj;
				for(int i=0;i<list.size();i++){
					doc = new Document();
					articleInfo =(ArticleInfo)list.get(i);
					
					Field id= new Field("id",String.valueOf(articleInfo.getId()),
							Field.Store.YES,Field.Index.ANALYZED);  //保存id值,并为其建立索引
					Field title = new Field("title",articleInfo.getTitle() ,
							Field.Store.YES, Field.Index.ANALYZED); //保存内容,并为其建立索引
					Field context = new Field("contents", articleInfo.getContents(),
							Field.Store.YES, Field.Index.ANALYZED); //保存内容,并为其建立索引
					Field url = new Field("url", articleInfo.getPath(), Field.Store.YES,
							Field.Index.NO);                                             //保存内容,不建立索引
					
					doc.add(id);
					doc.add(title);
					doc.add(context);
					doc.add(url);
					//写入索引文件
					writer.addDocument(doc);
				}
			}
			writer.optimize();
			writer.close();
		}catch(Exception ex){
			ex.printStackTrace();
		}


更新索引:
try {
				//将索引写入文件,标准分词器,field长度为有限的,create=false
				writer = new IndexWriter(FSDirectory.open(index),
						new StandardAnalyzer(Version.LUCENE_CURRENT), false,
						IndexWriter.MaxFieldLength.LIMITED);
				if(obj!=null){
					list = (List)obj;
					for(int i=0;i<list.size();i++){
						articleInfo = (ArticleInfo)list.get(i);
						System.out.println("update id is :"+articleInfo.getId());
						term = new Term("id",String.valueOf(articleInfo.getId()));
						
						Field id= new Field("id",String.valueOf(articleInfo.getId()),
								Field.Store.YES,Field.Index.ANALYZED);   //保存id值,并为其建立索引
						Field title = new Field("title",articleInfo.getTitle() ,
								Field.Store.YES, Field.Index.ANALYZED);    //保存内容,并为其建立索引
						Field context = new Field("contents", articleInfo.getContents(),
								Field.Store.YES, Field.Index.ANALYZED);     //保存内容,并为其建立索引
						Field url = new Field("url", articleInfo.getPath(), Field.Store.YES,Field.Index.NO);                                           //保存内容,不建立索引
						
						doc = new Document();
						doc.add(id);
						doc.add(title);
						doc.add(context);
						doc.add(url);
						//更新索引
						writer.updateDocument(term, doc);
//						writer.addDocument(doc);
						
					}
				}
				writer.optimize();
				writer.close();
			}catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}


删除索引:
try {
				Directory directory=FSDirectory.open(index);
				//该文件夹下是否存在索引文件
				if(IndexReader.indexExists(directory)){
					//因为要执行删除操作,所以read-only=false
					reader = IndexReader.open(directory, false);
					//判断是否最新的索引信息
					if(!reader.isCurrent()){
						//如果不是,重新获取索引信息
						reader.reopen();
					}
					if(obj!=null){
						list = (List)obj;
						for(int i=0;i<list.size();i++){
							info=(ArticleInfo)list.get(i);
							System.out.println("id is: "+info.getId());
							term = new Term("id", String.valueOf(info.getId()));
							//根据doc序号删除索引
//							reader.deleteDocument(1);
							//根据id删除索引
							reader.deleteDocuments(term);
						}
					}
				}
				reader.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}


最后,实现对索引的搜索:
			Directory directory= FSDirectory.open(new File(indexFile));
			//判断文件夹下是否存在索引文件
			if(IndexReader.indexExists(directory)){
				//索引存在
				indexReader = IndexReader.open(directory, true);
				//判断是否为最新索引
				if(!indexReader.isCurrent()){
					//获取最新索引
					indexReader.reopen();
				}
				
				Searcher searcher = new IndexSearcher(indexReader);
				//创建标准分词器
				Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
				//复合字段查询
				MultiFieldQueryParser parser = new MultiFieldQueryParser(fieldNames,analyzer);
				//查询关键字解析
				Query query = parser.parse(queryStr);
				//查询前100条记录
				TopDocs docs =searcher.search(query, count);
				ScoreDoc[] score = docs.scoreDocs;
				//关键字高亮显示
			    SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter("<font color='red'>", "</font>");
			    Highlighter highlighter = new Highlighter(simpleHTMLFormatter,new QueryScorer(query));
			    //截取含有关键字的文本片段,
			    highlighter.setTextFragmenter(new SimpleFragmenter(50));
			    System.out.println("length is: "+score.length);
			    if(score.length>0){
			    	list = new ArrayList<ArticleInfo>();
			    	for(int i=0;i<score.length;i++){
			    		doc = searcher.doc(score[i].doc);
			    		
			    		sum = highlighter.getBestFragment(analyzer, "contents", doc.get("contents"));
			    		if(sum==null || "".equals(sum)){
			    			sum = highlighter.getBestFragment(analyzer, "title", doc.get("title"));
			    		}
			    		System.out.println("sum is: "+sum);
			    		articleInfo = new ArticleInfo();
			    		articleInfo.setId(Integer.parseInt(doc.get("id")));
			    		articleInfo.setTitle(doc.get("title"));
			    		articleInfo.setContents(sum);
			    		articleInfo.setPath(doc.get("url"));
			    		list.add(articleInfo);
			    	}
			    }
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
分享到:
评论
3 楼 cf2huihui 2013-03-02  
不错,不错
2 楼 out_println 2012-05-26  
u_lie 写道
大哥~! 搜索部分上面的的代码怎么写的啊,能分享下吗~!谢谢

代码都在啊 ,建索引,更新索引,删除索引,最后搜索的代码都有啊
1 楼 u_lie 2012-05-16  
大哥~! 搜索部分上面的的代码怎么写的啊,能分享下吗~!谢谢

相关推荐

Global site tag (gtag.js) - Google Analytics