(六)lucene之其他查询方式(组合查询,制定数字范围、指定字符串开头)
- 本章使用的是lucene5.3.0
指定数字范围查询
package com.shyroke.test; import java.io.IOException;
import java.nio.file.Paths; import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.IntField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.index.DirectoryReader;
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.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.PrefixQuery;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.junit.Before;
import org.junit.Test; public class IndexTest { private Integer ids[]={1,2,3};
private String citys[]={"aingdao","banjing","changhai"};
private String descs[]={
"Qingdao is a beautiful city.",
"Nanjing is a city of culture.",
"Shanghai is a bustling city."
}; private IndexReader reader;
private IndexSearcher searcher;
private String indexDir="E:\\lucene5\\index"; @Before
public void setUp() throws IOException {
IndexWriter indexWriter = getIndexWiter(); for (int i = 0; i < ids.length; i++) {
Document document = new Document();
document.add(new IntField("id", ids[i], Field.Store.YES));
document.add(new StringField("city", citys[i], Field.Store.YES));
document.add(new StringField("desc", descs[i], Field.Store.NO));
indexWriter.addDocument(document);
} indexWriter.close(); Directory dir=FSDirectory.open(Paths.get(indexDir));
reader=DirectoryReader.open(dir);
searcher=new IndexSearcher(reader); } /**
* 实例化IndexWiter
*
* @return
* @throws IOException
*/
private IndexWriter getIndexWiter() throws IOException {
Directory dir = FSDirectory.open(Paths.get(indexDir));
Analyzer analyzer = new StandardAnalyzer();
IndexWriterConfig conf = new IndexWriterConfig(analyzer);
IndexWriter indexWriter = new IndexWriter(dir, conf); return indexWriter;
} /**
* 指定数字范围,其中id必须是整型的,如:document.add(new IntField("id", ids[i], Field.Store.YES));
* newIntRange方法第二、三个参数是查询的起点值和终点值,即区间,最后两个参数是指是否包含起点值和终点值。
* @throws Exception
*/
@Test
public void testNumericRangeQuery()throws Exception{
NumericRangeQuery<Integer> query=NumericRangeQuery.newIntRange("id", 1, 2, true, true);
TopDocs hits=searcher.search(query, 10);
for(ScoreDoc scoreDoc:hits.scoreDocs){
Document doc=searcher.doc(scoreDoc.doc);
System.out.println(doc.get("id"));
System.out.println(doc.get("city"));
System.out.println(doc.get("desc"));
System.out.println("---------------------");
}
} }
- 结果:
解析:newIntRange("id", 1, 2, true, true); 该方法查询出id值为1和2之间的document,且包含1和2.因为desc设置为“Field.Store.NO”所以这里的值为null
指定字符串开头搜索
/**
* 指定字符串开头搜索
* @throws Exception
*/
@Test
public void testPrefixQuery()throws Exception{
PrefixQuery query=new PrefixQuery(new Term("city","a"));
TopDocs hits=searcher.search(query, 10);
for(ScoreDoc scoreDoc:hits.scoreDocs){
Document doc=searcher.doc(scoreDoc.doc);
System.out.println(doc.get("id"));
System.out.println(doc.get("city"));
System.out.println(doc.get("desc"));
}
}
结果:
解:查询出city中以a为开头的document
多条件查询
/**
* 多条件查询
* @throws Exception
*/
@Test
public void testBooleanQuery()throws Exception{
NumericRangeQuery<Integer> query1=NumericRangeQuery.newIntRange("id", 1, 2, true, true);
PrefixQuery query2=new PrefixQuery(new Term("city","a"));
BooleanQuery.Builder booleanQuery=new BooleanQuery.Builder();
booleanQuery.add(query1,BooleanClause.Occur.MUST);
booleanQuery.add(query2,BooleanClause.Occur.MUST);
TopDocs hits=searcher.search(booleanQuery.build(), 10);
for(ScoreDoc scoreDoc:hits.scoreDocs){
Document doc=searcher.doc(scoreDoc.doc);
System.out.println(doc.get("id"));
System.out.println(doc.get("city"));
System.out.println(doc.get("desc"));
}
}
解:查询出id值为1和2之间且city值以a开头的document
(六)lucene之其他查询方式(组合查询,制定数字范围、指定字符串开头)的更多相关文章
- LinQ各种方式查询、组合查询、IQueryable集合类型
1.模糊查询(包含) Repeater1.DataSource = con.car.Where(r =>r.name.Contains(s)).ToList(); 2.开头查询 Repeater ...
- SolrJ查询条件组合查询实现——(十六)
带查询条件的实现原理: 查询按钮被包在一个大表单,表单还有三个隐藏域,一个商品筛选,一个 价格,一个排序,每次点击查询时候清空三个隐藏域,就带着一个大条件去查询;点击下面的筛选条件时,给隐藏域的筛选条 ...
- SQL语句汇总(三)——聚合函数、分组、子查询及组合查询
聚合函数: SQL中提供的聚合函数可以用来统计.求和.求最值等等. 分类: –COUNT:统计行数量 –SUM:获取单个列的合计值 –AVG:计算某个列的平均值 –MAX:计算列的最大值 –MIN:计 ...
- SQL语句汇总(三)——聚合函数、分组、子查询及组合查询
拖了一个星期,终于开始写第三篇了.走起! 聚合函数: SQL中提供的聚合函数可以用来统计.求和.求最值等等. 分类: –COUNT:统计行数量 –SUM:获取单个列的合计值 –AVG:计算某个列的平均 ...
- MySQL全面瓦解11:子查询和组合查询
概述 子查询是SQL查询中的重要一块,是我们基于多表之间进行数据聚合和判断的一种手段,使得我们的处理复杂数据更加的便捷,这一节我们主要来了解一下子查询. 先做一下数据准备,这边建立三张表:班级.学生. ...
- mysql 子查询 联结 组合查询
子查询 SELECT cust_id FROM orders WHERE order_num IN (SELECT order_num FROM orderitems WHERE prod_id='T ...
- linq的简单查询 和 组合查询
以Car表和Brand表为例,其中Car表的Brand是Brand表的Brandcode. (1)建立两表的linq(一定要做好主外键关系,),创建之后不用修改,如要添加,另建文件. (2)Car表的 ...
- MySQL联结查询和组合查询
联结查询 1.关系表 主键:一列或一组列,能够唯一区分表中的每一行,用来表示一个特定的行 外键:为某个表中的一列,包含另一个表的主键,定义量表的关系. 2.创建联结 规定要连接的表和他们如何关联即可 ...
- LinQ高级查询、组合查询、IQueryable集合类型
LinQ高级查询: 1.模糊查询(包含) Repeater1.DataSource = con.car.Where(r =>r.name.Contains(s)).ToList(); 2.开头 ...
随机推荐
- sklearn.GridSearchCV选择超参
from sklearn import datasets from sklearn.model_selection import train_test_split from sklearn.model ...
- what should I use .post vs .ajax?
what should I use .post vs .ajax? 问题: I've always had this dilemma困境 whether to use .post or .ajax f ...
- RabbitMQ and batch processing 批提交
RabbitMQ - RabbitMQ and batch processinghttp://rabbitmq.1065348.n5.nabble.com/RabbitMQ-and-batch-pro ...
- Android Studio创建Module-库模块
Android Studio创建Module-库模块 本文链接:https://blog.csdn.net/niuba123456/article/details/81049676 模块是源文件和 ...
- IFC数据模型构件控制
控制ifc构件的隐藏与显示.着色 osg::ref_ptr<osg::Geode> geode1 = new osg::Geode(); osg::ref_ptr<osg::Stat ...
- osg::PagedLOD example
int main() { osg::ref_ptr<osgViewer::Viewer> viewer1 = new osgViewer::Viewer; osg::ref_ptr< ...
- 123457123457#0#---------com.ppGame.SeaPuzzleGame73--前拼后广--宝宝海洋拼图pp
com.ppGame.SeaPuzzleGame73--前拼后广--宝宝海洋拼图pp
- 123457123456#0#-----com.yuming.FromPuzzleGame01--前拼后广--宝宝农场拼图cym
com.yuming.FromPuzzleGame01--前拼后广--宝宝农场拼图cym
- 123457123456#0#-----com.threeapp.headsoccer----宝宝头球大战
com.threeapp.headsoccer----宝宝头球大战
- LeetCode_198. House Robber
198. House Robber Easy You are a professional robber planning to rob houses along a street. Each hou ...