1

私のコード例では、lucene インデックスに 3 つのドキュメントを作成します。そのうちの 2 つはフィールド LASTNAME を保存していませんが、termvector を保存しており、1 つは保存していません。LUKE を使用すると、このフィールド (LASTNAME) のすべての用語を反復処理できます。私のコード例では、TermFreqVectors を反復処理します。これは、保存された TermVectors を含むドキュメントに対して正常に機能します。

この保存されていない条件をすべて取得するにはどうすればよいですか? LUKEはどうやってそれをやっているのですか?

私の最初の問題は、インデックスを最初から再作成せずに、100 フィールド近くの大きなインデックス (60GB) を別のフィールドで拡張したいということです。インデックスからすべてのデータを読み取り、この新しいフィールドを保存されているすべてのドキュメントに追加するだけで非常に高速です。

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.MockAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.RandomIndexWriter;
import org.apache.lucene.index.TermFreqVector;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.NIOFSDirectory;
import org.apache.lucene.util.LuceneTestCase;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;


public class TestDocTerms extends LuceneTestCase {

public void testDocTerms() throws IOException, ParseException {
    Analyzer analyzer = new MockAnalyzer(random);

    String fieldF = "FIRSTNAME";
    String fieldL = "LASTNAME";

    // To store an index on disk, use this instead:
    Directory directory = NIOFSDirectory.open(new File("/tmp/_index_tester/"));
    RandomIndexWriter iwriter = new RandomIndexWriter(random, directory, analyzer);
    iwriter.w.setInfoStream(VERBOSE ? System.out : null);
    Document doc = new Document();
    doc.add(newField(fieldF, "Alex", Field.Store.YES, Field.Index.ANALYZED));
    doc.add(newField(fieldL, "Miller", Field.Store.NO,Field.Index.ANALYZED,Field.TermVector.YES));
    iwriter.addDocument(doc);
    doc = new Document();
    doc.add(newField(fieldF, "Chris", Field.Store.YES, Field.Index.ANALYZED));
    doc.add(newField(fieldL, "Smith", Field.Store.NO, Field.Index.ANALYZED));
    iwriter.addDocument(doc);
    doc = new Document();
    doc.add(newField(fieldF, "Alex", Field.Store.YES, Field.Index.ANALYZED));
    doc.add(newField(fieldL, "Beatle", Field.Store.NO, Field.Index.ANALYZED,Field.TermVector.YES));
    iwriter.addDocument(doc);
    iwriter.close();

    // Now search the index:
    IndexSearcher isearcher = new IndexSearcher(directory, true); // read-only=true
    QueryParser parser = new QueryParser(TEST_VERSION_CURRENT, fieldF, analyzer);
    Query query = parser.parse(fieldF + ":" + "Alex");
    TopDocs hits = isearcher.search(query, null, 2);
    assertEquals(2, hits.totalHits);
    // Iterate through the results:
    for (int i = 0; i < hits.scoreDocs.length; i++) {
        Document hitDoc = isearcher.doc(hits.scoreDocs[i].doc);
        assertEquals("Alex", hitDoc.get(fieldF));
        System.out.println("query for:" +query.toString()+ " with this results firstN:" + hitDoc.get(fieldF) + " and lastN:" + hitDoc.get(fieldL));
    }
    parser = new QueryParser(TEST_VERSION_CURRENT, fieldL, analyzer);
    query = parser.parse(fieldL + ":" + "Miller");
    hits = isearcher.search(query, null, 2);
    assertEquals(1, hits.totalHits);
    // Iterate through the results:
    for (int i = 0; i < hits.scoreDocs.length; i++) {
        Document hitDoc = isearcher.doc(hits.scoreDocs[i].doc);
        assertEquals("Alex", hitDoc.get(fieldF));
        System.out.println("query for:" + query.toString() + " with this results firstN:" +hitDoc.get(fieldF)+ " and lastN:" +hitDoc.get(fieldL));
    }
    isearcher.close();

    // examine terms
    IndexReader ireader = IndexReader.open(directory, true); // read-only=true
    int numDocs = ireader.numDocs();

    for (int i = 0; i < numDocs; i++) {
        doc = ireader.document(i);
        System.out.println("docNum:" + i + " with:" + doc.toString());
        TermFreqVector t = ireader.getTermFreqVector(i, fieldL);
        if (t != null){
            System.out.println("Field:" + fieldL + " contains terms:" + t.toString());
        }
        TermFreqVector[] termFreqVectors = ireader.getTermFreqVectors(i);
        if (termFreqVectors != null){
            for (TermFreqVector tfv : termFreqVectors){
                String[] terms = tfv.getTerms();
                String field = tfv.getField();
                System.out.println("Field:" +field+ " contains terms:" + Arrays.toString(terms));
            }
        }
    }
    ireader.close();
}


}
4

1 に答える 1