Lucene:ID删除不起作用

时间:2017-03-29 09:48:46

标签: java search indexing lucene

我正在努力将Lucene整合到我们的项目中。目前我在保存和搜索方面没有任何问题,但是按ID删除无效。我有什么想法吗?

保存代码:

  @Override
    public void saveIndexes(String text,String filePath, long groupId, boolean type, int objectId)

    Directory directory = org.apache.lucene.store.FSDirectory.open(path);
            IndexWriterConfig config = new IndexWriterConfig(new SimpleAnalyzer());
            IndexWriter indexWriter = new IndexWriter(directory, config);
            doc.add(new TextField("id",String.valueOf(objectId),Field.Store.YES));
  indexWriter.addDocument(doc);
            indexWriter.commit();
            indexWriter.close();
            directory.close();
}

删除代码:

 @Override
    public void deleteById(long groupId,int objectId, boolean type) {
        try {
            Path path = //Path to index directory;
            Directory directory = org.apache.lucene.store.FSDirectory.open(path);
            IndexWriterConfig config = new IndexWriterConfig(new SimpleAnalyzer());
            IndexWriter indexWriter = new IndexWriter(directory, config);
            indexWriter.deleteDocuments(new Term("id",String.valueOf(objectId)));
            indexWriter.commit();
            indexWriter.close();
            directory.close();
        }catch (Exception e){
            e.printStackTrace();
        }

    }

1 个答案:

答案 0 :(得分:2)

可能是因为您使用Textfield索引ID。使用Stringfield索引ID。

原因:Stringfields不会像doc所说的那样被标记化:

  

索引但未标记化的字段:整个String值被索引为单个标记。例如,这可能会用于“国家/地区”。字段或' id'字段

见这里:http://lucene.apache.org/core/6_4_1/core/org/apache/lucene/document/StringField.html