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日记——单词替换 1.7 21

    21:单词替换 总时间限制:  1000ms 内存限制:  65536kB 描述 输入一个字符串,以回车结束(字符串长度<=100).该字符串由若干个单词组成,单词之间用一个空格隔开,所有单词区 ...

  2. UNITY在VS中调试

    下载地址:https://visualstudiogallery.msdn.microsoft.com/site/search?f%5B0%5D.Type=RootCategory&f%5B0 ...

  3. 在虚拟机中安装CentOS7

    在虚拟机中安装CentOS7 听语音 | 浏览:17352 | 更新:2014-10-31 12:14 1 2 3 4 5 6 7 分步阅读 一键约师傅 百度师傅最快的到家服务,最优质的电脑清灰! 百 ...

  4. 当一名黑客获得一份WebShell后,会做什么

    当你获得一份webshell后,你会干嘛? 我曾获得N多webshell.什么discuz.dedecms.phpwind.phpweb.aspcms等等,甚至还包括N多自己研发的线上平台. 可是,问 ...

  5. uva146 ID codes

    Description It is 2084 and the year of Big Brother has finally arrived, albeit a century late. In or ...

  6. 在脚本中使用export导出变量值

    LD_LIBRARY_PATH环境变量可以用于设置Linux动态库的位置,常见的用法如export LD_LIBRARY_PATH=/home/username/foo:$LD_LIBRARY_PAT ...

  7. 百度搜索--jquery

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  8. angular的uiRouter服务学习(5) --- $state.includes()方法

    $state.includes方法用于判断当前激活状态是否是指定的状态或者是指定状态的子状态. $state.includes(stateOrName,params,options) $state.i ...

  9. javascript中的链表结构—从链表中删除元素

    1.概念 上一个博文我们讲到链表,其中有一个方法remove()是暂时注释的,这个方法有点复杂,需要添加一个Previous()方法找到要删除的元素的前一个节点,这一个博文我们来分析一下这个remov ...

  10. 清北学堂2017NOIP冬令营入学测试 P4744 A’s problem(a)

    清北学堂2017NOIP冬令营入学测试 P4744 A's problem(a) 时间: 1000ms / 空间: 655360KiB / Java类名: Main 背景 冬令营入学测试题,每三天结算 ...