- import java.io.File;
- import java.io.IOException;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import org.apache.lucene.analysis.standard.StandardAnalyzer;
- import org.apache.lucene.document.Document;
- import org.apache.lucene.document.Field;
- import org.apache.lucene.document.NumericField;
- import org.apache.lucene.index.CorruptIndexException;
- import org.apache.lucene.index.IndexReader;
- import org.apache.lucene.index.IndexWriter;
- import org.apache.lucene.index.IndexWriterConfig;
- import org.apache.lucene.index.Term;
- import org.apache.lucene.search.BooleanQuery;
- import org.apache.lucene.search.Filter;
- import org.apache.lucene.search.FuzzyQuery;
- import org.apache.lucene.search.IndexSearcher;
- import org.apache.lucene.search.NumericRangeQuery;
- import org.apache.lucene.search.PhraseQuery;
- import org.apache.lucene.search.PrefixQuery;
- import org.apache.lucene.search.Query;
- import org.apache.lucene.search.ScoreDoc;
- import org.apache.lucene.search.Sort;
- import org.apache.lucene.search.TermQuery;
- import org.apache.lucene.search.TermRangeQuery;
- import org.apache.lucene.search.TopDocs;
- import org.apache.lucene.search.WildcardQuery;
- import org.apache.lucene.search.BooleanClause.Occur;
- import org.apache.lucene.store.Directory;
- import org.apache.lucene.store.FSDirectory;
- import org.apache.lucene.store.LockObtainFailedException;
- import org.apache.lucene.util.Version;
- public class SearchUtil {
- private String[] ids = {"1","2","3","4","5","6"};
- private String[] emails = {"soukenan@qq.com","li@soukenan.com","804632564@qq.com","admin@qq.com","soukenan@kenan.org","123@df.com"};
- private String[] content ={
- "Welcome to Kenan my home",
- "Hello Kenan ",
- "Good morning",
- "Are you OK?",
- "Yeah hahahahahahaha Kenan",
- "I like foot ball"
- };
- private int[] attachs = {1,4,6,2,3,8};
- private Date[] dates = null;
- private String[] names = {"5555","333","44","111","222","666"};
- //词典
- private Directory directory = null;
- //写入笔
- private IndexWriter writer = null;
- //文档对象
- private Document doc = null;
- //读取对象
- private IndexReader reader = null;
- private IndexSearcher searcher = null;
- private void datesInit() {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- dates = new Date[6];
- try {
- dates[0] = sdf.parse("2010-12-01");
- dates[1] = sdf.parse("2011-12-01");
- dates[2] = sdf.parse("2001-12-01");
- dates[3] = sdf.parse("2013-12-01");
- dates[4] = sdf.parse("2003-12-01");
- dates[5] = sdf.parse("2014-12-01");
- } catch (ParseException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public SearchUtil(){
- datesInit();
- try {
- directory = FSDirectory.open(new File("d:/lucene/index01"));
- this.getSearcher();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- /**
- * 构建索引
- */
- public void buildIndex(){
- try {
- writer = new IndexWriter(directory,new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
- for(int i=0;i<6;i++){
- doc = new Document();
- doc.add(new Field ("id",ids[i],Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));
- doc.add(new Field("email",emails[i],Field.Store.YES,Field.Index.NOT_ANALYZED));
- doc.add(new Field("content",this.content[i],Field.Store.NO,Field.Index.ANALYZED));
- doc.add(new Field("name",this.names[i],Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));
- //给数字加索引
- doc.add(new NumericField("attach", Field.Store.YES,true).setIntValue(attachs[i]));
- //给日期加索引
- doc.add(new NumericField("date",Field.Store.YES,true).setLongValue(dates[i].getTime()));
- writer.addDocument(doc);
- }
- } catch (CorruptIndexException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (LockObtainFailedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }finally{
- if(writer != null){
- try {
- writer.close();
- } catch (CorruptIndexException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- public IndexSearcher getSearcher(){
- try {
- if(reader == null){
- reader = IndexReader.open(directory);
- }else{
- IndexReader ir = IndexReader.openIfChanged(reader);
- if(ir!=null){
- reader.close();
- reader = ir;
- }
- }
- searcher = new IndexSearcher(reader);
- return searcher;
- } catch (CorruptIndexException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return null;
- }
- public void queryParse(Query query , int num ,Sort sort){
- try {
- TopDocs tds = null;
- if(sort!=null){
- tds = this.searcher.search(query, num,sort);
- }else{
- tds = this.searcher.search(query, num);
- }
- System.out.println("查询到的结果数:"+tds.totalHits);
- for(ScoreDoc sd : tds.scoreDocs){
- doc = this.searcher.doc(sd.doc);
- System.out.println("id:"+doc.get("id")+"---"+"评分:"+sd.score+"--"+"name:"+doc.get("name")+"---"+"attachs:"+doc.get("attach")+"---"+"email:"+doc.get("email"));
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } finally {
- try {
- this.searcher.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- public void queryByFilter(Query query , int num ,Filter filter){
- try {
- TopDocs tds = null;
- if(filter!=null){
- tds = this.searcher.search(query, filter, num);
- }else{
- tds = this.searcher.search(query, num);
- }
- System.out.println("查询到的结果数:"+tds.totalHits);
- for(ScoreDoc sd : tds.scoreDocs){
- doc = this.searcher.doc(sd.doc);
- System.out.println("id:"+doc.get("id")+"---"+"评分:"+sd.score+"--"+"name:"+doc.get("name")+"---"+"attachs:"+doc.get("attach")+"---"+"email:"+doc.get("email"));
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } finally {
- try {
- this.searcher.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- import org.apache.lucene.analysis.standard.StandardAnalyzer;
- import org.apache.lucene.index.Term;
- import org.apache.lucene.queryParser.ParseException;
- import org.apache.lucene.queryParser.QueryParser;
- import org.apache.lucene.queryParser.QueryParser.Operator;
- import org.apache.lucene.search.Filter;
- import org.apache.lucene.search.NumericRangeFilter;
- import org.apache.lucene.search.Query;
- import org.apache.lucene.search.QueryWrapperFilter;
- import org.apache.lucene.search.Sort;
- import org.apache.lucene.search.SortField;
- import org.apache.lucene.search.TermRangeFilter;
- import org.apache.lucene.search.WildcardQuery;
- import org.apache.lucene.util.Version;
- import org.junit.BeforeClass;
- import org.junit.Test;
- public class TestSearchUtil {
- static SearchUtil su ;
- @BeforeClass
- public static void setUpBeforeClass() throws Exception {
- su = new SearchUtil();
- }
- @Test
- public void testBuildIndex(){
- su.buildIndex();
- }
- @Test
- public void testSort() throws Exception{
- //创建QueryParser对象 默认的搜索域为content
- QueryParser parser = new QueryParser(Version.LUCENE_35, "content", new StandardAnalyzer(Version.LUCENE_35));
- //设置 空格的默认操作符为 AND 默认为OR
- // parser.setDefaultOperator(Operator.AND);
- //开启第一个字符的通配符配置
- parser.setAllowLeadingWildcard(true);
- //搜索content中包含kenan的
- Query query = parser.parse("kenan");
- //默认是按照评分排序
- su.queryParse(query, 10,null);
- //按照评分排序
- su.queryParse(query, 10, Sort.RELEVANCE);
- //按照索引排序 默认id来排序
- su.queryParse(query, 10, Sort.INDEXORDER);
- //按照attach的大小排序 升序
- su.queryParse(query, 10, new Sort(new SortField("attach", SortField.INT)));
- //按照attach的大小排序 降序
- su.queryParse(query, 10, new Sort(new SortField("attach", SortField.INT,true)));
- //按照name排序 升序
- su.queryParse(query, 10, new Sort(new SortField("name", SortField.STRING)));
- //按照name排序 降序
- su.queryParse(query, 10, new Sort(new SortField("name", SortField.STRING,true)));
- //按照评分排序
- su.queryParse(query, 10, Sort.RELEVANCE);
- //先按照评分排序 评分相同的,按照attach排序 降序
- su.queryParse(query, 10, new Sort(SortField.FIELD_SCORE,new SortField("attach", SortField.INT,true)));
- }
- @Test
- public void testQueryByFilter() throws ParseException{
- //创建QueryParser对象 默认的搜索域为content
- QueryParser parser = new QueryParser(Version.LUCENE_35, "content", new StandardAnalyzer(Version.LUCENE_35));
- //设置 空格的默认操作符为 AND 默认为OR
- // parser.setDefaultOperator(Operator.AND);
- //开启第一个字符的通配符配置
- parser.setAllowLeadingWildcard(true);
- //搜索content中包含kenan的
- Query query = parser.parse("kenan");
- Filter filter = null;
- //过滤name从1111到4444的 包括边界
- filter = new TermRangeFilter("name", "1111", "4444", true, true);
- //过滤attach大小从 1到3的 包括边界
- filter = NumericRangeFilter.newIntRange("attach", 1, 3, true, true);
- //通过一个Query进行过滤
- filter = new QueryWrapperFilter(new WildcardQuery(new Term("email", "*.com")));
- su.queryByFilter(query ,10,null);
- su.queryByFilter(query ,10,filter);
- }
- }
本文出自 “Kenan_ITBlog” 博客,请务必保留此出处http://soukenan.blog.51cto.com/5130995/1122938