java - 如何使用lucene搜索文件

标签 java 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;
}
}

最佳答案

您在哪里为 fdictionary00.txt 的内容建立索引?

只有当您有index.html时,您才可以使用IndexSearcher进行搜索。如果您是 lucene 新手,您可能需要查看一些快速教程。 (如http://lucenetutorial.com/lucene-in-5-minutes.html)

关于java - 如何使用lucene搜索文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8958871/

相关文章:

Solr 之间的语法

java - Firebase onDataChanged 调用了很多次(没有更新),有时使用空值

java - Spring Cloud 配置: define server config properties programmatically

java - 通过 AppEngine 调整 Jersey 的加载时间

lucene - Mac 上安装 Lucene 的步骤是什么?

elasticsearch - 为 SynonymFilterFactory 术语赋予权重

lucene - 如何使用 lucene 搜索 'and'?

java - 在默认浏览器中打开 URL

java - 尝试使用 Jackcess 和 JDBC 时抛出异常

java - 我将如何使用 Lucene 为客户建立索引