增删改查索引库

2/22/2017来源:ASP.NET技巧人气:2873

util

public class LuceneUtil {
	
	PRivate static Directory directory;
	private static Version version;
	private static Analyzer analyzer;
	private static MaxFieldLength maxFieldLength;
	
	static{
		try {
			directory =  FSDirectory.open(new File("c:/itcast/IndexDBDBDB"));
			version = Version.LUCENE_30;
			analyzer = new StandardAnalyzer(version);
			maxFieldLength = MaxFieldLength.LIMITED;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	
	public static Directory getDirectory() {
		return directory;
	}

	public static Version getVersion() {
		return version;
	}

	public static Analyzer getAnalyzer() {
		return analyzer;
	}

	public static MaxFieldLength getMaxFieldLength() {
		return maxFieldLength;
	}

	//不让外界new该帮助类
	private LuceneUtil(){}	
	
	//将javaBean转成Document对象
	public static Document javabean2document(Object obj) throws Exception{
		//创建Docuemnt对象
		Document document = new Document();
		//获取obj引用的对象字节码
		Class clazz = obj.getClass();
		//通过对象字节码获取私有的属性
		java.lang.reflect.Field[] reflectFields = clazz.getDeclaredFields();
		//迭代
		for(java.lang.reflect.Field reflectField : reflectFields){
			//强力反射
			reflectField.setaccessible(true);
			//获取属性名,id/title/content
			String name = reflectField.getName();
			//人工拼接方法名
			String methodName = "get" + name.substring(0,1).toUpperCase()+name.substring(1);
			//获取方法,例如:getId()/getTitle()/getContent()
			Method method = clazz.getMethod(methodName,null);
			//执行方法
			String value = method.invoke(obj,null).toString();
			//加入到Document对象中去,这时javabean的属性与document对象的属性相同
			document.add(new Field(name,value,Store.YES,Index.ANALYZED));
		}
		//返回document对象
		return document;
	}
	
	//将Document对象转成JavaBean对象
	public static Object document2javabean(Document document,Class clazz) throws Exception{
		Object obj = clazz.newInstance();
		java.lang.reflect.Field[] reflectFields = clazz.getDeclaredFields();
		for(java.lang.reflect.Field reflectField : reflectFields){
			reflectField.setAccessible(true);
			String name = reflectField.getName();//id/title/content
			String value = document.get(name);//1/培训/传智是一家培训机构
			BeanUtils.setProperty(obj,name,value);//封装javabean对应的属性中去,通过setXxx()方法
		}
		return obj;
	}curd

public class ArticleDao {
	@Test
	public void add() throws Exception{
		Article article = new Article(1,"培训","中大是一家java培训机构");
		Document document = LuceneUtil.javabean2document(article);
		IndexWriter indexWriter = new IndexWriter(LuceneUtil.getDirectory(),LuceneUtil.getAnalyzer(),LuceneUtil.getMaxFieldLength());
		indexWriter.addDocument(document);//核心
		indexWriter.close();
	}
	@Test
	public void addAll() throws Exception{
		IndexWriter indexWriter = new IndexWriter(LuceneUtil.getDirectory(),LuceneUtil.getAnalyzer(),LuceneUtil.getMaxFieldLength());
		
		Article article1 = new Article(1,"培训","火星是一家java培训机构");
		Document document1 = LuceneUtil.javabean2document(article1);
		indexWriter.addDocument(document1);
		
		Article article2 = new Article(2,"培训","水星是一家net培训机构");
		Document document2 = LuceneUtil.javabean2document(article2);
		indexWriter.addDocument(document2);
		
		Article article3 = new Article(3,"培训","金星是一家php培训机构");
		Document document3 = LuceneUtil.javabean2document(article3);
		indexWriter.addDocument(document3);
		
		Article article4 = new Article(4,"培训","土星是一家ios培训机构");
		Document document4 = LuceneUtil.javabean2document(article4);
		indexWriter.addDocument(document4);
		
		Article article5 = new Article(5,"培训","木星是一家ui培训机构");
		Document document5 = LuceneUtil.javabean2document(article5);
		indexWriter.addDocument(document5);
		
		Article article6 = new Article(6,"培训","天王星是一家c++培训机构");
		Document document6 = LuceneUtil.javabean2document(article6);
		indexWriter.addDocument(document6);
		
		Article article7 = new Article(7,"培训","海王星是一家SEO培训机构");
		Document document7 = LuceneUtil.javabean2document(article7);
		indexWriter.addDocument(document7);
		
		indexWriter.close();
	}
	@Test
	public void update() throws Exception{
		Article newArticle = new Article(1,"培训","冥王星是一家JAVA培训机构");
		Document document = LuceneUtil.javabean2document(newArticle);
		IndexWriter indexWriter = new IndexWriter(LuceneUtil.getDirectory(),LuceneUtil.getAnalyzer(),LuceneUtil.getMaxFieldLength());
		//更新id=7的document对象
		/*
		 * 参数一:term表示需要更新的document对象,id表示document对象中的id属性,7表示该id属性的值
		 * 参数二:新的document对象
		 */
		indexWriter.updateDocument(new Term("id","1"),document);//核心
		indexWriter.close();
	}
	@Test
	public void delete() throws Exception{
		IndexWriter indexWriter = new IndexWriter(LuceneUtil.getDirectory(),LuceneUtil.getAnalyzer(),LuceneUtil.getMaxFieldLength());
		indexWriter.deleteDocuments(new Term("id","2"));//核心
		indexWriter.close();
	}
	@Test
	public void deleteAll() throws Exception{
		IndexWriter indexWriter = new IndexWriter(LuceneUtil.getDirectory(),LuceneUtil.getAnalyzer(),LuceneUtil.getMaxFieldLength());
		indexWriter.deleteAll();//核心 
		indexWriter.close();
	}
	
		
	
	@Test
	public void findAllByKeyWords() throws Exception{
		String keywords = "培";
		List<Article> articleList = new ArrayList<Article>();
		QueryParser queryParser = new QueryParser(LuceneUtil.getVersion(),"content",LuceneUtil.getAnalyzer());
		Query query = queryParser.parse(keywords);
		IndexSearcher indexSearcher = new IndexSearcher(LuceneUtil.getDirectory());
		TopDocs topDocs = indexSearcher.search(query,100);//核心
		for(int i=0;i<topDocs.scoreDocs.length;i++){
			ScoreDoc scoreDoc = topDocs.scoreDocs[i];
			int no = scoreDoc.doc;
			Document document = indexSearcher.doc(no);
			Article article = (Article)LuceneUtil.document2javabean(document,Article.class);
			articleList.add(article);
		}
		for(Article a : articleList){
			System.out.println( a );
		}
	}
}