java - Lucene:使用 RAMDictionary 进行拼写检查似乎不起作用

标签 java lucene lucene.net spell-checking

我想使用目录作为 RAMDirectory 而不是使用 FSDirectory 来索引我的拼写检查器。因为我已经创建了索引,所以我只想使用该用户索引来索引拼写检查器,但我没有得到任何建议。exist 也返回 0,所以我猜测索引没有正确创建。

try{

        StandardAnalyzer analyzer = new StandardAnalyzer();
        IndexWriterConfig config = new IndexWriterConfig(analyzer);
        Directory directory = new RAMDirectory();

        IndexWriter indexWriter = new IndexWriter(directory,config);

        JSONArray documentArray = new JSONArray();
        String[] fieldArray = {"field"};
        JSONObject documentObj= new JSONObject();
        documentObj.put("field", "KARNATAKA");

        documentArray.put(documentObj);

        JSONObject documentObj1= new JSONObject();
        documentObj1.put("field", "KERALA");

        documentArray.put(documentObj1);

        for (int i = 0; i < documentArray.length(); i++)
        {
            JSONObject docObj = documentArray.getJSONObject(i);
            Document doc = new Document();
            for (int j = 0; j < fieldArray.length; j++)
            {
              doc.add(new Field(fieldArray[j], docObj.getString(fieldArray[j]), org.apache.lucene.document.TextField.TYPE_STORED));
            }
            indexWriter.addDocument(doc);
          }


        indexWriter.commit();
        indexWriter.close();


        DirectoryReader ireader = DirectoryReader.open(directory);
        SpellChecker spellChecker = new SpellChecker(directory);
        spellChecker.clearIndex();

        spellChecker.indexDictionary(new LuceneDictionary(ireader, "field"), new IndexWriterConfig(new StandardAnalyzer()),true);

        String[] suggestions = spellChecker.suggestSimilar("KANATAKA", 5);

        System.out.println(spellChecker.exist("Karnataka".toUpperCase()));
        if(suggestions.length > 0 )
            System.out.println(suggestions[0]);
        spellChecker.close();




    }
    catch(Exception e)
    {
        e.printStackTrace();
    }

请帮助我可能做错了什么

最佳答案

SpellChecker.clearIndex() 的文档说:

Removes all terms from the spell check index.

我看起来这不是你想做的事。我会删除该行,或者可能更好,只需为拼写检查器索引使用一个新目录,而不是使用与源索引相同的目录。

关于java - Lucene:使用 RAMDictionary 进行拼写检查似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44881512/

相关文章:

c# - 如何更新 lucene.net 索引中的字段而不是更新所有索引的示例

c# - 混合 Lucene/MySQL 查询或概念

full-text-search - 如何在 LuceneNet 中使用 indexWriter 删除文档

java - SpringBoot - JPA 保留 @OneToMany 关系 key 重复条目 '1'

java - 使用 Java 从 LDAP 检索所有用户及其角色

java - 如何处理企业环境中的长工作线程

java - 将 Objectify 与 GAE 一起使用时 ObjectifyService 的 NoClassDefFoundError

java - 如何创建可插入的 Java 程序?

lucene - Play Framework 2 的搜索模块

database - 如何在 Grafana 中查询 Elasticsearch