如何將非索引字節數組存儲到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
它工作!非常感謝 ! – Dny