如何使用lucene搜索文件
我想在文件“fdictionary.txt”中搜索查询,该文件包含逐行写入的单词列表(230,000 个单词)。有什么建议为什么这段代码不起作用? 拼写检查部分正在运行,并为我提供了建议列表(我将列表的长度限制为 1)。我想做的是搜索该词典,如果该词已经在其中,则不要调用拼写检查。我的搜索功能不起作用。它不会给我错误!这是我已经实现的:
public class SpellCorrection {
public static File indexDir = new File("/../idxDir");
public static void main(String[] args) throws IOException, FileNotFoundException, CorruptIndexException, ParseException {
Directory directory = FSDirectory.open(indexDir);
SpellChecker spell = new SpellChecker(directory);
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_20, null);
File dictionary = new File("/../fdictionary00.txt");
spell.indexDictionary(new PlainTextDictionary(dictionary), config, true);
String query = "red"; //kne, console
String correctedQuery = query; //kne, console
if (!search(directory, query)) {
String[] suggestions = spell.suggestSimilar(query, 1);
if (suggestions != null) {correctedQuery=suggestions[0];}
}
System.out.println("The Query was: "+query);
System.out.println("The Corrected Query is: "+correctedQuery);
}
public static boolean search(Directory directory, String queryTerm) throws FileNotFoundException, CorruptIndexException, IOException, ParseException {
boolean isIn = false;
IndexReader indexReader = IndexReader.open(directory);
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_20);
Term term = new Term(queryTerm);
Query termQuery = new TermQuery(term);
TopDocs hits = indexSearcher.search(termQuery, 100);
System.out.println(hits.totalHits);
if (hits.totalHits > 0) {
isIn = true;
}
return isIn;
}
}
I want to do a search for a query within a file "fdictionary.txt" containing a list of words (230,000 words) written line by line. any suggestion why this code is not working?
The spell checking part is working and gives me the list of suggestions (I limited the length of the list to 1). what I want to do is to search that fdictionary and if the word is already in there, do not call spell checking. My Search function is not working. It does not give me error! Here is what I have implemented:
public class SpellCorrection {
public static File indexDir = new File("/../idxDir");
public static void main(String[] args) throws IOException, FileNotFoundException, CorruptIndexException, ParseException {
Directory directory = FSDirectory.open(indexDir);
SpellChecker spell = new SpellChecker(directory);
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_20, null);
File dictionary = new File("/../fdictionary00.txt");
spell.indexDictionary(new PlainTextDictionary(dictionary), config, true);
String query = "red"; //kne, console
String correctedQuery = query; //kne, console
if (!search(directory, query)) {
String[] suggestions = spell.suggestSimilar(query, 1);
if (suggestions != null) {correctedQuery=suggestions[0];}
}
System.out.println("The Query was: "+query);
System.out.println("The Corrected Query is: "+correctedQuery);
}
public static boolean search(Directory directory, String queryTerm) throws FileNotFoundException, CorruptIndexException, IOException, ParseException {
boolean isIn = false;
IndexReader indexReader = IndexReader.open(directory);
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_20);
Term term = new Term(queryTerm);
Query termQuery = new TermQuery(term);
TopDocs hits = indexSearcher.search(termQuery, 100);
System.out.println(hits.totalHits);
if (hits.totalHits > 0) {
isIn = true;
}
return isIn;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您在哪里索引 fdictionary00.txt 的内容?
仅当您有index.html时,您才可以使用
IndexSearcher
进行搜索。如果您是 lucene 新手,您可能需要查看一些快速教程。 (如http://lucenetutorial.com/lucene-in-5-minutes.html)where are you indexing the content from fdictionary00.txt?
You can search using
IndexSearcher
, only when you have index. If you are new to lucene, you might want to check some quick tutorials. (like http://lucenetutorial.com/lucene-in-5-minutes.html)您从未建立过索引。
您需要设置索引...
然后需要创建一个文档并将每个术语作为分析字段添加到文档中。
然后将文档添加到索引中
现在构建索引并关闭索引编写器。
You never built the index.
You need to setup the index...
You then need to create a document and add each term to the document as an analyzed field..
Then add the document to the index
Now build the index and close the index writer.
您可以使您的
SpellChecker
实例在服务中可用并使用spellChecker.exist(word)
。请注意,
SpellChecker
不会索引 2 个字符或更少的单词。要解决此问题,您可以在创建索引后将它们添加到索引中(将它们添加到SpellChecker.F_WORD
字段中)。如果您想添加到实时索引并使其可用于
exist(word)
,那么您需要将它们添加到SpellChecker.F_WORD
字段。当然,因为您没有添加到所有其他字段(例如克/开始/结束等),所以您的单词不会显示为其他拼写错误单词的建议。在这种情况下,您必须将单词添加到文件中,以便在您重新创建索引时将其作为建议使用。如果该项目将 SpellChecker.createDocument(...) 设为公共/受保护,而不是私有,那就太好了,因为此方法通过添加单词来完成所有操作。
毕竟,您需要调用
spellChecker.setSpellIndex(directory)
。You could make your
SpellChecker
instance available in a service and usespellChecker.exist(word)
.Be aware that the
SpellChecker
will not index words 2 characters or less. To get around this you can add them to the index after you have created it (add them intoSpellChecker.F_WORD
field).If you want to add to your live index and make them available for
exist(word)
then you will need to add them to theSpellChecker.F_WORD
field. Of course, because you're not adding to all the other fields such as gram/start/end etc then your word will not appear as a suggestion for other misspelled words.In this case you'd have had to add the word into your file so when you re-create the index it would then be available as a suggestion. It would be great if the project made
SpellChecker.createDocument(...)
public/protected, rather than private, as this method accomplishes everything with adding words.After all this your need to call
spellChecker.setSpellIndex(directory)
.