PrefixQuery就是使用前缀来进行查找的。通常情况下,首先定义一个词条Term。该词条包含要查找的字段名以及关键字的前缀,然后通过该词条构造一个PrefixQuery对象,就可以进行前缀查找了。

package ch11;

import org.apache.lucene.analysis.standard.StandardAnalyzer;

import org.apache.lucene.document.Document;

import org.apache.lucene.document.Field;

import org.apache.lucene.index.IndexWriter;

import org.apache.lucene.index.Term;

import org.apache.lucene.search.Hits;

import org.apache.lucene.search.IndexSearcher;

import org.apache.lucene.search.PrefixQuery;

import org.apache.lucene.search.RangeQuery;

public class PrefixQueryTest {

     public static void main(String[] args) throws Exception {

         //生成Document对象,下同

         Document doc1 = new Document();

         //添加“name”字段的内容,下同

         doc1.add(Field.Text("name", "David"));

         //添加“title”字段的内容,下同

         doc1.add(Field.Keyword("title", "doc1"));

         Document doc2 = new Document();

         doc2.add(Field.Text("name", "Darwen"));

         doc2.add(Field.Keyword("title", "doc2"));

         Document doc3 = new Document();

         doc3.add(Field.Text("name", "Smith"));

         doc3.add(Field.Keyword("title", "doc3"));

         Document doc4 = new Document();

         doc4.add(Field.Text("name", "Smart"));

         doc4.add(Field.Keyword("title", "doc4"));

         //生成索引书写器

         IndexWriter writer = new IndexWriter("c://index",

                 new StandardAnalyzer(), true);

         //设置为混合索引模式

         writer.setUseCompoundFile(true);

         //依次将文档添加到索引中

         writer.addDocument(doc1);

         writer.addDocument(doc2);

         writer.addDocument(doc3);

         writer.addDocument(doc4);

         //关闭索引书写器

         writer.close();

         //生成索引搜索器对象

         IndexSearcher searcher = new IndexSearcher("c://index");

         //构造词条

         Term pre1 = new Term("name", "Da");

         Term pre2 = new Term("name", "da");

         Term pre3 = new Term("name", "sm");

         //用于保存检索结果

         Hits hits = null;

         //生成PrefixQuery类型的对象,初始化为null

         PrefixQuery query = null;

         query = new PrefixQuery(pre1);

         //开始第一次检索,并返回检索结果

         hits = searcher.search(query);

         //输出相应的检索结果

         printResult(hits, "前缀为'Da'的文档");

         query = new PrefixQuery(pre2);

         //开始第二次检索,并返回检索结果

         hits = searcher.search(query);

         //输出相应的检索结果

         printResult(hits, "前缀为'da'的文档");

         query = new PrefixQuery(pre3);

         //开始第二次检索,并返回检索结果

         hits = searcher.search(query);

         //输出相应的检索结果

         printResult(hits, "前缀为'sm'的文档");

     }

     public static void printResult(Hits hits, String key) throws Exception

         {System.out.println("查找 /"" + key + "/" :");

         if (hits != null) {

             if (hits.length() == 0) {

                 System.out.println("没有找到任何结果");

                 System.out.println();

             } else {

                 System.out.print("找到");

                 for (int i = 0; i < hits.length(); i++) {

                     //取得文档

                     Document d = hits.doc(i);

                     //取得“title”字段的内容

                     String dname = d.get("title");

                     System.out.print(dname + "   ");

                 }

                 System.out.println();

                 System.out.println();

             }

         }

     }

}

代码中,首先构造了4个不同的Document。每个Document都有一个名为“name”的字段,其中存储了人物的名称。然后,代码构建了3个不同的词条,分别为“Da”、“da”和“sm”,可以看到,它们正好都是“name”字段中关键字的前缀。

