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 ...
随机推荐
- AC日记——字符串最大跨距 openjudge 1.7 26
26:字符串最大跨距 总时间限制: 1000ms 内存限制: 65536kB 描述 有三个字符串S,S1,S2,其中,S长度不超过300,S1和S2的长度不超过10.想检测S1和S2是否同时在S中 ...
- java 28 - 1 设计模式 之 面向对象思想设计原则和模版设计模式概述
在之前的java 23 中,了解过设计模式的单例模式和工厂模式.在这里,介绍下设计模式 面向对象思想设计原则 在实际的开发中,我们要想更深入的了解面向对象思想,就必须熟悉前人总结过的面向对象的思想的设 ...
- C#手工注入辅助工具
看了某牛出版的MySql手注天书一神书,基本上解决了SQL注入上的知识点,于是打完(酱油)省赛回来通宵了一晚上写了个工具 方便语句构造SQL 联合查询 报错注入 盲注 读写 命令执行 基本都有整合 遇 ...
- poj 2481
Cows Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 16163 Accepted: 5380 Description ...
- webapp开发需要注意的浏览器内核知识
Web App:1.开发成本较低使用web开发技术就可以轻松的完成web app的开发2.升级较简单升级不需要通知用户,在服务端更新文件即可,用户完全没有感觉3.维护比较轻松和一般的web一样,维护比 ...
- HTML 学习笔记(图像)
HTML 图像 图像标签(<img>)和源属性(Src) 在HTML中,图像由<img>标签定义. <img>是空标签,意思是说,他只包含属性,并且没有闭合标签 要 ...
- 小心 CSS3 Transform 引起的 z-index "失效"
https://www.douban.com/note/343402554/ http://www.jb51.net/css/255811.html 最后我直接removeClass;把transfo ...
- CodeGenerator.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace CiCe ...
- Parallel.Invoke并行你的代码
Parallel.Invoke并行你的代码 使用Parallel.Invoke并行你的代码 优势和劣势 使用Parallel.Invoke的优势就是使用它执行很多的方法很简单,而不用担心任务或者线程的 ...
- struts2: 玩转 rest-plugin
近期使用struts2的rest-plugin,参考官方示例struts2-rest-showcase,做了一个restful service小项目,但官网提供的这个示例过于简单,埋下了巨坑无数,下面 ...