`
忧里修斯
  • 浏览: 428275 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

检索数据库中的数据简单示例

阅读更多
1、检索实体类
package com.dxcollector.entity;

public class DxNovel implements java.io.Serializable {

	private Long id;
	private String pid;
	private String NTitle;
	private String NAuthor;
	private String NDesc;
	private String NType;
	private String NTotalWords;
	private String NImgUrl;
	private String NSourceUrl;
         //省略getter and setter
}


2、索引的建立
package com.dxcollector.search;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version;

import com.dxcollector.entity.DxNovel;
import com.dxcollector.novels.readnovel.NovelMgr;

/**
 * 索引管理
 * 
 * @author 忧里修斯
 */
public class IndexMgr {
	
	NovelMgr novelMgr = new NovelMgr();
	/**
	 * 创建新的索引
	 * 
	 * @param index_store_path 索引存放路径
	 */
	public void createIndex(String index_store_path){
		
		IndexWriter indexWriter = null;
		try {
			
			Directory directory = FSDirectory.open(new File(index_store_path));
			indexWriter = new IndexWriter(directory,new StandardAnalyzer(Version.LUCENE_29),IndexWriter.MaxFieldLength.UNLIMITED);
			List<DxNovel> novelList = novelMgr.getAllNovel();
			System.out.println("大小:"+novelList.size());
			Document doc = null;
			for (DxNovel dxNovel : novelList) {
				
				doc = new Document();
				Field titleField = new Field("NTitle",dxNovel.getNTitle(),Field.Store.YES,Field.Index.ANALYZED);
				Field authorField = new Field("NAuthor",dxNovel.getNAuthor(),Field.Store.YES,Field.Index.NOT_ANALYZED);
				Field descField = new Field("NDesc",dxNovel.getNDesc(),Field.Store.YES,Field.Index.ANALYZED);
				Field typeField = new Field("NType",dxNovel.getNType(),Field.Store.YES,Field.Index.ANALYZED);
				doc.add(titleField);
				doc.add(authorField);
				doc.add(descField);
				doc.add(typeField);
				indexWriter.addDocument(doc);
			}
			indexWriter.optimize();
			indexWriter.close();
			
		} catch (CorruptIndexException e) {
			e.printStackTrace();
		} catch (LockObtainFailedException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("索引建立完成");
	}
	
	public static void main(String[] args) {
		
		IndexMgr im = new IndexMgr();
		im.createIndex("C:/index/");
	}
}


3、搜索
package com.dxcollector.search;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

import com.dxcollector.entity.DxNovel;

/**
 * 搜索管理器
 * 
 * @author 忧里修斯
 *
 */
public class SearchMgr {
	
	//索引文件存放的位置
	private String index_store_path = "C:/index/";
	public SearchMgr(String index_store_path){
		this.index_store_path = index_store_path;
	}
	
	/**
	 * 搜索小说
	 * 
	 * @param searchType 搜索类型,对应小说标题NTitle等
	 * @param keyword 关键字
	 * @return List<DxNovel>
	 */
	public List<DxNovel> search(String searchType,String keyword){
		
		List<DxNovel> novelList = null;
		try {
			
			novelList = new ArrayList<DxNovel>();
			Directory directory = FSDirectory.open(new File(index_store_path));
			IndexSearcher indexSearcher = new IndexSearcher(directory,true);
			Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_29);
			
			//建立搜索单元
			Query query = null;
			QueryParser queryParser = new QueryParser(Version.LUCENE_29,searchType,analyzer); 
			
			query = queryParser.parse(keyword);
			//多域搜索
			if(searchType.equals("")){
				queryParser.setDefaultOperator(QueryParser.OR_OPERATOR);
				String[] fields = {"NTitle", "NAuthor", "NDesc","NType"};
				BooleanClause.Occur[] flags = {BooleanClause.Occur.SHOULD,
				                BooleanClause.Occur.SHOULD,
				                BooleanClause.Occur.SHOULD,
				                BooleanClause.Occur.SHOULD};
				try {
					query = MultiFieldQueryParser.parse(Version.LUCENE_29,keyword, fields, flags, analyzer);
				} catch (ParseException e) {
					e.printStackTrace();
				}
			}
			
			
			//检索
			TopDocs topDocs = indexSearcher.search(query,100);
			ScoreDoc[] hits = topDocs.scoreDocs;
			
			System.out.println("结果集大小:"+hits.length);
			for (int i = 0; i < hits.length; i++) {
				int docId = hits[i].doc;
				Document doc = indexSearcher.doc(docId);
				DxNovel novel = new DxNovel();
				novel.setNTitle(doc.get("NTitle"));
				novel.setNAuthor(doc.get("NAuthor"));
				novel.setNDesc(doc.get("NDesc"));
				novel.setNType(doc.get("NType"));
				novelList.add(novel);
			}
		} catch (CorruptIndexException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return novelList;
	}
	
	public static void main(String[] args) {
		
		SearchMgr searcher = new SearchMgr("C:/index/");
		List<DxNovel> novelList = new ArrayList<DxNovel>();
		novelList = searcher.search("NTitle", "my");
		for (DxNovel dxNovel : novelList) {
			System.out.println("标题:"+dxNovel.getNTitle());
			System.out.println("作者:"+dxNovel.getNAuthor());
			System.out.println("类型:"+dxNovel.getNType());
			System.out.println("简介:"+dxNovel.getNDesc());
		}
	}
}


说明:
1、使用的luncene的版本为lucene-2.9.2
2、需注意的是搜索英文时,单词是以空格分开的。如youger dream,若搜索"ea"是搜索不到的,只有搜索"youger"或"dream"才能搜索得到。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics