Lucene的练习

今天看了看Lucene,其实好早以前就通过买的参考书学习过了。现在又重温一下。

关于Lucene2014年底最新的版本是4.10的。但是4.8+的版本都只支持JDK1.7,4.0的版本是支持JDK1.6的。只是4.0版本所用的中文分词都不太好,所以这里还是介绍一下常用的3.5版本吧。

3.5版本采用的分词器是IKAnalyzer。这个在4.0已经不支持了。

还是先说JAR文件吧,工程中使用的JAR文件:lucene-core-3.5.0.jar、lucene-analyzers-3.5.0.jar、lucene-highlighter-3.5.0.jar、IKAnalyzer3.2.5Stable.jar其中有核心包、分词包和高亮包。

工程中写了两个类,一个是创建索引的类,一个是搜索的类:

创建索引:

package com.zyujie.lucene;

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

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;

public class CreateIndexFile {

	/*
	 * 创建索引
	 */
	public void createIndex() throws Exception {
		String filePath = "D:\\lucene\\cityDemo\\cityIndex";	//索引文件夹目录
		//创建索引之前,先将旧的索引文件清空
		this.deleteIndexFile(filePath);
		File indexFile = new File(filePath);	//索引文件存放的目录
		Analyzer analyzer = new IKAnalyzer();	//IKAnalyzer支持中文分词器
		IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_35, analyzer);	//配置项
		iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);	//模式
		IndexWriter indexWriter = null;
		try {
			indexWriter = new IndexWriter(FSDirectory.open(indexFile), iwc);	//打开索引文件夹
			List cityList = this.getCityDatas();
			for (int i = 0; i < cityList.size(); i++) {
				Document doc = (Document) cityList.get(i);
				indexWriter.addDocument(doc);	//将doc生成的数据添加到indexWriter,写入索引文件
			}
			System.out.println("索引已经创建!");
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			indexWriter.close(); // 需关闭
		}
	}
	
	/*
	 * 删除索引文件
	 */
	public void deleteIndexFile(String indexFilePath){
		File file = new File(indexFilePath);
		String[] filelist = file.list();	//获取下面的所有索引文件
	    for (int i = 0; i < filelist.length; i++) {
	    	File delfile = new File(indexFilePath + "\\" + filelist[i]);
    		delfile.delete();	//删除所有索引文件
	    }
	    System.out.println("已清空索引文件");  
	}
	
	/*
	 * 造数据
	 */
	public List getCityDatas(){
		List cityList = new ArrayList();
		//创建Document对象
		Document bjdoc = new Document();
		bjdoc.add(new Field("id", "010", Store.YES, Index.NOT_ANALYZED));	//Store.YES存入索引文件,Index.NOT_ANALYZED不进行分词
		bjdoc.add(new Field("name", "北京", Store.YES, Index.ANALYZED));
		bjdoc.add(new Field("area", "华北地区", Store.YES, Index.ANALYZED));
		bjdoc.add(new Field("population", "2114.8万", Store.YES, Index.NOT_ANALYZED));
		bjdoc.add(new Field("description", "中华人民共和国的首都、直辖市和国家中心城市,也是中国的政治、文化、科教和国际交往中心", Store.YES, Index.ANALYZED));
		
		Document shdoc = new Document();
		shdoc.add(new Field("id", "021", Store.YES, Index.NOT_ANALYZED));
		shdoc.add(new Field("name", "上海", Store.YES, Index.ANALYZED));
		shdoc.add(new Field("area", "华东地区", Store.YES, Index.ANALYZED));
		shdoc.add(new Field("population", "2500万", Store.YES, Index.NOT_ANALYZED));
		shdoc.add(new Field("description", "上海(Shanghai),简称“沪”,有“东方巴黎”的美称,中华人民共和国直辖市之一", Store.YES, Index.ANALYZED));
		
		Document gzdoc = new Document();
		gzdoc.add(new Field("id", "020", Store.YES, Index.NOT_ANALYZED));
		gzdoc.add(new Field("name", "广州", Store.YES, Index.ANALYZED));
		gzdoc.add(new Field("area", "华南地区", Store.YES, Index.ANALYZED));
		gzdoc.add(new Field("population", "1292.68万", Store.YES, Index.NOT_ANALYZED));
		gzdoc.add(new Field("description", "广州,简称穗,别称羊城、花城,广东省省会,位于广东省中南部", Store.YES, Index.ANALYZED));
		
		Document tjdoc = new Document();
		tjdoc.add(new Field("id", "022", Store.YES, Index.NOT_ANALYZED));
		tjdoc.add(new Field("name", "天津", Store.YES, Index.ANALYZED));
		tjdoc.add(new Field("area", "华北地区", Store.YES, Index.ANALYZED));
		tjdoc.add(new Field("population", "11946.88万", Store.YES, Index.NOT_ANALYZED));
		tjdoc.add(new Field("description", "天津,简称津,中华人民共和国直辖市、中国国家中心城市、中国北方经济中心", Store.YES, Index.ANALYZED));
		
		Document cddoc = new Document();
		cddoc.add(new Field("id", "028", Store.YES, Index.NOT_ANALYZED));
		cddoc.add(new Field("name", "成都", Store.YES, Index.ANALYZED));
		cddoc.add(new Field("area", "西南地区", Store.YES, Index.ANALYZED));
		cddoc.add(new Field("population", "1417万", Store.YES, Index.NOT_ANALYZED));
		cddoc.add(new Field("description", "成都,四川省省会[1] ,成都位于中国华西地区东部,成都平原腹地,古为蜀国地", Store.YES, Index.ANALYZED));
		
		Document cqdoc = new Document();
		cqdoc.add(new Field("id", "023", Store.YES, Index.NOT_ANALYZED));
		cqdoc.add(new Field("name", "重庆", Store.YES, Index.ANALYZED));
		cqdoc.add(new Field("area", "西南地区", Store.YES, Index.ANALYZED));
		cqdoc.add(new Field("population", "2970万", Store.YES, Index.NOT_ANALYZED));
		cqdoc.add(new Field("description", "重庆,简称巴和渝,别称山城、渝都、桥都,中华人民共和国直辖市", Store.YES, Index.ANALYZED));
		
		cityList.add(bjdoc);
		cityList.add(shdoc);
		cityList.add(gzdoc);
		cityList.add(tjdoc);
		cityList.add(cddoc);
		cityList.add(cqdoc);
		return cityList;
	} 
}
搜索类:

package com.zyujie.lucene;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.QueryParser;
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.search.highlight.Formatter;
import org.apache.lucene.search.highlight.Fragmenter;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.Scorer;
import org.apache.lucene.search.highlight.SimpleFragmenter;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;
import org.wltea.analyzer.lucene.IKSimilarity;

public class SearchCity {

	public void searchCityInfo(String queryStr){
		String filePath = "D:\\lucene\\cityDemo\\cityIndex";	//索引文件夹目录
		String[] fields = { "id", "name", "area", "description" };	//允许搜索的字段
		
		File indexFile = new File(filePath);	//索引文件存放的目录
		Analyzer analyzer = new IKAnalyzer();	//IKAnalyzer支持中文分词器
		
		IndexReader indexReader = null;
		IndexSearcher indexSearcher = null;
		try {
			QueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_35, fields, analyzer);	//构造查询器用来搜索多个字段
                        //PrefixQuery prefixQuery = new PrefixQuery(new Term("name","成")); //与查询前缀匹配的查询,suggest功能
                        Query query = queryParser.parse(queryStr);
			indexReader = IndexReader.open(FSDirectory.open(indexFile));	//indexReader读取索引目录
			indexSearcher = new IndexSearcher(indexReader);	//实例化搜索对象
			indexSearcher.setSimilarity(new IKSimilarity());	//设置匹配实现

			TopDocs docs = indexSearcher.search(query, 10);	//搜索返回的条目数,关键词包括最多的前10条
			int totalHits = docs.totalHits;	//匹配的结果总数
			System.out.println("total : " + totalHits);

			//为允许搜索的字段,设置高亮,html标签
			Formatter formatter = new SimpleHTMLFormatter("<font color='red'>","</font>");
			Scorer scorer = new QueryScorer(query);
			Highlighter highlighter = new Highlighter(formatter, scorer);
			
			//复杂的很长的文本字段,需要截取部分来显示,设置片断边界大小,可以保证分词的完整性
			Fragmenter fragmenter = new SimpleFragmenter(50);
			highlighter.setTextFragmenter(fragmenter);
			
			for (ScoreDoc doc : docs.scoreDocs) {
				Document document = indexSearcher.doc(doc.doc);	//从符合结果的集合中取出每个文档对象
				
				System.out.println("城市区号:-->" + document.get("id"));	//通过key得到值
				//返回高亮后的结果,如果当前属性值没有出现关键字则出现null
				String hc_name = highlighter.getBestFragment(analyzer, "name", document.get("name"));	//需要高亮显示的字段
				if (hc_name == null) {
					String description = document.get("name");
					int endIndex = Math.min(50, description.length());	//返回较小的值
					hc_name = description.substring(0, endIndex);// 最多前50个字符
				}
				System.out.println("城市名称:-->" + hc_name);
				System.out.println("所在区域:-->" + document.get("area"));
				System.out.println("人口:-->" + document.get("population"));
//				System.out.println("简介:-->" + document.get("description"));
				
				//返回高亮后的结果,如果当前属性值没有出现关键字则出现null
				String hc_description = highlighter.getBestFragment(analyzer, "description", document.get("description"));
				if (hc_description == null) {
					String description = document.get("description");
					int endIndex = Math.min(50, description.length());
					hc_description = description.substring(0, endIndex);
				}
				System.out.println("简介:-->" + hc_description);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 使用完毕需要关闭!
			try {
				indexReader.close();
				indexSearcher.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
写了一个main方法去调用:

public static void main(String[] args) throws Exception {
//		CreateIndexFile cif = new CreateIndexFile();
//		cif.createIndex();
		
		SearchCity sc = new SearchCity();
		sc.searchCityInfo("上海");
	}
运行结果:

total : 1
城市区号:-->021
城市名称:--><font color='red'>上海</font>
所在区域:-->华东地区
人口:-->2500万
简介:--><font color='red'>上海</font>(Shanghai),简称“沪”,有“东方巴黎”的美称,中华人民共和国直辖市之一
以上只是Lucene3.5简单的应用,还有一些更多功能,可参见demo和api。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值