lucene-查询query->PrefixQuery使用前缀搜索
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使用前缀搜索的更多相关文章
- Lucene 查询(Query)子类
QueryParser(单域查询) QueryParser子类对单个域查询时创建查询query,构造方法中需要传入Lucene版本号,检索域名和分词器. QueryParser parser = ne ...
- lucene 查询 (转载)
原网址:http://hi.baidu.com/lszhuhaichao/blog/item/ccffc7cb858f1514bf09e66f.html Lucene3.0之查询处理(1):原理201 ...
- lucene查询索引库、分页、过滤、排序、高亮
2.查询索引库 插入测试数据 xx.xx. index. ArticleIndex @Test public void testCreateIndexBatch() throws Exception{ ...
- query_string查询支持全部的Apache Lucene查询语法 低频词划分依据 模糊查询 Disjunction Max
3.3 基本查询3.3.1词条查询 词条查询是未经分析的,要跟索引文档中的词条完全匹配注意:在输入数据中,title字段含有Crime and Punishment,但我们使用小写开头的crime来搜 ...
- lucene查询解析器语法
注意:使用QueryParser查询,关键词是会被分词的,如果不需要分词,可以选择使用Lucene提供的API查询类. Lucene提供了丰富的API来组合定制你所需要的查询器,同时也可以利用Quer ...
- 基于Lucene查询原理分析Elasticsearch的性能
前言 Elasticsearch是一个很火的分布式搜索系统,提供了非常强大而且易用的查询和分析能力,包括全文索引.模糊查询.多条件组合查询.地理位置查询等等,而且具有一定的分析聚合能力.因为其查询场景 ...
- Lucene 查询原理 传统二级索引方案 倒排链合并 倒排索引 跳表 位图
提问: 1.倒排索引与传统数据库的索引相比优势? 2.在lucene中如果想做范围查找,根据上面的FST模型可以看出来,需要遍历FST找到包含这个range的一个点然后进入对应的倒排链,然后进行求并集 ...
- Lucene查询索引(分页)
分页查询只需传入每页显示记录数和当前页就可以实现分页查询功能 Lucene分页查询是对搜索返回的结果进行分页,而不是对搜索结果的总数量进行分页,因此我们搜索的时候都是返回前n条记录 package c ...
- 第六步:Lucene查询索引(优化一)
package cn.harmel.lucene; import java.io.IOException; import java.nio.file.Paths; import org.apache. ...
- lucene 查询的使用
各种查询方式一:使用QueryParser与查询语法.(会使用分词器) MultiFieldQueryParser查询字符串 ------------------------> Query对象 ...
随机推荐
- Unity官网教程之Tips
前言 翻译整理unity官网教程的tips部分,原文:http://unity3d.com/cn/learn/tutorials/topics/tips Snap 按住Ctrl键,并用鼠标拖动Game ...
- html5的小知识点小集合
html5的小知识点小集合 html5知识 1. Doctype作用?标准模式与兼容模式各有什么区别? (1).<!DOCTYPE>声明位于位于HTML文档中的第一行,处于< ...
- dp入门问题
昨天晚上的rank彻底废了..一个星期没敲代码完全没手感.作为总结,贴一道昨天浪费了我两小时的dp.http://acm.dirring.com/problem.php?cid=1003&pi ...
- 转: Git远程操作详解 (阮一峰)
说明: 通过这个说明终于把远程操作给搞明白了. http://www.ruanyifeng.com/blog/2014/06/git_remote.html
- Android ART运行时与Dalvik虚拟机
这几天在做一个项目时需要在Android中使用OSGi框架(Apache Felix),于是在一个android 4.4.2 版本系统的某品牌的平板上实验. 实验内容很简单:把felix包里的feli ...
- 浅谈Android Fragment嵌套使用存在的一些BUG以及解决方法
时间 2014-03-18 18:00:55 eoe博客 原文 http://my.eoe.cn/916054/archive/24053.html 主题 安卓开发 自从Android3.0引入了F ...
- Android中实现如下多语言选择Radiobutton效果
手边的samsung手机设置多语言的方式一般是点击设置多语言的一栏后进入到多语言选择界面,选择完成之后当前的语言环境用小字方式直接显示在设置多语言栏的下方.另一种选择多语言的方式如上图所示,我也在系统 ...
- window10 安装出现the error code is 2503错误的解决方法
window10 安装出现the error code is 2503错误的解决方法: 设置 C:\WINDOWS\TEMP的权限
- 全球首发免费的MySql for Entity Framework Core
from:http://www.1234.sh/post/pomelo-data-mysql?utm_source=tuicool&utm_medium=referral Source 源代码 ...
- Apache Shiro 使用手册(一)Shiro架构介绍 - kdboy - ITeye技术网站
转载 原文地址 http://kdboy.iteye.com/blog/1154644 一.什么是Shiro Apache Shiro是一个强大易用的Java安全框架,提供了认证.授权.加密和会话管理 ...