2014-05-23 2 views
0

Lucene 3.6을 사용하고 있습니다. 업데이트가 작동하지 않는 이유를 알고 싶습니다. 잘못된 것이 있습니까?lucene updateDocument not work

public class TokenTest 
{ 
    private static String IndexPath = "D:\\update\\index"; 

    private static Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_33); 

    public static void main(String[] args) throws Exception 
    { 

     try 
     { 

      update(); 

      display("content", "content"); 

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

    @SuppressWarnings("deprecation") 
    public static void display(String keyField, String words) throws Exception 
    { 
     IndexSearcher searcher = new IndexSearcher(FSDirectory.open(new File(IndexPath))); 
     Term term = new Term(keyField, words); 
     Query query = new TermQuery(term); 
     TopDocs results = searcher.search(query, 100); 

     ScoreDoc[] hits = results.scoreDocs; 

     for (ScoreDoc hit : hits) 
     { 

      Document doc = searcher.doc(hit.doc); 
      System.out.println("doc_id = " + hit.doc); 
      System.out.println("内容: " + doc.get("content")); 
      System.out.println("路径:" + doc.get("path")); 

     } 
    } 

    public static String update() throws Exception 
    { 

     IndexWriterConfig writeConfig = new IndexWriterConfig(Version.LUCENE_33, analyzer); 

     IndexWriter writer = new IndexWriter(FSDirectory.open(new File(IndexPath)), writeConfig); 

     Document document = new Document(); 

     Field field_name2 = new Field("path", "update_path", Field.Store.YES, Field.Index.ANALYZED); 
     Field field_content2 = new Field("content", "content update", Field.Store.YES, Field.Index.ANALYZED); 

     document.add(field_name2); 
     document.add(field_content2); 

     Term term = new Term("path", "qqqqq"); 

     writer.updateDocument(term, document); 
     writer.optimize(); 
     writer.close(); 
     return "update_path"; 
    } 

} 

답변

0

난 당신이 문서와 같은 해당 필드 "경로"= "QQQQ"을 업데이트 할 가정합니다. 정확히 이것을 거꾸로 가지고 있습니다 (please read the documentation).

updateDocument

은 다음과 같은 두 가지 단계를 수행하여 인덱스 문서 path:qqqq
  • 가 추가 포함되어 있지 않기 때문에

    1. 찾기이 경우 term
      • 가 포함 된 문서를 삭제, 아무도는 발견되지 새로운 document을 색인에 추가하십시오.

    당신은 문서를 검색하려고 시도하는 것과 반대되는 것처럼 보인 다음 용어를 추가합니다. 그런 식으로 작동하지 않습니다. 당신이 찾고있는 것은 다음과 같습니다 :

    Term term = new Term("content", "update"); 
    
    document.removeField("path"); 
    document.add("path", "qqqq"); 
    
    writer.updateDocument(term, document);