2011-01-26 15 views
0

私はluceneの使用を開始しようとしています。コードは、私は、インデックス文書に使用していることである:Luceneの異常な動作

public void index(String type, String words) { 
     IndexWriter indexWriter = null; 
     try { 
      if (dir == null) 
       dir = createAndPropagate(); 
      indexWriter = new IndexWriter(dir, new StandardAnalyzer(), true, 
        new KeepOnlyLastCommitDeletionPolicy(), 
        IndexWriter.MaxFieldLength.UNLIMITED); 

      Field wordsField = new Field(FIELD_WORDS, words, Field.Store.YES, 
        Field.Index.ANALYZED); 
      Field typeField = new Field(FIELD_TYPE, type, Field.Store.YES, 
        Field.Index.ANALYZED); 

      Document doc = new Document(); 
      doc.add(wordsField); 
      doc.add(typeField); 

      indexWriter.addDocument(doc); 
      indexWriter.commit(); 
     } catch (IOException e) { 
      logger.error("Problems while adding entry to index.", e); 
      } finally { 
      try { 
       if (indexWriter != null) 
        indexWriter.close(); 
      } catch (IOException e) { 
       logger.error("Unable to close index writer.", e); 
      } 
     } 

    } 

検索は次のようになります。

public List<TagSearchEntity> searchFor(final String type, String words, 
      int amount) { 
     List<TagSearchEntity> result = new ArrayList<TagSearchEntity>(); 

     try { 
      if (dir == null) 
       dir = createAndPropagate(); 

      for (final Document doc : searchFor(dir, type, words, amount)) { 
       @SuppressWarnings("serial") 
       TagSearchEntity searchResult = new TagSearchEntity() {{ 
        setType(type); 
        setWords(doc.getField(FIELD_WORDS).stringValue()); 
       }}; 
       result.add(searchResult); 
      } 
     } catch (IOException e) { 
      logger.error("Problems while searching", e); 
     } 

     return result; 
    } 

private List<Document> searchFor(Directory indexDirectory, String type, 
      String words, int amount) throws IOException { 
     Searcher indexSearcher = new IndexSearcher(indexDirectory); 

     final Query tagQuery = new TermQuery(new Term(FIELD_WORDS, words)); 
     final Query typeQuery = new TermQuery(new Term(FIELD_TYPE, type)); 

     @SuppressWarnings("serial") 
     BooleanQuery query = new BooleanQuery() {{ 
      add(tagQuery, BooleanClause.Occur.SHOULD); 
      add(typeQuery, BooleanClause.Occur.MUST); 
     }}; 

     List<Document> result = new ArrayList<Document>(); 

     for (ScoreDoc scoreDoc : indexSearcher.search(query, amount).scoreDocs) { 
      result.add(indexSearcher.doc(scoreDoc.doc)); 
     } 

     indexSearcher.close(); 

     return result; 
    } 

私は2つのユースケースを持っています。最初のドキュメントはあるタイプのドキュメントを追加してからそれを検索し、別のタイプのドキュメントを追加してから検索します。もう1つはすべてのドキュメントを追加してから検索します。

@Test 
    public void testSearch() { 
     search.index("type1", "test type1 for test purposes test test"); 
     List<TagSearchEntity> result = search.searchFor("type1", "test", 10); 
     assertNotNull("Retrieved list should not be null.", result); 
     assertTrue("Retrieved list should not be empty.", !result.isEmpty()); 

     search.index("type2", "test type2 for test purposes test test"); 
     result.clear(); 
     result = search.searchFor("type2", "test", 10); 
     assertTrue("Retrieved list should not be empty.", !result.isEmpty()); 

     search.index("type3", "test type3 for test purposes test test"); 
     result.clear(); 
     result = search.searchFor("type3", "test", 10); 
     assertTrue("Retrieved list should not be empty.", !result.isEmpty()); 
    } 

をしかし、他の一つは、最後のドキュメントのインデックスを作成しているようだ:最初の1が正常に動作します

@Test 
    public void testBuggy() { 
     search.index("type1", "test type1 for test purposes test test"); 
     search.index("type2", "test type2 for test purposes test test"); 
     search.index("type3", "test type3 for test purposes test test"); 

     List<TagSearchEntity> result = search.searchFor("type3", "test", 10); 
     assertNotNull("Retrieved list should not be null.", result); 
     assertTrue("Retrieved list should not be empty.", !result.isEmpty()); 

     result.clear(); 
     result = search.searchFor("type2", "test", 10); 
     assertTrue("Retrieved list should not be empty.", !result.isEmpty()); 

     result.clear(); 
     result = search.searchFor("type1", "test", 10); 
     assertTrue("Retrieved list should not be empty.", !result.isEmpty()); 
    } 

をそれが正常にtype3を見つけたが、他のすべてを見つけることができません。もし私がこれらの呼び出しをshullfleしても、最後に索引付けされた文書だけを見つけることに成功します。 私が使用しているLuceneのバージョンは、次のとおりです。

<dependency> 
     <groupId>org.apache.lucene</groupId> 
     <artifactId>lucene-core</artifactId> 
     <version>2.4.1</version> 
    </dependency> 

    <dependency> 
     <groupId>lucene</groupId> 
     <artifactId>lucene</artifactId> 
     <version>1.4.3</version> 
    </dependency> 

私が間違って何をしているのですか?どのようにすべてのドキュメントのインデックスを作成するには?

答えて

2

インデックス操作ごとに新しいインデックスが作成されています。 3番目の引数はcreateフラグで、trueに設定されています。 documentation of IndexWriterによると、このフラグが設定されている場合、新しいインデックスを作成するか、既存のインデックスを上書きします。既存のインデックスに追加する場合はfalseに設定します。

+0

ありがとう、これは私の問題を完全に解決しました。 – folone

関連する問題