can't delete document with lucene IndexWriter.deleteDocuments(term)

后端 未结 1 1359
隐瞒了意图╮
隐瞒了意图╮ 2021-01-23 02:20

Have been struggling for this two days now, just can\'t delete the document with indexWriter.deleteDocuments(term)

Here I will put the code which will do a

相关标签:
1条回答
  • 2021-01-23 02:49

    Your problem is in the analyzer. SimpleAnalyzer defines tokens as maximal strings of letters (StandardAnalyzer, or even WhitespaceAnalyzer, are more typical choices), so the value you are indexing gets split into the tokens: "b", "a", "b", "d", "f". The delete method you've defined doesn't pass through the analyzer though, but rather just creates a raw term. You can see this in action if you try replacing your main with this:

    generateIndex("5836962b0293a47b09d345f1");
    query("5836962b0293a47b09d345f1");
    delete("b");
    query("5836962b0293a47b09d345f1");
    

    As a general rule, queries and terms and such do not analyze, QueryParser does.

    For (what looks like) an identifier field, you probably don't really want to analyze this field at all. In that case, add this to the FieldType:

    fieldType.setTokenized(false);
    

    You will then have to change your query (again, QueryParser analyzes), and use TermQuery instead.

    Query query = new TermQuery(new Term("_id", id));
    
    0 讨论(0)
提交回复
热议问题