• 本章使用的是lucene5.3.0
    1. 指定数字范围查询
  1. package com.shyroke.test;
  2.  
  3. import java.io.IOException;
  4. import java.nio.file.Paths;
  5.  
  6. import org.apache.lucene.analysis.Analyzer;
  7. import org.apache.lucene.analysis.standard.StandardAnalyzer;
  8. import org.apache.lucene.document.Document;
  9. import org.apache.lucene.document.Field;
  10. import org.apache.lucene.document.IntField;
  11. import org.apache.lucene.document.StringField;
  12. import org.apache.lucene.index.DirectoryReader;
  13. import org.apache.lucene.index.IndexReader;
  14. import org.apache.lucene.index.IndexWriter;
  15. import org.apache.lucene.index.IndexWriterConfig;
  16. import org.apache.lucene.index.Term;
  17. import org.apache.lucene.search.BooleanClause;
  18. import org.apache.lucene.search.BooleanQuery;
  19. import org.apache.lucene.search.IndexSearcher;
  20. import org.apache.lucene.search.NumericRangeQuery;
  21. import org.apache.lucene.search.PrefixQuery;
  22. import org.apache.lucene.search.ScoreDoc;
  23. import org.apache.lucene.search.TopDocs;
  24. import org.apache.lucene.store.Directory;
  25. import org.apache.lucene.store.FSDirectory;
  26. import org.junit.Before;
  27. import org.junit.Test;
  28.  
  29. public class IndexTest {
  30.  
  31. private Integer ids[]={1,2,3};
  32. private String citys[]={"aingdao","banjing","changhai"};
  33. private String descs[]={
  34. "Qingdao is a beautiful city.",
  35. "Nanjing is a city of culture.",
  36. "Shanghai is a bustling city."
  37. };
  38.  
  39. private IndexReader reader;
  40. private IndexSearcher searcher;
  41. private String indexDir="E:\\lucene5\\index";
  42.  
  43. @Before
  44. public void setUp() throws IOException {
  45. IndexWriter indexWriter = getIndexWiter();
  46.  
  47. for (int i = 0; i < ids.length; i++) {
  48. Document document = new Document();
  49. document.add(new IntField("id", ids[i], Field.Store.YES));
  50. document.add(new StringField("city", citys[i], Field.Store.YES));
  51. document.add(new StringField("desc", descs[i], Field.Store.NO));
  52. indexWriter.addDocument(document);
  53. }
  54.  
  55. indexWriter.close();
  56.  
  57. Directory dir=FSDirectory.open(Paths.get(indexDir));
  58. reader=DirectoryReader.open(dir);
  59. searcher=new IndexSearcher(reader);
  60.  
  61. }
  62.  
  63. /**
  64. * 实例化IndexWiter
  65. *
  66. * @return
  67. * @throws IOException
  68. */
  69. private IndexWriter getIndexWiter() throws IOException {
  70. Directory dir = FSDirectory.open(Paths.get(indexDir));
  71. Analyzer analyzer = new StandardAnalyzer();
  72. IndexWriterConfig conf = new IndexWriterConfig(analyzer);
  73. IndexWriter indexWriter = new IndexWriter(dir, conf);
  74.  
  75. return indexWriter;
  76. }
  77.  
  78. /**
  79. * 指定数字范围,其中id必须是整型的,如:document.add(new IntField("id", ids[i], Field.Store.YES));
  80. * newIntRange方法第二、三个参数是查询的起点值和终点值,即区间,最后两个参数是指是否包含起点值和终点值。
  81. * @throws Exception
  82. */
  83. @Test
  84. public void testNumericRangeQuery()throws Exception{
  85. NumericRangeQuery<Integer> query=NumericRangeQuery.newIntRange("id", 1, 2, true, true);
  86. TopDocs hits=searcher.search(query, 10);
  87. for(ScoreDoc scoreDoc:hits.scoreDocs){
  88. Document doc=searcher.doc(scoreDoc.doc);
  89. System.out.println(doc.get("id"));
  90. System.out.println(doc.get("city"));
  91. System.out.println(doc.get("desc"));
  92. System.out.println("---------------------");
  93. }
  94. }
  95.  
  96. }
  • 结果:

