码迷,mamicode.com
首页 > 其他好文 > 详细

PriexQuery前缀搜索

时间:2015-03-30 16:23:56      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

/*
 * 这段代码写的是BooleanQuery布尔搜索
 * 顾名思义就是布尔型查询。
 * 布尔型,是由多个子句和子句间的布尔逻辑所组成的查询
 * */
package query;


import java.io.IOException;


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.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.TermQuery;


public class Query_Boolean {

public Query_Boolean(String INDEX_STORE){
try{
IndexWriter writer = new IndexWriter(INDEX_STORE, new StandardAnalyzer(), true);
writer.setUseCompoundFile(false);

//创建3个文档
Document doc1 = new Document();
Document doc2 = new Document();
Document doc3 = new Document();

Field f1 = new Field("bookname", "钢铁是怎样炼成的", Field.Store.YES, Field.Index.TOKENIZED);
Field f2 = new Field("bookname", "英雄儿女", Field.Store.YES, Field.Index.TOKENIZED);
Field f3 = new Field("bookname", "篱笆女人和狗", Field.Store.YES, Field.Index.TOKENIZED);

doc1.add(f1);
doc2.add(f2);
doc3.add(f3);

writer.addDocument(doc1);
writer.addDocument(doc2);
writer.addDocument(doc3);
writer.close();

IndexSearcher searcher = new IndexSearcher(INDEX_STORE);

//创建两个词条对象
Term t1 = new Term("bookname","女");
Term t2 = new Term("bookname","狗");

//创建2个TermQuery对象
TermQuery q1 = new TermQuery(t1);
TermQuery q2 = new TermQuery(t2);

//构建Boolean对象
BooleanQuery query = new BooleanQuery();

//将两个TermQuery加入到Boolea的子句中去,且关系均为必须满足
query.add(q1, BooleanClause.Occur.MUST);
query.add(q2, BooleanClause.Occur.MUST);


//打印查询结构
Hits hits =searcher.search(query);
for(int i = 0; i < hits.length(); i++){
System.out.println(hits.doc(i));
}
}catch(IOException e){
e.printStackTrace();
}
}


public static void main(String[] args) {
// TODO Auto-generated method stub
Query_Boolean qb = new Query_Boolean("E:\\Lucene项目\\索引文件");
}


}

PriexQuery前缀搜索

标签:

原文地址:http://blog.csdn.net/u012965373/article/details/44752471

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!