2015-12-16 44 views
1

如何將非索引字節數組存儲到lucene文檔中?在lucene中存儲非索引二進制數據

我嘗試了這些:

 doc.add(new Field("bin1", new InputStreamReader(new ByteArrayInputStream(new byte [100000])))); 
    doc.add(new BinaryDocValuesField("bin2", new BytesRef(new byte [100000]))); 

並沒有什麼工作(沒有存儲領域,無法查詢時檢索)

測試代碼:

String index="dms1"; 

    Directory indexDirectory = FSDirectory.open(Paths.get(index)); 
    StandardAnalyzer analyzer = new StandardAnalyzer(); 
    IndexWriterConfig iwc = new IndexWriterConfig(analyzer); 
    iwc.setOpenMode(IndexWriterConfig.OpenMode 
      .CREATE 
    ); 
    //create the indexer 
    IndexWriter iw = new IndexWriter(indexDirectory, iwc); 

    { 
    Document doc = new Document(); 

    doc.add(new TextField("id", "1", Field.Store.YES)); 
    doc.add(new Field("bin1", new InputStreamReader(new ByteArrayInputStream(new byte [100000])))); 
    doc.add(new BinaryDocValuesField("bin2", new BytesRef(new byte [100000]))); 

    iw.addDocument(doc); 
    iw.commit(); 
    } 

    DirectoryReader ir = DirectoryReader.open(indexDirectory); 

    IndexSearcher is = new IndexSearcher(ir); 


    QueryParser qp = new QueryParser(
      "", 
      analyzer); 
    Query q = qp.parse(
      //"content1:hp" 
      "*:*" 
); 
    TopDocs hits = is.search(q, 10); 

    for (ScoreDoc scoreDoc : hits.scoreDocs) { 
    Document doc = is.doc(scoreDoc.doc); 

    System.out.println(doc); 

    System.out.println("doc.getBinaryValue(bin1):" + doc.getBinaryValue("bin1"));; 
    System.out.println("doc.getBinaryValues(bin1):" + doc.getBinaryValues("bin1"));; 
    System.out.println("doc.getBinaryValues(bin1).length:" + doc.getBinaryValues("bin1").length);; 
    System.out.println("doc.get(bin1):" + doc.get("bin1"));; 

    System.out.println("doc.getBinaryValue(bin2):" + doc.getBinaryValue("bin2"));; 
    System.out.println("doc.getBinaryValues(bin2):" + doc.getBinaryValues("bin2"));; 
    System.out.println("doc.getBinaryValues(bin2).length:" + doc.getBinaryValues("bin2").length);; 
    System.out.println("doc.get(bin2):" + doc.get("bin2"));; 
    } 

輸出:

Document<stored,indexed,tokenized<id:1>> 
    doc.getBinaryValue(bin1):null 
    doc.getBinaryValues(bin1):[Lorg.apache.lucene.util.BytesRef;@899e53 
    doc.getBinaryValues(bin1).length:0 
    doc.get(bin1):null 
    doc.getBinaryValue(bin2):null 
    doc.getBinaryValues(bin2):[Lorg.apache.lucene.util.BytesRef;@f98160 
    doc.getBinaryValues(bin2).length:0 
    doc.get(bin2):null 

誰能闡明如何存儲字節以及如何再次檢索值?

我知道使用base64或其他編碼將字節轉換爲文本或將其存儲爲文件鏈接的其他解決方案,但是我需要知道的是一種更有效的方法,因爲lucene API具有「二進制」方法我認爲這應該是正確的做法。

lucene的版本:5.3.1

回答

2

使用StoredField。你可以通過無論是在BytesRef,或字節數組本身進入該領域:

byte[] myByteArray = new byte[100000]; 
document.add(new StoredField("bin1", myByteArray)); 

至於獲取的價值,你是對正確的軌道存在了。喜歡的東西:

Document resultDoc = searcher.doc(docno); 
BytesRef bin1ref = resultDoc.getBinaryValue("bin1"); 
bytes[] bin1bytes = bin1ref.bytes; 

順便說一句,與這兩個領域的問題,你試過:

  • BIN1:當你傳遞一個讀者進入Field構造函數,它決定將其視爲TextField將被索引並且不存儲,實際上與您正在查找的內容相反。該構造是無論如何不贊成,贊成只用TextField

    的。如果你贊成就選擇剛傳入byte[]代替Reader的,它實際上會工作,因爲這會充當StoredField(如上圖所示),儘管該構造函數也被棄用)。

  • bin2:DocValuesFields的工作方式不同。你可以read up a bit on that here,如果你很好奇。

+0

它工作!非常感謝 ! – Dny

相關問題