用Lucene实现在检索结果中再检索
2007-07-06 15:26
246 查看
http://www.matrix.org.cn/thread.shtml?topicId=753ba0a5-125e-11dc-b33a-df989147150e&forumId=32
Lucene是可以做到的,利用lucene的Filter,具体可以查看lucene的api中的org.apache.lucene.search.CachingWrapperFilter,它可以缓存上次的搜索结果,从而实现在结果中的搜索。
测试实例:
package com.wsjava;
import java.io.IOException;
import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.CachingWrapperFilter;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryFilter;
public class IndexTest {
/**
* @param args
* @throws ParseException
* @throws IOException
*/
public static void main(String[] args) throws IOException, ParseException {
index();
search("day"); //简单搜索
searchInResult("day", "you"); //在结果集中搜索
}
public static void index() throws IOException {
IndexWriter writer = new IndexWriter("d:/tesindex",new SimpleAnalyzer(), true);
writer.setMaxMergeDocs(1000);
writer.setMergeFactor(100);
for (int i = 0; i < 10; i++) {
Document doc = new Document();
String content = "How do you do?";
if (i >= 5) {
content = "What's a good day. ";
}
if (i >= 7) {
content = "Nice day. Thanks you!";
}
doc.add(new Field("content", content, Field.Store.YES,Field.Index.TOKENIZED));
writer.addDocument(doc);
}
}
//简单实现对qw的搜索.
public static void search(String qw) throws IOException, ParseException {
QueryParser queryParser = new QueryParser("content",new SimpleAnalyzer());
Query query = queryParser.parse(qw.trim());
QueryFilter filter = new QueryFilter(query);
search(query, filter);
}
//在搜索oldqw的结果集中搜索qw.
public static void searchInResult(String qw, String oldqw) throws ParseException, IOException {
QueryParser queryParser = new QueryParser("content",new SimpleAnalyzer());
Query query = queryParser.parse(qw.trim());
Query oldQuery = queryParser.parse(oldqw.trim());
QueryFilter oldFilter = new QueryFilter(oldQuery);
CachingWrapperFilter filter = new CachingWrapperFilter(oldFilter);
search(query, filter);
}
private static void search(Query query, Filter filter) throws IOException, ParseException {
IndexSearcher ins = new IndexSearcher("d:/tesindex");
Hits hits = ins.search(query, filter);
for (int i = 0; i < hits.length(); i++) {
Document doc = hits.doc(i);
System.out.println(doc.get("content"));
}
System.out.println();
}
}
上面是简单的测试程序。当然在实际应用中可以做得比较复杂。
Lucene是可以做到的,利用lucene的Filter,具体可以查看lucene的api中的org.apache.lucene.search.CachingWrapperFilter,它可以缓存上次的搜索结果,从而实现在结果中的搜索。
测试实例:
package com.wsjava;
import java.io.IOException;
import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.CachingWrapperFilter;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryFilter;
public class IndexTest {
/**
* @param args
* @throws ParseException
* @throws IOException
*/
public static void main(String[] args) throws IOException, ParseException {
index();
search("day"); //简单搜索
searchInResult("day", "you"); //在结果集中搜索
}
public static void index() throws IOException {
IndexWriter writer = new IndexWriter("d:/tesindex",new SimpleAnalyzer(), true);
writer.setMaxMergeDocs(1000);
writer.setMergeFactor(100);
for (int i = 0; i < 10; i++) {
Document doc = new Document();
String content = "How do you do?";
if (i >= 5) {
content = "What's a good day. ";
}
if (i >= 7) {
content = "Nice day. Thanks you!";
}
doc.add(new Field("content", content, Field.Store.YES,Field.Index.TOKENIZED));
writer.addDocument(doc);
}
}
//简单实现对qw的搜索.
public static void search(String qw) throws IOException, ParseException {
QueryParser queryParser = new QueryParser("content",new SimpleAnalyzer());
Query query = queryParser.parse(qw.trim());
QueryFilter filter = new QueryFilter(query);
search(query, filter);
}
//在搜索oldqw的结果集中搜索qw.
public static void searchInResult(String qw, String oldqw) throws ParseException, IOException {
QueryParser queryParser = new QueryParser("content",new SimpleAnalyzer());
Query query = queryParser.parse(qw.trim());
Query oldQuery = queryParser.parse(oldqw.trim());
QueryFilter oldFilter = new QueryFilter(oldQuery);
CachingWrapperFilter filter = new CachingWrapperFilter(oldFilter);
search(query, filter);
}
private static void search(Query query, Filter filter) throws IOException, ParseException {
IndexSearcher ins = new IndexSearcher("d:/tesindex");
Hits hits = ins.search(query, filter);
for (int i = 0; i < hits.length(); i++) {
Document doc = hits.doc(i);
System.out.println(doc.get("content"));
}
System.out.println();
}
}
上面是简单的测试程序。当然在实际应用中可以做得比较复杂。
相关文章推荐
- 用lucene实现在上次检索结果中再检索
- JAVA_WEB项目之Lucene实现检索结果排序和关键字在索引库中多字段查询结果进行高亮显示
- 实现lucene检索结果排序
- java实现搜索引擎,全文检索,超大数据量查询,lucene
- 用PHP调用Lucene包来实现全文检索(转)
- lucene全文搜索之四:创建索引搜索器、6种文档搜索器实现以及搜索结果分析(结合IKAnalyzer分词器的搜索器)基于lucene5.5.3
- Solr or Lucene全文检索实现原理
- 用PHP调用Lucene包来实现全文检索
- lucene全文检索学习记录,附带源码——三种实现,超全超细致
- 利用Lucene.net实现检索并对检索关键字高亮显示
- [原创]一种基于Python爬虫和Lucene检索的垂直搜索引擎的实现方法介绍
- mysql,实现数据库检索结果添加自增的序号
- ]NET Core Lucene.net和PanGu分词实现全文检索
- 站内搜索------仿造Baidu简单实现基于Lucene.net的全文检索的功能
- [转]用PHP调用Lucene包来实现全文检索
- 使用Lucene-Spatial实现集成地理位置的全文检索
- 使用Lucene实现多个文档关键词检索demo(一)
- asp.net中利用Lucene.net实现检索并对检索关键字高亮显示
- js实现下拉框根据输入值检索结果
- Lucene实现在线全文检索的总结