lucene-查询query->WildcardQuery使用通配符搜索
Lucene也提供了通配符的查询,这就是WildcardQuery。
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.WildcardQuery;
public class WildcardQueryTest {
public static void main(String[] args) throws Exception {
//生成Document对象,下同
Document doc1 = new Document();
//添加“content”字段的内容,下同
doc1.add(Field.Text("content", "whatever"));
//添加“title”字段的内容,下同
doc1.add(Field.Keyword("title", "doc1"));
Document doc2 = new Document();
doc2.add(Field.Text("content", "whoever"));
doc2.add(Field.Keyword("title", "doc2"));
Document doc3 = new Document();
doc3.add(Field.Text("content", "however"));
doc3.add(Field.Keyword("title", "doc3"));
Document doc4 = new Document();
doc4.add(Field.Text("content", "everest"));
doc4.add(Field.Keyword("title", "doc4"));
//生成索引书写器
IndexWriter writer = new IndexWriter("c://index",
new StandardAnalyzer(), true);
//将文档对象添加到索引中
writer.addDocument(doc1);
writer.addDocument(doc2);
writer.addDocument(doc3);
writer.addDocument(doc4);
//关闭索引书写器
writer.close();
//生成索引书写器
IndexSearcher searcher = new IndexSearcher("c://index");
//构造词条
Term word1 = new Term("content", "*ever");
Term word2 = new Term("content", "wh?ever");
Term word3 = new Term("content", "h??ever");
Term word4 = new Term("content", "ever*");
//生成WildcardQuery对象,初始化为null
WildcardQuery query = null;
//用于保存检索结果
Hits hits = null;
query = new WildcardQuery(word1);
//开始第一次检索,并返回检索结果
hits = searcher.search(query);
//输出检索结果的相关信息
printResult(hits, "*ever");
query = new WildcardQuery(word2);
//开始第二次检索,并返回检索结果
hits = searcher.search(query);
//输出检索结果的相关信息
printResult(hits, "wh?ever");
query = new WildcardQuery(word3);
//开始第三次检索,并返回检索结果
hits = searcher.search(query);
//输出检索结果的相关信息
printResult(hits, "h??ever");
query = new WildcardQuery(word4);
//开始第四次检索,并返回检索结果
hits = searcher.search(query);
//输出检索结果的相关信息
printResult(hits, "ever*");
}
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();
}
}
}
}
由上述代码可以看出,通配符“?”代表1个字符,而“*”则代表0至多个字符。不过通配符检索和上面的FuzzyQuery由于需要对字段关键字进行字符串匹配,所以,在搜索的性能上面会受到一些影响。
lucene-查询query->WildcardQuery使用通配符搜索的更多相关文章
- Lucene 查询(Query)子类
QueryParser(单域查询) QueryParser子类对单个域查询时创建查询query,构造方法中需要传入Lucene版本号,检索域名和分词器. QueryParser parser = ne ...
- lucene 查询的使用
各种查询方式一:使用QueryParser与查询语法.(会使用分词器) MultiFieldQueryParser查询字符串 ------------------------> Query对象 ...
- 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{ ...
- lucene查询解析器语法
注意:使用QueryParser查询,关键词是会被分词的,如果不需要分词,可以选择使用Lucene提供的API查询类. Lucene提供了丰富的API来组合定制你所需要的查询器,同时也可以利用Quer ...
- query_string查询支持全部的Apache Lucene查询语法 低频词划分依据 模糊查询 Disjunction Max
3.3 基本查询3.3.1词条查询 词条查询是未经分析的,要跟索引文档中的词条完全匹配注意:在输入数据中,title字段含有Crime and Punishment,但我们使用小写开头的crime来搜 ...
- 使用 Apache Lucene 和 Solr 4 实现下一代搜索和分析
使用 Apache Lucene 和 Solr 4 实现下一代搜索和分析 使用搜索引擎计数构建快速.高效和可扩展的数据驱动应用程序 Apache Lucene™ 和 Solr™ 是强大的开源搜索技术, ...
- 基于Lucene查询原理分析Elasticsearch的性能
前言 Elasticsearch是一个很火的分布式搜索系统,提供了非常强大而且易用的查询和分析能力,包括全文索引.模糊查询.多条件组合查询.地理位置查询等等,而且具有一定的分析聚合能力.因为其查询场景 ...
- Lucene查询语法详解
Lucene查询 Lucene查询语法以可读的方式书写,然后使用JavaCC进行词法转换,转换成机器可识别的查询. 下面着重介绍下Lucene支持的查询: Terms词语查询 词语搜索,支持 单词 和 ...
- Lucene查询索引(分页)
分页查询只需传入每页显示记录数和当前页就可以实现分页查询功能 Lucene分页查询是对搜索返回的结果进行分页,而不是对搜索结果的总数量进行分页,因此我们搜索的时候都是返回前n条记录 package c ...
随机推荐
- Unity打包同一文件Hash不一样
问题起因 游戏开发基本都会涉及到资源版本管理及更新,本文记录我在打包过程中遇到的一小问题: 开过中常用于标记资源版本的方法有计算文件Hash.VCS的版本等. 在Unity中对同一个资源文件进行多次打 ...
- Application中的路径
前提条件 项目工程目录:E:/Work/cosmosbox/cb-client/ 我电脑当前的用户名:qingqing PersistentDataPath Application.persisten ...
- Windows英文版GitHub客户端使用操作流程图文攻略教程现没中文版
Git是一个分布式的版本控制系统,最初由Linus Torvalds编写,用作Linux内核代码的管理.作为一个程序员,我们需要掌握其用法. 作为开源代码库以及版本控制系统,Github目前拥有140 ...
- Hibernate延迟加载Lazy
Hibernate延迟加载Lazy 延迟加载(lazy load)又称为懒加载,延迟加载的机制是为了避免一些无谓性能的开销而提出来的,所谓延迟加载就是当在真正需要数据的时候,才真正执行数据加载操作 如 ...
- 当手机被PS掉,人们看到的是手中的灵魂
Eric Pickersgill是一名摄影师,最近喜欢拍摄并记录人们使用智能手机的情景,不过不同的是,在最终作品中会将手机从人们手中PS掉,一刹那会进入一个奇怪的世界.黑白照片也极具冲击力. 每个人神 ...
- vijos1037搭建双塔(一维背包问题)
描述 2001年9月11日,一场突发的灾难将纽约世界贸易中心大厦夷为平地,Mr. F曾亲眼目睹了这次灾难.为了纪念“9?11”事件,Mr. F决定自己用水晶来搭建一座双塔. Mr. F有N块水晶,每块 ...
- 在表单中元素的onchange事件的兼容性问题
onchange:在值发生改变的时候触发 text:当光标离开的时候如果内容有变化就触发 radio/check:标准浏览器下点击的时候只要值变了就触发 非标准浏览器下焦点离开的时候如果值变了就触发
- win2008server R2 x64 部署.net core到IIS上出现【Failed to load the dll from [C:\Program Files\dotnet\host\fxr\1.0.1\hostfxr.dll], HRESULT: 0x80070057】错误
win2008server R2 x64 部署.net core到IIS上出现[Failed to load the dll from [C:\Program Files\dotnet\host\fx ...
- matrix-tree
学一发matrix-tree 原来bzoj1016这题是暴搜+玄学并查集过的-数据弱怪我咯 首先matrix-tree需要度数矩阵D,就是说当i=j时D[i][j]为i的度数,否则为0. 还有邻接矩阵 ...
- Codevs 1910递归函数
1910 递归函数 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 对于一个递归函数w(a, b, c). 如果a <= ...