搜索引擎中往往有一个可选的搜索词的列表,当搜索结果太少时,可以帮助用户扩展搜索内容,或者搜索结果太多的时候可以帮助用户深入定向搜索。一种方法是从搜索日志中挖掘字面相似的词作为相关搜索词列表。另一种方法是把用户共同查询的词作为相关搜索词,需要有搜索日志才能实现。【摘自《Lucene In Action》】
下面使用的是第一种方法:
package com.tan.code;
//省略引入
public class RelateWords {
private static final String TEXT_FIELD = "text";
/**
*
* @param words 候選相関詞列表
* @param word 相關搜索詞的種子詞
* @return
* @throws IOException
* @throws ParseException
*/
static public String[] filterRelated(HashSet<String> words, String word)
throws IOException, ParseException {
//RAMDirectory ramDirectory = new RAMDirectory();
Directory directory=new SimpleFSDirectory(new File("E://related"));
IndexWriter indexWriter = new IndexWriter(directory,
new IndexWriterConfig(Version.LUCENE_43, new IKAnalyzer(true)));
for (String text : words) {
Document document = new Document();
document.add(new TextField(TEXT_FIELD, text, Store.YES));
indexWriter.addDocument(document);
}
indexWriter.close();
IndexReader indexReader = DirectoryReader.open(directory);
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
QueryParser queryParser = new QueryParser(Version.LUCENE_43,
TEXT_FIELD, new IKAnalyzer(true));
Query query = queryParser.parse(word);
TopDocs td = indexSearcher.search(query, 10);
ScoreDoc[] sd = td.scoreDocs;
String relateWords[] = new String[sd.length];
for (int i = 0; i < sd.length; i++) {
int z = sd[i].doc;
Document doc = indexSearcher.doc(z);
relateWords[i] = doc.get(TEXT_FIELD);
}
indexReader.close();
//ramDirectory.close();
directory.close();
return relateWords;
}
}
测试代码:
@Test
public void test() throws IOException, ParseException {
// fail("Not yet implemented");
HashSet<String> words = new HashSet<String>();
// words.add("Lucene");
// words.add("Lucene入門資料");
// words.add("java資料下載");
// words.add("SQL詳解");
// words.add("揭祕Lucene原理");
// words.add("Spring原理解析");
// words.add("什麽是Lucene?怎麽樣才可以學好Lucene呢?");
String word = "Spring資料";
String rewords[] = RelateWords.filterRelated(words, word);
System.out.println("搜索内容:" + word);
System.out.println("相關搜索匹配結果:");
for (int i = 0; i < rewords.length; i++) {
System.out.println(rewords[i]);
}
}
测试结果:
搜索内容:Spring資料
相關搜索匹配結果:
java資料下載
Lucene入門資料
Spring原理解析
分享到:
相关推荐
根据lucene最新版本5.5做同义词分析器,熟悉内部分析构造
lucene 近实时搜索 很清楚的解释了关于lucene近实时搜索的代码。很值得学习
主要是关于lucene站内搜索的技术代码,可以使用;按照需要进行代码修改。
Lucene实现全文搜索,支持英文、模糊和智能查询
c#下实现Lucene时间区间查询匹配。主要还是对Lucene查循对像Query的实现
lucene3.6 搜索例子
毕业设计:基于lucene的桌面搜索引擎
全文检索介绍 索引 分词 Lucene介绍 Lucene应用详解 索引器 检索器 条件查询 实用工具及高亮器 Lucene综合应用——仿搜索引擎
用LUCENE.NET开发搜索引擎,可将sercher\api文件夹中内容换成爬虫爬到的网页图片,可以直接运行的代码
基于Lucene的全文搜索引擎研究与应用.pdf 详实的介绍Lucene的架构设计分析
lucene_heritrix 搜索引擎 很好的东西来看看吧
完整的《Lucene+nutch搜索引擎开发》PDF版一共83.6M,无奈我上传的最高限是80M,所以切成两个。这一个是主文件,还需要下载一个副文件Lucene+nutch搜索引擎开发.z01。解压时直接放到一起,解压这个主文件就行了。
基于Lucene的搜索引擎应用与研究
lucene 高级搜索项目 附件搜索 附件内容搜索 全文搜索
Lucene nutch 搜索引擎开发 Part1
基于Lucene的搜索策略研究
weblucene,站内搜索,制作自己的站内搜索,希望能有所帮助!
lucene.net及.net爬虫实现的简单搜索引擎,为缓解读写速度与爬取速度不匹配,使用redis缓存数据库作为中间件来实现缓存机制
学习Lucene和Nutch的入门书籍,学习搜索引擎开发值得一看的书籍
Lucene+nutch搜索引擎开发(全本2-1),本电子书共两部分