• 本章使用的是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之其他查询方式(组合查询,制定数字范围、指定字符串开头)的更多相关文章

  1. LinQ各种方式查询、组合查询、IQueryable集合类型

    1.模糊查询(包含) Repeater1.DataSource = con.car.Where(r =>r.name.Contains(s)).ToList(); 2.开头查询 Repeater ...

  2. SolrJ查询条件组合查询实现——(十六)

    带查询条件的实现原理: 查询按钮被包在一个大表单,表单还有三个隐藏域,一个商品筛选,一个 价格,一个排序,每次点击查询时候清空三个隐藏域,就带着一个大条件去查询;点击下面的筛选条件时,给隐藏域的筛选条 ...

  3. SQL语句汇总(三)——聚合函数、分组、子查询及组合查询

    聚合函数: SQL中提供的聚合函数可以用来统计.求和.求最值等等. 分类: –COUNT:统计行数量 –SUM:获取单个列的合计值 –AVG:计算某个列的平均值 –MAX:计算列的最大值 –MIN:计 ...

  4. SQL语句汇总(三)——聚合函数、分组、子查询及组合查询

    拖了一个星期,终于开始写第三篇了.走起! 聚合函数: SQL中提供的聚合函数可以用来统计.求和.求最值等等. 分类: –COUNT:统计行数量 –SUM:获取单个列的合计值 –AVG:计算某个列的平均 ...

  5. MySQL全面瓦解11:子查询和组合查询

    概述 子查询是SQL查询中的重要一块,是我们基于多表之间进行数据聚合和判断的一种手段,使得我们的处理复杂数据更加的便捷,这一节我们主要来了解一下子查询. 先做一下数据准备,这边建立三张表:班级.学生. ...

  6. mysql 子查询 联结 组合查询

    子查询 SELECT cust_id FROM orders WHERE order_num IN (SELECT order_num FROM orderitems WHERE prod_id='T ...

  7. linq的简单查询 和 组合查询

    以Car表和Brand表为例,其中Car表的Brand是Brand表的Brandcode. (1)建立两表的linq(一定要做好主外键关系,),创建之后不用修改,如要添加,另建文件. (2)Car表的 ...

  8. MySQL联结查询和组合查询

    联结查询 1.关系表 主键:一列或一组列,能够唯一区分表中的每一行,用来表示一个特定的行 外键:为某个表中的一列,包含另一个表的主键,定义量表的关系. 2.创建联结 规定要连接的表和他们如何关联即可 ...

  9. LinQ高级查询、组合查询、IQueryable集合类型

    LinQ高级查询: 1.模糊查询(包含) Repeater1.DataSource = con.car.Where(r =>r.name.Contains(s)).ToList(); 2.开头 ...

随机推荐

  1. new HttpClient().PostAsync封装参数

    var data = Encoding.UTF8.GetBytes("{ \"y\": 5, \"x\": 3}"); var conten ...

  2. cross socket和msgpack的数据序列和还原

    cross socket和msgpack的数据序列和还原 procedure TForm1.Button1Click(Sender: TObject); begin var pack: TSimple ...

  3. 关于路由跟踪指令---traceroute

    我们都用过ping命令来检查主机与目标地址是否连通,自己的主机与目标地址的通讯包通讯速率,所谓的通讯包也就是那些什么TCP/IP,UDP包. 原理:https://www.cnblogs.com/be ...

  4. SpringBoot缓存技术

    一.SpringBoot整合Ehhcache 添加maven依赖 <dependency> <groupId>org.springframework.boot</grou ...

  5. 在主机上如何构建openwrt的编译环境?

    答: 请参考官方文档

  6. python笔记8 socket(TCP) subprocess模块 粘包现象 struct模块 基于UDP的套接字协议

    socket 基于tcp协议socket 服务端 import socket phone = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 买 ...

  7. 网络爬虫urllib:request之urlopen

    网络爬虫urllib:request之urlopen 网络爬虫简介 定义:按照一定规则,自动抓取万维网信息的程序或脚本. 两大特征: 能按程序员要求下载数据或者内容 能自动在网络上流窜(从一个网页跳转 ...

  8. DisplayAttribute应用——根据PropertyName自动获取对应的UI显示名

    model定义,使用DisplayAttribute public class AddressSetInfo { /// <summary> /// invoiceAddress.Id / ...

  9. 123457123457#0#-----com.threeapp.PaoPaoLong01-----泡泡龙大作战01

    com.threeapp.PaoPaoLong01-----泡泡龙大作战01

  10. react 点击事件

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...