搜索引擎中往往有一個可選的搜索詞的列表
下面使用的是第一種方法
[java]
package de;
//省略引入
public class RelateWords {
private static final String TEXT_FIELD =
/**
*
* @param words 候選相関詞列表
* @param word 相關搜索詞的種子詞
* @return
* @throws IOException
* @throws ParseException
*/
static public String[] filterRelated(HashSet
throws IOException
//RAMDirectory ramDirectory = new RAMDirectory();
Directory directory=new SimpleFSDirectory(new File(
IndexWriter indexWriter = new IndexWriter(directory
new IndexWriterConfig(Version
for (String text : words) {
Document document = new Document();
document
indexWriter
}
indexWriter
IndexReader indexReader = DirectoryReader
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
QueryParser queryParser = new QueryParser(Version
TEXT_FIELD
Query query = queryParser
TopDocs td = indexSearcher
ScoreDoc[] sd = td
String relateWords[] = new String[sd
for (int i =
int z = sd[i]
Document doc = indexSearcher
relateWords[i] = doc
}
indexReader
//ramDirectory
directory
return relateWords;
}
}
測試代碼
[java]
@Test
public void test() throws IOException
// fail(
HashSet
// words
// words
// words
// words
// words
// words
// words
String word =
String rewords[] = RelateWords
System
System
for (int i =
System
}
}
測試結果
[java]
搜索內容
相關搜索匹配結果
java資料下載
Lucene入門資料
Spring原理解析
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25641.html