`
smallearth
  • 浏览: 37461 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

Lucene小练11(QueryPraser搜索--基本能实现各种搜索)

 
阅读更多
package Java.se.lucene;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

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.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.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.LockObtainFailedException;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;

public class Search {
	private String[] ids={"1","2","3","4","5","6"};
	private String[] emails={"aa@aa.com","bb@bb.com",
			"cc@cc.com","dd@dd.com","ee@ee.com","ff@ff.com"};
	private String[] contents={"i like  gdsfgfds","i like fsdfs","i like fdsfsd",
			"i like fdsfsd","i like like fdfs","i like like like fsefsdfg"};
	private int[] attachs={1,2,3,4,5,6};
	private String[] names={"liwu","zhangsan","xiaoqinag","laona",
			"dabao","lisi"};
	private IndexWriter writer=null;
	private Date[] dates=null;
	private Map<String,Float> scores=new HashMap<String,Float>();
	private Directory directory=null;
	private IndexReader reader=null;
	
	public Search()
	{
		directory=new RAMDirectory();
		setDate();
		index();
	}
	//建立索引
	public void index()
	{
		Document document=null;
         try {
			writer=new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_36,
				new StandardAnalyzer(Version.LUCENE_36)));
			writer.deleteAll();//更新索引
			for(int i=0;i<ids.length;i++)
			{
				document=new Document();
				document.add(new Field("id", ids[i], 
						Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS  ));
				document.add(new Field("email",emails[i],
						Field.Store.YES,Field.Index.NOT_ANALYZED));
				document.add(new Field("content", contents[i], 
						Field.Store.YES, Field.Index.ANALYZED));
				document.add(new Field("name",names[i],
						Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));
		    	//为数字添加索引
				document.add(new NumericField("attach", Field.Store.YES,true).
						setIntValue(attachs[i]));
				//为日期添加索引
				document.add(new NumericField("date", Field.Store.YES,true)
				.setLongValue(dates[i].getTime()));//记住要getTime
						
				
		    	String str=emails[i].substring(emails[i].lastIndexOf("@")+1);
		    	//System.out.println(str);
    		    if(scores.containsKey(str))
		    	{
		    		document.setBoost(scores.get(str));
		    	}else{
	    		document.setBoost(0.5f);
		    	}
		    	writer.addDocument(document);
		    	writer.commit();//提交writer
			}
		} 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{
            try {
				writer.close();
			} catch (CorruptIndexException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	//获取searcher
	public IndexSearcher getSearcher()
	{
		try {
			if(reader==null)
			{
				reader=IndexReader.open(directory);
			}else {
				IndexReader rd=IndexReader.openIfChanged(reader);
				if(reader!=null)
				{
					reader.close();
					reader=rd;
				}
			}
			return new IndexSearcher(reader);
		} catch (CorruptIndexException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	//词条搜索
	public void searchByQueryParse(Query query,int num)
	{     
		try {
				IndexSearcher searcher=getSearcher();
				TopDocs tds=searcher.search(query, num);
				System.out.println("一共查询了:"+tds.totalHits);
				for(ScoreDoc sd:tds.scoreDocs)
				{
					Document doc=searcher.doc(sd.doc);
					System.out.println(doc.get("id")+"-->"+doc.get("name")+".."
							+"["+doc.get("email")+"]"+"("+doc.get("content")+")"+
							"-->"+doc.get("attach")+".."+doc.get("date"));				
				}			
				searcher.close();
			} catch (CorruptIndexException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}	
	}
	
	//创建日期
    public void setDate()
    {
    	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-mm-kk");
    	try {
    		dates=new Date[ids.length];
        	dates[0]=sdf.parse("2010-08-17");
        	dates[1]=sdf.parse("2011-02-17");
        	dates[2]=sdf.parse("2012-03-17");
        	dates[3]=sdf.parse("2011-04-17");
        	dates[4]=sdf.parse("2012-05-17");
        	dates[5]=sdf.parse("2011-07-17");
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
    } 
}

package Java.se.lucene;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.queryParser.QueryParser.Operator;
import org.apache.lucene.search.Query;
import org.apache.lucene.util.Version;
import org.junit.Before;
import org.junit.Test;

public class Test_Search {
	private static final int num=10;
	private Search search=null;
	
	@Before
	public void init()
	{
	   search=new Search();	
	}
	
	@Test
	public void test_search09() throws ParseException
	{
		//创建ParseQuery,默认搜索域是content
		QueryParser parser=new QueryParser(Version.LUCENE_36,"content",new
				StandardAnalyzer(Version.LUCENE_36));
		//将空格改为默认”和“
		parser.setDefaultOperator(Operator.AND);
		//搜索content中包含like的
		Query query=parser.parse("like");
		//搜索content中包含fsdfs 或者fdsfsd的,空格代表“或”
		query=parser.parse("fsdfs fdsfsd");
		//搜索域为name中包含mike的
		query=parser.parse("name:mike");
		//同样可以用*和?来匹配
		query=parser.parse("name:na*");
		//通配符默认不能放在首位
		//query=parser.parse("name:*na"); 
		//匹配name中没有mike但是含有fdsfsd的
		query=parser.parse("- name:mike + fdsfsd");
		//设定搜索区间,闭区间用【】
		query=parser.parse("id:[1 TO 4]");
		//设定搜索区间,开区间{}
		query=parser.parse("id:{1 TO 4}");
		//匹配字符串i like  gdsfgfds
		query=parser.parse("i like  gdsfgfds");
		//匹配字符串i gdsfgfds之间有一个空格的
		query=parser.parse("\"i gdsfgfds\"~1");
		//内办法匹配数字范围
		query=parser.parse("attach:{1 TO 3}");
	    search.searchByQueryParse(query,num);
	}
//	public static void main(String[] args) {
//		Search search=new Search();
//		search.searchByTerm("name", "mike", 3);
//	}
}


分享到:
评论

相关推荐

    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-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-spatial3d-6.6.0-API文档-中文版.zip

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

    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-spatial3d-7.3.1-API文档-中英对照版.zip

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

    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-spatial3d-7.2.1-API文档-中文版.zip

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

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

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

    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-spatial3d-7.7.0-API文档-中文版.zip

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

    lucene-spatial3d-7.3.1-API文档-中文版.zip

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

    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-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-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-spatial3d-7.7.0-API文档-中英对照版.zip

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

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

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

Global site tag (gtag.js) - Google Analytics