在lucene中存储非索引二进制数据

在lucene中存储非索引二进制数据,lucene,Lucene,如何将非索引字节数组存储到lucene文档中 我试过这些: doc.add(新字段(“bin1”,新的InputStreamReader(新的ByteArrayInputStream(新的字节[100000])); doc.add(新的二进制docvalues字段(“bin2”,新的BytesRef(新的字节[100000])); 并且没有工作(字段未存储,查询时无法检索) 测试代码: String index=“dms1”; 目录indexDirectory=FSDirectory.open

如何将非索引字节数组存储到lucene文档中

我试过这些:

doc.add(新字段(“bin1”,新的InputStreamReader(新的ByteArrayInputStream(新的字节[100000]));
doc.add(新的二进制docvalues字段(“bin2”,新的BytesRef(新的字节[100000]));
并且没有工作(字段未存储,查询时无法检索)

测试代码:

String index=“dms1”;
目录indexDirectory=FSDirectory.open(path.get(index));
StandardAnalyzer=新的StandardAnalyzer();
IndexWriterConfig iwc=新的IndexWriterConfig(分析器);
iwc.setOpenMode(IndexWriterConfig.OpenMode
.创造
);
//创建索引器
IndexWriter iw=新的IndexWriter(indexDirectory,iwc);
{
单据单据=新单据();
添加文档(新的文本字段(“id”,“1”,Field.Store.YES));
添加(新字段(“bin1”,新的InputStreamReader(新的ByteArrayInputStream(新的字节[100000])));
doc.add(新的二进制docvalues字段(“bin2”,新的BytesRef(新的字节[100000]));
iw.addDocument(doc);
提交();
}
DirectoryReader ir=DirectoryReader.open(indexDirectory);
IndexSearcher is=新的IndexSearcher(ir);
QueryParser qp=新的QueryParser(
"",
分析仪);
查询q=qp.parse(
//“内容1:hp”
"*:*"
);
TopDocs hits=is.search(q,10);
for(ScoreDoc ScoreDoc:hits.scoreDocs){
单据单据=is.doc(scoreDoc.doc);
系统输出打印项次(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”);;
}
输出:

文档
doc.getBinaryValue(bin1):空
doc.getBinaryValues(bin1):[Lorg.apache.lucene.util.BytesRef;@899e53
doc.getBinaryValues(bin1)。长度:0
doc.get(bin1):空
doc.getBinaryValue(bin2):空
doc.getBinaryValues(bin2):[Lorg.apache.lucene.util.BytesRef;@f98160
doc.getBinaryValues(bin2).长度:0
doc.get(bin2):空
有人能解释一下如何存储字节以及如何再次检索值吗

我知道其他解决方案使用base64或其他编码将字节转换为文本或将其存储为文件链接,但我需要知道的是一种更有效的方法,因为lucene API有“二进制”方法,所以我认为这应该是正确的方法

lucene版本:5.3.1

使用。您可以将
字节引用
或字节数组本身传入字段:

byte[]myByteArray=新字节[100000];
文件。添加(新存储字段(“bin1”,myByteArray));
就检索值而言,您已经走上了正确的轨道。例如:

documentresultdoc=searcher.doc(docno);
BytesRef bin1ref=resultDoc.getBinaryValue(“bin1”);
字节[]bin1bytes=bin1ref.bytes;

顺便说一下,您尝试的两个字段的问题是:

  • bin1:当您将读取器传递到
    字段
    构造函数时,它决定将其视为一个
    TextField
    ,该字段将被索引而不是存储,这实际上与您要查找的内容相反。无论如何,该构造函数都不受欢迎,只使用
    TextField

    如果您选择只传入
    字节[]
    而不是
    读取器
    ,它实际上会工作,因为它会充当
    存储字段
    (如上所示),尽管该构造函数也不推荐使用)

  • bin2:DocValuesFields的工作方式不同。如果你好奇的话,你可以