繁体   English   中英

Lucene性能:将字段数据从一个索引传输到另一索引

[英]Lucene performance: Transferring fields data from one index to another

简而言之,我需要交换从一个索引到结果索引的多个字段和值的映射。

以下是方案。

索引1结构[字段=>值] [存储]

Doc 1    
keys => keyword1;    
Ids => id1, id1, id2, id3, id7, id11, etc.. 

Doc 2    
keys => keyword2;    
Ids => id3, id11, etc..

索引2结构[字段=>值] [存储]

Doc 1    
ids => id1    
keys => keyword1, keyword1

Doc 3    
ids => id3    
keys => keyword1, keyword2, etc..

请注意,键<-> ids 映射在结果索引中是相反的。

在时间复杂度方面,您认为最有效的方法是什么? ..

我能想到的唯一方法是..

1) index1Reader.terms();    
2) Process only terms belonging to "Ids" field    
3) For each term, get TermDocs    
4) For each doc, load it, get "keys" field info    
5) Create a new Lucene Doc, add 'Id', multi Keys, write it to index2.     
6) Go to step 2.

由于字段已存储,因此我敢肯定有多种方法可以做到这一点。

请以任何演奏技巧指导我。 考虑到Index1大小约为6GB, 即使是最细微的改进也会对我的情况产生巨大影响。

总数 唯一关键字的数量:1800万; 总数 ID的总数:90万

有趣的更新

优化1

  • 在添加新文档时,与其创建多个重复的“字段”对象,不如创建带有“”分隔符的单个StringBuffer,然后将整个字段添加为单个字段,似乎可以提高25%。

更新2:代码

    public void go() throws IOException, ParseException {
    String id = null;
    int counter = 0;
    while ((id = getNextId()) != null) { // this method is not taking time..
        System.out.println("Node id: " + id);
        updateIndex2DataForId(id);
        if(++counter > 10){
            break;
        }
    }
    index2Writer.close();
}

private void updateIndex2DataForId(String id) throws ParseException, IOException {
    // Get all terms containing the node id
    TermDocs termDocs = index1Reader.termDocs(new Term("id", id));
    // Iterate
    Document doc = new Document();
    doc.add(new Field("id", id, Store.YES, Index.NOT_ANALYZED));
    int docId = -1;        
    while (termDocs.next()) {
        docId = termDocs.doc();
        doc.add(getKeyDataAsField(docId, Store.YES, Index.NOT_ANALYZED));            
    }
    index2Writer.addDocument(doc);
}

private Field getKeyDataAsField(int docId, Store storeOption, Index indexOption) throws CorruptIndexException,
        IOException {
    Document doc = index1Reader.document(docId, fieldSelector); // fieldSel has "key"
    Field f = new Field("key", doc.get("key"), storeOption, indexOption);
    return f;
}

使用FieldCache就像是一种魅力……但是,我们需要分配越来越多的RAM以容纳堆中的所有字段。

我已使用以下代码段更新了上述updateIndex2DataForId()。

private void updateIndex2DataForId(String id) throws ParseException, IOException {
    // Get all terms containing the node id
    TermDocs termDocs = index1Reader.termDocs(new Term("id", id));
    // Iterate
    Document doc = new Document();
    doc.add(new Field("id", id, Store.YES, Index.NOT_ANALYZED));
    int docId = -1;
    StringBuffer buffer = new StringBuffer();
    while (termDocs.next()) {
        docId = termDocs.doc();
        buffer .append(keys[docId] + " "); // keys[] is pre-populated using FieldCache                 
    }
    doc.add(new Field("id", buffer.trim().toString(), Store.YES, Index.ANALYZED));   
    index2Writer.addDocument(doc);
}

String[] keys = FieldCache.DEFAULT.getStrings(index1Reader, "keywords");

它使一切变得更快,我无法告诉您确切的指标,但我必须说非常重要。

现在程序将在合理的时间内完成。 无论如何,高度赞赏进一步的指导。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM