【Lucene】挖掘相关搜索词
2013-09-16 16:04
260 查看
搜索引擎中往往有一个可选的搜索词的列表,当搜索结果太少时,可以帮助用户扩展搜索内容,或者搜索结果太多的时候可以帮助用户深入定向搜索。一种方法是从搜索日志中挖掘字面相似的词作为相关搜索词列表。另一种方法是把用户共同查询的词作为相关搜索词,需要有搜索日志才能实现。【摘自《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】挖掘相关搜索词
- 【Lucene 挖掘相关搜索词】
- lucene3.6.0的高级搜索相关技术
- lucene3.6.0的高级搜索相关技术
- Hack in Lucene.Net之为什么无法在搜索时统计分类下相关结果数或者实现Group By效果
- lucene的基本操作-搜索相关
- 一个专业搜索公司关于lucene+solar资料
- 经典的机器学习方面源代码库(非常全,数据挖掘,计算机视觉,模式识别,信息检索相关领域都适用的了)
- 发一个Lucene 2.4.0对搜索结果高亮显示的代码
- Lucene.NET建立,搜索多个索引文件
- lucene4.5源码分析系列:搜索过程
- Lucene的多种高级搜索形式
- 【搜索那些事】细谈lucene(一)初识全文资源检索框架lucene
- [转]用Lucene.net对数据库建立索引及搜索
- Java 全文搜索框架 Lucene
- Lucene in action 笔记 term vector——针对特定field建立的词频向量空间,不存!不会!影响搜索,其作用是告诉我们搜索结果是“如何”匹配的,用以提供高亮、计算相似度,在VSM模型中评分计算
- 关于搜索的相关收集
- 基于lucene的搜索方案
- Lucene.net 多条件查询搜索
- lucene之排序、设置权重、优化、分布式搜索(转)