lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Uwe Schindler" <...@thetaphi.de>
Subject RE: Storing a serialized object ?
Date Sat, 04 Jul 2009 08:13:43 GMT
You can add a serialized object easily as a stored field to a document, just
serialize the object to an byte[] array and store this in the index, e.g.:

ByteArrayOutputStream serData=new ByteArrayOutputStream();
ObjectOutputStream out=new ObjectOutputStream(serData);
try {
	out.writeObject(dataStringContents);
} finally  {
	out.close();
	serData.close();
}
doc.add(new Field("fieldname", serData.toByteArray(),
Field.Store.COMPRESS));

When have done a Lucene search, you can retrieve the object like this from
an Document instance:

byte[] serData=ldoc.getBinaryValue("fieldname");
if (serData!=null) {
	ObjectInputStream in=new ObjectInputStream(new
ByteArrayInputStream(serData));
	try {
		bla = in.readObject();
	} finally  {
		in.close();
	}
}

But this is only stored content, you cannot search inside the object,
because it is a) stored and b) Lucene does not know what terms are in it.

-----
Uwe Schindler
H.-H.-Meier-Allee 63, D-28213 Bremen
http://www.thetaphi.de
eMail: uwe@thetaphi.de

> -----Original Message-----
> From: MilleBii [mailto:millebii@gmail.com]
> Sent: Friday, July 03, 2009 9:32 PM
> To: java-user@lucene.apache.org
> Subject: Storing a serialized object ?
> 
> I want to store in the index a data structure and load it back at search
> time.
> 
> Is it safe to serialize the java object store it and load it back later ?
> Presumably I need to store it binary, right ?
> 
> Otherwise I need to create my own store & load methods, waste of time.
> 
> --
> -MilleBii-


---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Mime
View raw message