Lucene 4.3删除

Lucene 4.3删除,lucene,Lucene,我在从我的LuceneIndex中删除文档时遇到问题。对于我的删除,我使用了termquery。在我注意到文档没有被删除后,我尝试先搜索文档,但没有找到任何文档 我存储文档的方式如下: public boolean storeNote(Note note) throws Exception { Document doc = new Document(); this.initalizeWriter(OpenMode.CREATE_OR_APPEND);

我在从我的
LuceneIndex
中删除文档时遇到问题。对于我的删除,我使用了termquery。在我注意到文档没有被删除后,我尝试先搜索文档,但没有找到任何文档

我存储文档的方式如下:

  public boolean storeNote(Note note) throws Exception {

        Document doc = new Document();

        this.initalizeWriter(OpenMode.CREATE_OR_APPEND);
        if (note != null && note.getUri() != null && note.getPatientUri() != null && !note.getUri().isEmpty()
                && !note.getPatientUri().isEmpty()) {

            doc.add(new TextField(URINOTE, note.getUri(), Field.Store.YES));
            doc.add(new TextField(URIPATIENT, note.getPatientUri(), Field.Store.YES));

            if (note.getTitle() != null && !note.getTitle().isEmpty()) {
                doc.add(new TextField(NOTETITLE, note.getTitle(), Field.Store.YES));
            }

            if (note.getText() != null && !note.getText().isEmpty()) {
                doc.add(new TextField(NOTETEXT, note.getText(), Field.Store.YES));
            }

        }

        try 
        {
        this.writer.addDocument(doc);
        }
        catch(Exception e)
        {
            LOGGER.info("DocSave Failed \n" + e.getMessage());
            return false;
        }
        finally
        {
            // Ensure that index is closed. Open indexFiles are locked!
            this.closeWriter();
        }
        return true;
    }
在将文档存储到我的索引中之后,是时候删除它了,我尝试这样做:

 public boolean deleteNote(Note note) throws IOException
    {
        if(note == null || note .getUri() == null || note.getUri().isEmpty() )   
                return false;

            LOGGER.info("Deleting Notes with URI '" + note.getPatientUri());

            TermQuery deleteTerm = new TermQuery(new Term(URINOTE, note.getUri()));

            try
            {
                this.initalizeWriter(OpenMode.CREATE_OR_APPEND);
                this.writer.deleteDocuments(deleteTerm);      
                this.writer.commit();
                LOGGER.info("Deleting for '" + note.getUri() + "done");
            }
            catch(Exception e)
            {
                this.writer.rollback();
                LOGGER.info("Rollback for '" + note.getPatientUri() + "done \n");
                LOGGER.info("Check Consitenzy");
            }
            finally
            {
               this.closeWriter();
            }
            return true;
    }

String queryString = URIPATIENT + ":\"" + request.getPatientUri() + "\"";

        Query query = new QueryParser(Version.LUCENE_43, NOTETEXT, analyzer).parse(queryString);
我的问题是deleteTerm不返回任何结果。我试着用我的术语搜索


知道我错过了什么吗

对于文档的id,您应该使用“未标记”的内容。 您应该使用“StringField”,文档中说:

StringField:索引但未标记的字段:整个字符串 值作为单个标记进行索引

然后,delete应该由单个术语调用(您不需要查询)

这应该行得通

doc.add(new StringField(URINOTE, note.getUri(), Field.Store.YES));
....
this.writer.deleteDocuments(new Term(URINOTE, note.getUri()));