Lucene&&solr02

4 篇文章 0 订阅
3 篇文章 0 订阅

MatchAllDocsQuery
使用MatchAllDocsQuery查询索引目录中的所有文档

public void testMatchAllDocsQuery()throws Exception{
		//指定索引库存放的路径
		Directory directory = FSDirectory.open(new File("G:\\temp\\index"));
		//创建一个IndexReader对象
		IndexReader indexReader = DirectoryReader.open(directory);
		//创建IndexSearcher对象
		IndexSearcher indexSearcher = new IndexSearcher(indexReader);
		//创建一个Query对象
		Query query = new MatchAllDocsQuery();
		//查询索引库
		TopDocs topDocs = indexSearcher.search(query,100);
		ScoreDoc[] scoreDocs = topDocs.scoreDocs;
		System.out.println("查询结果总记录数" +topDocs.totalHits);
		//遍历查询结果
		for (ScoreDoc scoreDoc : scoreDocs) {
			int docId= scoreDoc.doc;
			//通过id查询文档对象
			Document document = indexSearcher.doc(docId);
			//取属性
			System.out.println(document.get("name"));
			System.out.println(document.get("path"));
			System.out.println(document.get("content"));
			System.out.println(document.get("size"));
			
		}
      indexReader.close();
	}

NumericRangeQuery
数值范围查询

@Test
	public void testNumericRangeQuery()throws Exception{
		//创建一个数值范围查询
		//参数1:要查询的域 参数2:最小值 参数3:最大值  参数4:是否包含最小值  参数5:是否包含最大值
		Query query =NumericRangeQuery.newLongRange("size", 0l, 1000l, 	true, true);
		//打印结果
		printResult(getIndexSearcher(), query);
	}

BooleanQuery

public void testBooleanQuery()throws Exception{
		//创建一个BooleanQuery对象
		BooleanQuery query = new BooleanQuery();
		//创建子查询,文件大于1000小于10000
		Query query1 =NumericRangeQuery.newLongRange("size", 1000l, 10000l, 	true, true);
		//文件名中包含mybatis关键字
		Query query2 = new TermQuery(new Term("name","mybatis"));
		//添加到BooleanQuery对象中
		query.add(query1,Occur.MUST);
		query.add(query2,Occur.MUST);
		//执行查询
		printResult(getIndexSearcher(), query);
	}

Occur.MUST:必须满足此条件,相当于and
Occur.SHOULD:应该满足,但是不满足也可以,相当于or
Occur.MUST_NOT:必须不满足。相当于not
使用QueryParser查询

public void testQueryParser()throws Exception{
		//创建一个QueryParser对象。 参数1:默认搜索域 参数:2分析器对象
		QueryParser queryParser = new QueryParser("content", new IKAnalyzer());
		//调用parse方法可以获得一个Query对象
		//参数:要查询的内容,可以是一句话。先分词在查询
		Query query = queryParser.parse("mybatis is a apache project");
		printResult(getIndexSearcher(), query);
	}

导入
lucene-queryparser包
多域查询

@Test
	public void testMultiFileQueryParser()throws Exception{
		//指定默认搜索域
		String [] fields = {"name","content"};
		MultiFieldQueryParser queryParser = new MultiFieldQueryParser(fields, new IKAnalyzer());
		Query query = queryParser.parse("mybatis is a apache project");
		System.out.println(query);
		printResult(getIndexSearcher(), query);
	}

什么是Solr
Solr是Apache下的一个顶级开源项目,采用java开发,它是基于Lucene的全文搜索服务器。Solr提供了比Lucene更为丰富的查询语言,同时实现了可配置、可扩展、并对索引、搜索性能进行了优化。
Solr可以独立运行,运行在Jetty、Tomcat等这些Service容器中,Solr索引的实现方法很简单,用POST方法向Solr服务器发送一个描述Field及其内容的XML文档,Solr根据xml文档添加、删除、更新索引。Solr搜索只需要发送HTTP GET请求,然后对Solr返回XML、json等格式的查询结果进行解析,组织页面布局。Solr不提供构建UI的功能,Solr提供了一个管理页面,通过管理页面可以查询Solr的配置和运行情况。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值