Lucene 自身内置了许多查询对象,常用的有TermQuery、TermRangeQuery、NumericRangeQuery、PrefixQuery、WildcardQuery、FuzzyQuery、BooleanQuery、PhraseQuery.它们分别提供了不同形式的查询方式.分别看一下它们是如何使用的:
创建一个测试索引
public static void index(boolean hasIndex) { int[] ids = {0,1,2,3,4,5}; String[] emails = {"lfd@foxmail.com","lfd@qq.com","lfx@qq.com","lfx@foxmail.com","hll@gcp.edu","zzp@gcp.edu"}; String[] contents = { "incididunt ut labore et dolore magna aliqua. Ut enim ad lorem. ", "Lorem ipsum dolor sit amet lorem consectetur adipisicing", "dolor in reprehenderit in voluptate velit esse cillum nostrud exercitation ullamco laboris. ", "dolor in reprehenderit in voluptate velit esse cillum nostrud exercitation ullamco laboris. ", "Lorem ipsum dolor sit amet, consectetur adipisicing elit", "Consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna " }; String[] names = {"zzp","lfd","lfx","tom","huanglili","tzp"}; IndexWriter writer = null ; Directory directory = null ; try { directory = FSDirectory.open(new File("D:/Lucene")) ; //directory = new RAMDirectory() ; //索引文件在内存 writer = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_46, new StandardAnalyzer(Version.LUCENE_46))) ; //是否重新构建索引 if(hasIndex) { writer.deleteAll() ; } int count = names.length ; for(int i=0; i<count; i++) { Document doc = new Document() ; /* 首先是一个不变的属性值,这类字段还有一个主要用途, * 就是可以用于对搜索的返回结果集排序或是按范 * 围查询FloatField DoubleField IntField LongField BinaryDocValuesField NumericDocValuesField SortedDocValuesField SortedSetDocValuesField StoredField 整个域要存储的 StringField 是一个不需要分词,而直接用于索引的字符串 TextField 是一大块需要经过分词的文本 FieldType fieldType = new FieldType(); fieldType.setIndexed(true);//set 是否索引 fieldType.setStored(true);//set 是否存储 fieldType.setTokenized(false);//set 是否分词*/ doc.add(new IntField("id", ids[i], Store.YES)) ; doc.add(new StringField("email", emails[i], Store.YES)); doc.add(new TextField("content", contents[i], Store.YES)) ; FieldType type = new FieldType() ; type.setIndexed(true) ; type.setStored(true) ; doc.add(new Field("name", names[i], type)) ; writer.addDocument(doc) ; } writer.commit() ; } catch (IOException e) { e.printStackTrace(); } finally { try { if(writer != null) { writer.close() ; writer = null ; } } catch (IOException e) { e.printStackTrace(); } } }
为子方便,这里提取了一个方法获取IndexSearcher
private static IndexSearcher getSearcher(Directory directory) { try { if(reader == null) { reader = DirectoryReader.open(directory) ; } else { DirectoryReader ir = DirectoryReader.openIfChanged(reader) ; if(ir != null) { reader.close() ; reader = ir ; } } } catch (IOException e) { e.printStackTrace(); } IndexSearcher searcher = new IndexSearcher(reader) ; return searcher ; }
这里的openIfChanged方法:判断索引是否发生改变,如果发生变化则重新生成DirectoryReader.
TermQuery对象介绍:搜索指定Field中配置的内容.
public static void searcher01() { IndexSearcher searcher = getSearcher(directory) ; Query query = new TermQuery(new Term("content", "visited")) ; try { TopDocs topDocs = searcher.search(query, 10) ; ScoreDoc[] scores = topDocs.scoreDocs ; int length = scores.length ; for(int i=0; i<length; i++) { Document doc = searcher.doc(scores[i].doc) ; System.out.println("id:" + doc.get("id") + " email:" + doc.get("email") + " content:" + doc.get("content") + " name:" + doc.get("name")); } } catch (IOException e) { e.printStackTrace(); } }
new TermQuery(new Term("content", "visited")) ; 传入一个Term,搜索Field为"content"中含有"visited"的内容
TermRangeQuery对象介绍:搜索字符边界
public static void searcher02() { Query query = new TermRangeQuery("name", new BytesRef("a") , new BytesRef("i"), true, true) ; IndexSearcher searcher = getSearcher(directory) ; try { TopDocs docs = searcher.search(query, 100) ; ScoreDoc[] scores = docs.scoreDocs ; for(ScoreDoc score : scores) { Document doc = searcher.doc(score.doc) ; System.out.println("id:" + doc.get("id") + " email:" + doc.get("email") + " content:" + doc.get("content") + " name:" + doc.get("name")); } } catch (IOException e) { e.printStackTrace(); } }
new TermRangeQuery("name", new BytesRef("a") , new BytesRef("i"), true, true) ;搜索Field为"name",字符开头从a到i的内容.后边两个true分别表示是否包含开始字符,是否包含结束字符.
NumericRangeQuery对象介绍:搜索数字的开始和结束内容.
public static void searcher03() { Query query = NumericRangeQuery.newIntRange("id", 1, 4, true, true) ; IndexSearcher searcher = getSearcher(directory) ; try { TopDocs docs = searcher.search(query, 100) ; ScoreDoc[] scores = docs.scoreDocs ; for(ScoreDoc score : scores) { Document doc = searcher.doc(score.doc) ; System.out.println("id:" + doc.get("id") + " email:" + doc.get("email") + " content:" + doc.get("content") + " name:" + doc.get("name")); } } catch (IOException e) { e.printStackTrace(); } }
NumericRangeQuery.newIntRange("id", 1, 4, true, true) ;搜索Field为"id"标识从1到4的内容,后面两个true分别表示是否包含1,是否包含4
PrefixQuery查询对象:匹配单词的开始
//单词的开始 public static void searcher04() { Query query = new PrefixQuery(new Term("content", "in")) ; IndexSearcher searcher = getSearcher(directory) ; try { TopDocs docs = searcher.search(query, 100) ; ScoreDoc[] scores = docs.scoreDocs ; for(ScoreDoc score : scores) { Document doc = searcher.doc(score.doc) ; System.out.println("id:" + doc.get("id") + " email:" + doc.get("email") + " content:" + doc.get("content") + " name:" + doc.get("name")); } } catch (IOException e) { e.printStackTrace(); } }
new PrefixQuery(new Term("content", "in")) ; 搜索Fiild为"content"中单词开始为"in"的内容.
WildcardQuery查询对象:单词通配符搜索
public static void searcher05() { Query query = new WildcardQuery(new Term("content", "*tate")) ; IndexSearcher searcher = getSearcher(directory) ; try { TopDocs docs = searcher.search(query, 100) ; ScoreDoc[] scores = docs.scoreDocs ; for(ScoreDoc score : scores) { Document doc = searcher.doc(score.doc) ; System.out.println("id:" + doc.get("id") + " email:" + doc.get("email") + " content:" + doc.get("content") + " name:" + doc.get("name")); } } catch (IOException e) { e.printStackTrace(); } }
WildcardQuery(new Term("content", "*tate")."*"表示任意内容.即搜索Field为"content"中单词以tate结束的内容
FuzzyQuery查询对象:模糊匹配
相关推荐
全局搜索lucene4.6版本开发需要的jar包
创建索引,查询,过滤,同义词,近及时搜索,里面有4.6与5.0的jar文件,请自行引入
18.lucene4.6索引的相关操作 19.lucene4.6的各种Query(1) 20.lucene4.6的各种Query(2) 21.lucene4.6的各种Query(3) 22.solr4.6的快速搭建 23.solr4.6索引的相关操作 24.solr4.6搜索的相关参数功能(1) 25.solr4.6搜索...
lucene4.6所有jar包 lucene 搜索引擎 可以网上找教程,但是貌似网上的都太旧了, 4.6改动挺大的,构造函数都有很大差异~~无语啊!
lucene4.6实例,其中用到的jar包到官网下载.
lucene
Lucene是一套用于全文检索和搜寻的开源程式库,由Apache软件基金会支持和提供。Lucene提供了一个简单却强大的应用程式接口,能够做全文索引和搜寻。 从之前发布其他chm文件下载用户的反映看,有不少朋友反映下载后...
整个项目的构建全部采用最新技术,包括但不限于以下技术:struts 2.3.16 +spring 4.0.1 +hibernate 4.3.1+jquery-easyui 1.3.5+lucene 4.6.0+solr 4.6.0+Heritrix1.14并对其做了二次封装。为了增进学员的理解,课程...
NULL 博文链接:https://love-66521.iteye.com/blog/2039912
NULL 博文链接:https://sharp-fcc.iteye.com/blog/2038339
Lucene4.6版本,适用于Lucene的所有研究,以及中文分词功能
Lucene3.0之查询处理(1):原理和查询类型 各种Query对象详解
在Eclipse环境中运用java,Lucene建索引及查询关键字
lucene引擎和sqlite数据库分词查询,统计单词频率,统计重点单词,重点句子
lucene,lucene教程,lucene讲解。 为了对文档进行索引,Lucene 提供了五个基础的类 public class IndexWriter org.apache.lucene.index.IndexWriter public abstract class Directory org.apache.lucene.store....
Lucene创建索引,查询索引的简单使用。
lucene实例lucene实例lucene实例lucene实例lucene实例lucene实例lucene实例lucene实例lucene实例
而第二种思路就是,我直接从搜索到的结果集种显示前十条给第一页显示,第二页的时候,我在查询一次,给出10-20条数据给第二页显示,我每次翻页都要重新查询 第三种思路就变成了 我第一页仅需要10条数据,但是我一次读出来...
Lucene各版本间变化较大,lucene官方的turtial里面很多还是lucene-3.x.x的版本,这是Lucene实战(中文版第二版)对应Lucene版本,有需要的拿去用。
c#下实现Lucene时间区间查询匹配。主要还是对Lucene查循对像Query的实现