Lucene_demo06_几种搜索
2015-10-16 09:11
465 查看
创建searcher的过程
1、创建Directory
2、根据directory创建indexReader
3、根据indexReader创建indexSearcher
4、创建搜索的Query
5、根据searcher搜索并且返回TopDocs
6、根据TopDocs获取ScordDoc对象获取具体的Document对象
7、根据searcher和ScordDoc对象获取具体的Document对象
8、根据Document对象获取需要的值
9、关闭reader
Java代码
/**
* @see 1、关键词查询
* @see 2、查询所有的文档 重点
* @see 3、范围查询
* @see 4、通配符查询 重点
* @see 5、短语查询
* @see 6、Boolean查询 重点
*/
public class QueryTest {
/**
* 关键词查询 * 因为在创建Term对象的时候,没有分词器,所以这里的字母是区分大小写的 * Term构造函数的第二个参数指的是关键词,必须存在
*/
@Test
public void testTermQuery() throws Exception {
Term term = new Term("title", "总冠军");
Query query = new TermQuery(term);
this.testSearchIndex(query);
}
/**
* 查询所有的文档
*/
@Test
public void testAllQuery() throws Exception {
Query query = new MatchAllDocsQuery();
this.testSearchIndex(query);
}
/**
* 通配符查询 说明: * 代表任意多个任意字符 ? 代表一个任意字符
*/
@Test
public void testWildCardQuery() throws Exception {
Term term = new Term("title", "*总?军");
Query query = new WildcardQuery(term);
this.testSearchIndex(query);
}
/**
* boolean查询 可以根据Occur的常量把好几个查询结合在一起
*/
@Test
public void testBooleanQuery() throws Exception {
Term term = new Term("title", "总冠军");
TermQuery termQuery = new TermQuery(term);
Term term2 = new Term("content", "2?13");
Query wildCardQuery = new WildcardQuery(term2);
BooleanQuery query = new BooleanQuery();
query.add(termQuery, Occur.SHOULD);// Occur.MUST必须有、Occur.MUST_NOT必须没有、Occur.SHOULD可以有
query.add(wildCardQuery, Occur.SHOULD);
this.testSearchIndex(query);
}
/**
* 范围查询 查询id范围在5~15间的数据
*/
@Test
public void testRangeQuery() throws Exception {
Query query = NumericRangeQuery.newLongRange("id", 5L, 15L, true, true);
this.testSearchIndex(query);
}
/**
* 所有的Term对象只能在同一个field中进行 如果两个以上大的关键词进行组合查询,得知道其中的位置(分词后的位置)
*/
@Test
public void testPharseQuery() throws Exception {
Term term = new Term("title", "NBA总冠军");
Term term2 = new Term("title", "NBA总冠军");
PhraseQuery phraseQuery = new PhraseQuery();
phraseQuery.add(term);
phraseQuery.add(term2);
this.testSearchIndex(phraseQuery);
}
// 公共输出方法
private void testSearchIndex(Query query) throws Exception {
IndexSearcher indexSearcher = new IndexSearcher(LuceneUtils.directory);
TopDocs topDocs = indexSearcher.search(query, 50);
int count = topDocs.totalHits;// 总的记录数
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
List<Article> articleList = new ArrayList<Article>();
for (int i = 0; i < scoreDocs.length; i++) {
int index = scoreDocs[i].doc;
Document document = indexSearcher.doc(index);
Article article = DocumentUtils.document2Article(document);
articleList.add(article);
}
// 输入搜索出来的内容
for (Article article : articleList) {
System.out.println(article.getId());
System.out.println(article.getTitle());
System.out.println(article.getContent());
}
}
}
1、创建Directory
2、根据directory创建indexReader
3、根据indexReader创建indexSearcher
4、创建搜索的Query
5、根据searcher搜索并且返回TopDocs
6、根据TopDocs获取ScordDoc对象获取具体的Document对象
7、根据searcher和ScordDoc对象获取具体的Document对象
8、根据Document对象获取需要的值
9、关闭reader
Java代码
/**
* @see 1、关键词查询
* @see 2、查询所有的文档 重点
* @see 3、范围查询
* @see 4、通配符查询 重点
* @see 5、短语查询
* @see 6、Boolean查询 重点
*/
public class QueryTest {
/**
* 关键词查询 * 因为在创建Term对象的时候,没有分词器,所以这里的字母是区分大小写的 * Term构造函数的第二个参数指的是关键词,必须存在
*/
@Test
public void testTermQuery() throws Exception {
Term term = new Term("title", "总冠军");
Query query = new TermQuery(term);
this.testSearchIndex(query);
}
/**
* 查询所有的文档
*/
@Test
public void testAllQuery() throws Exception {
Query query = new MatchAllDocsQuery();
this.testSearchIndex(query);
}
/**
* 通配符查询 说明: * 代表任意多个任意字符 ? 代表一个任意字符
*/
@Test
public void testWildCardQuery() throws Exception {
Term term = new Term("title", "*总?军");
Query query = new WildcardQuery(term);
this.testSearchIndex(query);
}
/**
* boolean查询 可以根据Occur的常量把好几个查询结合在一起
*/
@Test
public void testBooleanQuery() throws Exception {
Term term = new Term("title", "总冠军");
TermQuery termQuery = new TermQuery(term);
Term term2 = new Term("content", "2?13");
Query wildCardQuery = new WildcardQuery(term2);
BooleanQuery query = new BooleanQuery();
query.add(termQuery, Occur.SHOULD);// Occur.MUST必须有、Occur.MUST_NOT必须没有、Occur.SHOULD可以有
query.add(wildCardQuery, Occur.SHOULD);
this.testSearchIndex(query);
}
/**
* 范围查询 查询id范围在5~15间的数据
*/
@Test
public void testRangeQuery() throws Exception {
Query query = NumericRangeQuery.newLongRange("id", 5L, 15L, true, true);
this.testSearchIndex(query);
}
/**
* 所有的Term对象只能在同一个field中进行 如果两个以上大的关键词进行组合查询,得知道其中的位置(分词后的位置)
*/
@Test
public void testPharseQuery() throws Exception {
Term term = new Term("title", "NBA总冠军");
Term term2 = new Term("title", "NBA总冠军");
PhraseQuery phraseQuery = new PhraseQuery();
phraseQuery.add(term);
phraseQuery.add(term2);
this.testSearchIndex(phraseQuery);
}
// 公共输出方法
private void testSearchIndex(Query query) throws Exception {
IndexSearcher indexSearcher = new IndexSearcher(LuceneUtils.directory);
TopDocs topDocs = indexSearcher.search(query, 50);
int count = topDocs.totalHits;// 总的记录数
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
List<Article> articleList = new ArrayList<Article>();
for (int i = 0; i < scoreDocs.length; i++) {
int index = scoreDocs[i].doc;
Document document = indexSearcher.doc(index);
Article article = DocumentUtils.document2Article(document);
articleList.add(article);
}
// 输入搜索出来的内容
for (Article article : articleList) {
System.out.println(article.getId());
System.out.println(article.getTitle());
System.out.println(article.getContent());
}
}
}
相关文章推荐
- 深入坐标系 和坐标系相关的四个属
- PyQT信号槽_学习笔记 (Signals/Slots)
- C++ 远程 shell (cmd)
- iOS应用架构谈 网络层设计方案
- 黑马程序员——this,super,interface,final,abstract,static
- ios66详解之时间戳与时间的转换
- 第七周上机实践项目5——排队看病模拟
- SimpleDateFormat的线程安全问题
- 第四周项目3 单链表应用(3)
- nginx搭建https服务器
- 基于 Lucene 的8 个开源搜索引擎
- 什么是垂直搜索引擎
- AutoLayout代码布局使用大全—一种全新的布局思想
- angularJS与bootstrap结合实现动态加载弹出提示内容
- 第七周 项目4-队列数组
- 第六周项目1—建立顺序栈算法库
- 搜索引擎的工作原理
- Lucene_demo09_txt文件索引
- Linux:32/64位程序(应用程序、共享库、内核模块)
- 签劳动合同后可以辞职吗