解析:newIntRange("id", 1, 2, true, true); 该方法查询出id值为1和2之间的document,且包含1和2.因为desc设置为“Field.Store.NO”所以这里的值为null

  • 指定字符串开头搜索

  1. /**
  2. * 指定字符串开头搜索
  3. * @throws Exception
  4. */
  5. @Test
  6. public void testPrefixQuery()throws Exception{
  7. PrefixQuery query=new PrefixQuery(new Term("city","a"));
  8. TopDocs hits=searcher.search(query, 10);
  9. for(ScoreDoc scoreDoc:hits.scoreDocs){
  10. Document doc=searcher.doc(scoreDoc.doc);
  11. System.out.println(doc.get("id"));
  12. System.out.println(doc.get("city"));
  13. System.out.println(doc.get("desc"));
  14. }
  15. }
  1. 结果:

解:查询出city中以a为开头的document

  • 多条件查询

  1. /**
  2. * 多条件查询
  3. * @throws Exception
  4. */
  5. @Test
  6. public void testBooleanQuery()throws Exception{
  7. NumericRangeQuery<Integer> query1=NumericRangeQuery.newIntRange("id", 1, 2, true, true);
  8. PrefixQuery query2=new PrefixQuery(new Term("city","a"));
  9. BooleanQuery.Builder booleanQuery=new BooleanQuery.Builder();
  10. booleanQuery.add(query1,BooleanClause.Occur.MUST);
  11. booleanQuery.add(query2,BooleanClause.Occur.MUST);
  12. TopDocs hits=searcher.search(booleanQuery.build(), 10);
  13. for(ScoreDoc scoreDoc:hits.scoreDocs){
  14. Document doc=searcher.doc(scoreDoc.doc);
  15. System.out.println(doc.get("id"));
  16. System.out.println(doc.get("city"));
  17. System.out.println(doc.get("desc"));
  18. }
  19. }

解:查询出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. OpenJudge计算概论-细菌实验分组

    /*====================================================细菌实验分组总时间限制: 1000ms 内存限制: 65536kB描述有一种细菌分为A.B两 ...

  2. hwclock和date源码分析

    一. hwclock 1.1 hwclock源码在哪里? util-linux 或者busybox 1.2 获取源码 git clone https://github.com/karelzak/uti ...

  3. javascript从网络下载随机笑话

    /*** * 讲笑话函数(调试用) * @param callback 回调函数 */ function randomText(callback) { var result =''; $.ajax({ ...

  4. MySQL数据库表的设计和优化(下)

    二.基于单表设计的多表设计原则:(1)表关系: 一)一对一关系: 定义: 在这种关系中,关系表的每一边都只能存在一个记录.每个数据表中的关键字在对应的关系表中只能存在一个记录或者没有对应的记录.这种关 ...

  5. redis列表和有序集合

    redis中的list数据类型是可以插入重复数据的,有去重的需求的话可以用redis有序集合数据类型 Redis Zadd 命令用于将一个或多个成员元素及其分数值加入到有序集当中. 如果某个成员已经是 ...

  6. Python3多重继承排序原理(C3算法)

    参考:https://www.jianshu.com/p/c9a0b055947b https://xubiubiu.com/2019/06/10/python-%E6%96%B9%E6%B3%95% ...

  7. iOS-UITextField和UITextView隐藏键盘

    UITextField和UITextView隐藏键盘 71 views, IOS DEV, by admin. self._textField.returnKeyType=UIReturnKeyDon ...

  8. 如何去除PATH里的重复项并排序

    注意sed的用法,linux和Mac os不同,linux是Gnu的,Mac是BSD的 PATH排序去掉重复内容 mac和linux的换行符替换方法不一样,如下是Mac下的操作 export PATH ...

  9. linux中安装docker

    uname -r yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ ...

  10. linux系统时间设定

    更改系统时间并同步硬件时钟 sudo date -s '2018-12-27 12:46' sudo hwclock --systohc hwclock说明:hwclock --help