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

Lucene實戰開發手記(六)--- 搜索、刪除索引代碼

阅读更多

無特別之處,了解的朋友不用看。

	
	/**
	 * 限制分類的搜索
	 * @param keyword
	 * @param categoryKeyword
	 * @return
	 * @throws LuceneException
	 */
	public  List searchIndex(String keyword, String categoryKeyword) throws LuceneException{
		Analyzer analyzer = new CJKAnalyzer();
		
		BooleanQuery query = new BooleanQuery();
		BooleanClause.Occur[] flags = new BooleanClause.Occur[]{BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD};
		try {
			Query multiQuery = MultiFieldQueryParser.parse(keyword, new String[] {
			        "topic", "contents" }, flags, analyzer);
			query.add(multiQuery, BooleanClause.Occur.MUST);
			QueryParser parse = new QueryParser("categoryId",analyzer);
			query.add(parse.parse(categoryKeyword), BooleanClause.Occur.MUST);//注意QueryParser是個很聰明的家伙,能自動判別你想要的Query的具體類型
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();			
			throw new LuceneException();
		}
		return searchIndex(query);
	}
	
	/**
	 * 搜索的實際執行方法,未做分頁
	 * @param query
	 * @return
	 * @throws LuceneException
	 */
	public  List searchIndex(Query query) throws LuceneException{
		
		List<KbIndexModel> resultList = new ArrayList<KbIndexModel>();
		File indexDir = new File(INDEX_STORE_PATH);
		Analyzer analyzer = new CJKAnalyzer();
		IndexSearcher indexSearcher;
		
		if (indexDir.exists() == false){
			return resultList;
		}
		
		try {
			indexSearcher = new IndexSearcher(IndexReader.open(indexDir));		
	        System.out.println("\nQuery:"+query.toString()+"--------------------");		

			Hits hits = indexSearcher.search(query);
			
			//高亮顯示設置
			SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter(
	                "<span class=\"searchcontent\">", "</span>");
			Highlighter highlighter = new Highlighter(simpleHTMLFormatter,new QueryScorer(query));
			highlighter.setTextFragmenter(new SimpleFragmenter(CONTENTS_SHOW_LENGTH));
			
			
			for (int i = 0; i < hits.length() && i < MAX_RESULT; i++)
			{
				Document doc = hits.doc(i);
				KbIndexModel resultModel = new KbIndexModel();
				resultModel.setId(doc.get("id"));
				resultModel.setArticleId(doc.get("articleId"));
				resultModel.setFileName(doc.get("fileName"));				
				resultModel.setCategory(doc.get("category"));
				
				String topic = doc.get("topic");
				//高亮顯示
				TokenStream tokenStream = analyzer.tokenStream("topic", new StringReader(topic));
				String topic2 = highlighter.getBestFragment(tokenStream, topic);
				if (topic2 == null){
					topic2 = topic;
				}
				resultModel.setTopic(topic2);				
				//後面的代碼無關緊要
				String contents = hits.doc(i).get("contents");				
				
				tokenStream = analyzer.tokenStream("contents", new StringReader(contents));
				String contents2 = highlighter.getBestFragment(tokenStream, contents);
				
				if(contents2 == null){
					if (contents == null){
						contents2 = "";
					}
					else if (contents.length() > CONTENTS_SHOW_LENGTH){
						contents2 = contents.substring(0, CONTENTS_SHOW_LENGTH);
					}	
					else{
						contents2 = contents;
					}					
				}		
				
				resultModel.setContents(contents2 + "...");

				resultModel.setIndex(i+1);
				resultList.add(resultModel);
				indexSearcher.close();
			}		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new LuceneException();
		} 
		return resultList;
	}
	
	/*
	 * 依ID刪除索引
	 */
	public void delIndex(String id) throws LuceneException {
		IndexReader indexReader;
		try {
			File indexDir = new File(INDEX_STORE_PATH);
			if (indexDir.exists()){
				indexReader = IndexReader.open(INDEX_STORE_PATH);		
				Term term = new Term("id", id);		
				indexReader.deleteDocuments(term);
				indexReader.close();IndexWriter indexWriter = new IndexWriter(INDEX_STORE_PATH, new CJKAnalyzer(), false);
    indexWriter.optimize();
    indexWriter.close();
			}			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new LuceneException();
		}
	}
	

 

分享到:
评论