lucene-查询query->PrefixQuery使用前缀搜索的更多相关文章

  1. Lucene 查询(Query)子类

    QueryParser(单域查询) QueryParser子类对单个域查询时创建查询query,构造方法中需要传入Lucene版本号,检索域名和分词器. QueryParser parser = ne ...

  2. lucene 查询 (转载)

    原网址:http://hi.baidu.com/lszhuhaichao/blog/item/ccffc7cb858f1514bf09e66f.html Lucene3.0之查询处理(1):原理201 ...

  3. lucene查询索引库、分页、过滤、排序、高亮

    2.查询索引库 插入测试数据 xx.xx. index. ArticleIndex @Test public void testCreateIndexBatch() throws Exception{ ...

  4. query_string查询支持全部的Apache Lucene查询语法 低频词划分依据 模糊查询 Disjunction Max

    3.3 基本查询3.3.1词条查询 词条查询是未经分析的,要跟索引文档中的词条完全匹配注意:在输入数据中,title字段含有Crime and Punishment,但我们使用小写开头的crime来搜 ...

  5. lucene查询解析器语法

    注意:使用QueryParser查询,关键词是会被分词的,如果不需要分词,可以选择使用Lucene提供的API查询类. Lucene提供了丰富的API来组合定制你所需要的查询器,同时也可以利用Quer ...

  6. 基于Lucene查询原理分析Elasticsearch的性能

    前言 Elasticsearch是一个很火的分布式搜索系统,提供了非常强大而且易用的查询和分析能力,包括全文索引.模糊查询.多条件组合查询.地理位置查询等等,而且具有一定的分析聚合能力.因为其查询场景 ...

  7. Lucene 查询原理 传统二级索引方案 倒排链合并 倒排索引 跳表 位图

    提问: 1.倒排索引与传统数据库的索引相比优势? 2.在lucene中如果想做范围查找,根据上面的FST模型可以看出来,需要遍历FST找到包含这个range的一个点然后进入对应的倒排链,然后进行求并集 ...

  8. Lucene查询索引(分页)

    分页查询只需传入每页显示记录数和当前页就可以实现分页查询功能 Lucene分页查询是对搜索返回的结果进行分页,而不是对搜索结果的总数量进行分页,因此我们搜索的时候都是返回前n条记录 package c ...

  9. 第六步:Lucene查询索引(优化一)

    package cn.harmel.lucene; import java.io.IOException; import java.nio.file.Paths; import org.apache. ...

  10. lucene 查询的使用

    各种查询方式一:使用QueryParser与查询语法.(会使用分词器) MultiFieldQueryParser查询字符串 ------------------------> Query对象 ...

随机推荐

  1. AC日记——合法C标识符 openjudge 1.7 06

    06:合法 C 标识符 总时间限制:  1000ms 内存限制:   65536kB 描述 给定一个不包含空白符的字符串,请判断是否是C语言合法的标识符号(注:题目保证这些字符串一定不是C语言的保留字 ...

  2. u3d_Shader_effects笔记3 half diffuse 和 ramp texture

    1.前面的心情 每次写博客,先写心情也好,就当是小日记了吧.现在已经懒到不想动笔和纸来写日记了.近两天公司的活较少,晚上直接回来了,没有留公司.在公司看代码,不做工,就困... 哎,小辉哥家的老房子后 ...

  3. Android app 简单的电话拨号器

    实现步骤: 1.画UI 可以用拖拽和文本编辑. 2.根据UI写业务逻辑  在MainActivity中的onCreate中编写 //get editText content et_number = ( ...

  4. Visual Studio各版本工程文件之间的转换

    转载于:http://www.cnblogs.com/jmliao/p/5594179.html 由于VS版本比较多,低版本无法直接打开高版本的工程文件,通过对工程文件进行一些修改可以解决这些问题. ...

  5. tween.js

     简要教程 tween.js是一款可生成平滑动画效果的js动画库.相关的动画库插件还有:snabbt.js 强大的jQuery动画库插件和Tweene-超级强大的jQuery动画代理插件. tween ...

  6. (原创)mybaits学习三,springMVC和mybatis融合

    上一节,总计了spring和mybaits的融合,这一节,我们来学习springmvc和mybatis融合 最近在弄一个SSM的项目,然后在网上找资料,将资料总结如下 一,开发环境的配置 MyEcli ...

  7. [转]终于找到全annotation配置springMVC的方法了(事务不失效)

    原文:http://icanfly.iteye.com/blog/778401 icanfly 写道 如果带上事务,那么用annotation方式的事务注解和bean配置,事务会失效,要将servic ...

  8. [MetaHook] SearchPattern function

    By Nagi void *SearchPattern(void *pStartSearch, DWORD dwSearchLen, char *pPattern, DWORD dwPatternLe ...

  9. android animation中的参数interpolator详解

      android:interpolator interpolator 被用来修饰动画效果,定义动画的变化率,可以使存在的动画效果可以 accelerated(加速),decelerated(减速), ...

  10. Google最新截屏案例详解

    Google从Android 5.0 开始,给出了截屏案例ScreenCapture,在同版本的examples的Media类别中可以找到.给需要开发手机或平板截屏应用的小伙伴提供了非常有意义的参考资 ...