相关推荐

    lucene-analyzers-smartcn-7.7.0-API文档-中文版.zip

    赠送jar包:lucene-analyzers-smartcn-7.7.0.jar; 赠送原API文档:lucene-analyzers-smartcn-7.7.0-javadoc.jar; 赠送源代码:lucene-analyzers-smartcn-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-...

    lucene-analyzers-common-6.6.0-API文档-中文版.zip

    赠送jar包:lucene-analyzers-common-6.6.0.jar; 赠送原API文档:lucene-analyzers-common-6.6.0-javadoc.jar; 赠送源代码:lucene-analyzers-common-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-...

    lucene-core-7.7.0-API文档-中文版.zip

    赠送jar包:lucene-core-7.7.0.jar; 赠送原API文档:lucene-core-7.7.0-javadoc.jar; 赠送源代码:lucene-core-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-core-7.7.0.pom; 包含翻译后的API文档:lucene...

    lucene-backward-codecs-7.3.1-API文档-中英对照版.zip

    赠送jar包:lucene-backward-codecs-7.3.1.jar; 赠送原API文档:lucene-backward-codecs-7.3.1-javadoc.jar; 赠送源代码:lucene-backward-codecs-7.3.1-sources.jar; 赠送Maven依赖信息文件:lucene-backward-...

    lucene-core-7.2.1-API文档-中文版.zip

    赠送jar包:lucene-core-7.2.1.jar; 赠送原API文档:lucene-core-7.2.1-javadoc.jar; 赠送源代码:lucene-core-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-core-7.2.1.pom; 包含翻译后的API文档:lucene...

    lucene-spatial-extras-7.3.1-API文档-中英对照版.zip

    赠送jar包:lucene-spatial-extras-7.3.1.jar; 赠送原API文档:lucene-spatial-extras-7.3.1-javadoc.jar; 赠送源代码:lucene-spatial-extras-7.3.1-sources.jar; 赠送Maven依赖信息文件:lucene-spatial-extras...

    lucene-analyzers-smartcn-7.7.0-API文档-中英对照版.zip

    赠送jar包:lucene-analyzers-smartcn-7.7.0.jar; 赠送原API文档:lucene-analyzers-smartcn-7.7.0-javadoc.jar; 赠送源代码:lucene-analyzers-smartcn-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-...

    lucene-spatial-extras-7.2.1-API文档-中英对照版.zip

    赠送jar包:lucene-spatial-extras-7.2.1.jar; 赠送原API文档:lucene-spatial-extras-7.2.1-javadoc.jar; 赠送源代码:lucene-spatial-extras-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-spatial-extras...

    lucene-spatial-extras-6.6.0-API文档-中英对照版.zip

    赠送jar包:lucene-spatial-extras-6.6.0.jar; 赠送原API文档:lucene-spatial-extras-6.6.0-javadoc.jar; 赠送源代码:lucene-spatial-extras-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-spatial-extras...

    lucene-backward-codecs-7.2.1-API文档-中英对照版.zip

    赠送jar包:lucene-backward-codecs-7.2.1.jar; 赠送原API文档:lucene-backward-codecs-7.2.1-javadoc.jar; 赠送源代码:lucene-backward-codecs-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-backward-...

    lucene-backward-codecs-6.6.0-API文档-中英对照版.zip

    赠送jar包:lucene-backward-codecs-6.6.0.jar; 赠送原API文档:lucene-backward-codecs-6.6.0-javadoc.jar; 赠送源代码:lucene-backward-codecs-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-backward-...

    lucene-backward-codecs-6.6.0-API文档-中文版.zip

    赠送jar包:lucene-backward-codecs-6.6.0.jar; 赠送原API文档:lucene-backward-codecs-6.6.0-javadoc.jar; 赠送源代码:lucene-backward-codecs-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-backward-...

    lucene-suggest-6.6.0-API文档-中文版.zip

    赠送jar包:lucene-suggest-6.6.0.jar; 赠送原API文档:lucene-suggest-6.6.0-javadoc.jar; 赠送源代码:lucene-suggest-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-suggest-6.6.0.pom; 包含翻译后的API...

    lucene-highlighter-6.6.0-API文档-中文版.zip

    赠送jar包:lucene-highlighter-6.6.0.jar; 赠送原API文档:lucene-highlighter-6.6.0-javadoc.jar; 赠送源代码:lucene-highlighter-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-highlighter-6.6.0.pom;...

    lucene-core-6.6.0-API文档-中文版.zip

    赠送jar包:lucene-core-6.6.0.jar; 赠送原API文档:lucene-core-6.6.0-javadoc.jar; 赠送源代码:lucene-core-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-core-6.6.0.pom; 包含翻译后的API文档:lucene...

    lucene-sandbox-7.2.1-API文档-中文版.zip

    赠送jar包:lucene-sandbox-7.2.1.jar; 赠送原API文档:lucene-sandbox-7.2.1-javadoc.jar; 赠送源代码:lucene-sandbox-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-sandbox-7.2.1.pom; 包含翻译后的API...

    lucene-memory-6.6.0-API文档-中文版.zip

    赠送jar包:lucene-memory-6.6.0.jar; 赠送原API文档:lucene-memory-6.6.0-javadoc.jar; 赠送源代码:lucene-memory-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-memory-6.6.0.pom; 包含翻译后的API文档...

    lucene-suggest-7.7.0-API文档-中文版.zip

    赠送jar包:lucene-suggest-7.7.0.jar; 赠送原API文档:lucene-suggest-7.7.0-javadoc.jar; 赠送源代码:lucene-suggest-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-suggest-7.7.0.pom; 包含翻译后的API...

    lucene-core-6.6.0-API文档-中英对照版.zip

    赠送jar包:lucene-core-6.6.0.jar; 赠送原API文档:lucene-core-6.6.0-javadoc.jar; 赠送源代码:lucene-core-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-core-6.6.0.pom; 包含翻译后的API文档:lucene...

    lucene-spatial-6.6.0-API文档-中文版.zip

    赠送jar包:lucene-spatial-6.6.0.jar; 赠送原API文档:lucene-spatial-6.6.0-javadoc.jar; 赠送源代码:lucene-spatial-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-spatial-6.6.0.pom; 包含翻译后的API...

Global site tag (gtag.js) - Google Analytics