You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@lucene.apache.org by Larry White <lw...@tracelink.com> on 2015/09/23 16:17:03 UTC

How can I use a byte array as a Lucene index field?

I'm using Lucene 4.10.3. (I plan to upgrade soon but need to fix an issue
on this version today).

I switched a Lucene index from using string document ids to byte arrays.
The problem I'm having is that the system no longer finds documents by
their id. I *suspect* this is because the lucene code is not doing an
Array.equals(), but rather a standard equals(). This is the code to add the
document:

    Document doc = new Document();
    byte[] key = indexData.getKey().toByteArray();
    System.out.println(Arrays.toString(key));
    doc.add(new StoredField(DOCUMENT_PRIMARY_KEY, new BytesRef(key)));
    writer.addDocument(doc);

And this is the code to delete the document. The delete fails because the
document is not found (although it does exist in the index).

    void prepareDelete(byte[] documentId) throws IOException {
        System.out.println(Arrays.toString(documentId));
        Term term =
                new Term(DOCUMENT_PRIMARY_KEY, new BytesRef(documentId));
        writer.deleteDocuments(term);
    }

By comparing the output of the print statements, I've determined that the
keys are the same (in the sense that they contain the same bytes), but they
do not share identity.

Thanks very much for any and all assistance.

Re: How can I use a byte array as a Lucene index field?

Posted by Larry White <lw...@tracelink.com>.
i upgraded to 5.3 and fixed as you suggested. thank you.

On Wed, Sep 23, 2015 at 11:31 AM, Michael McCandless <
lucene@mikemccandless.com> wrote:

> You are indexing your field as a StoredField which means it's not
> actually indexed (just stored), so no query (nor IW.deleteDocument)
> will ever be able to find it.
>
> Try StringField instead ... in recent versions you can pass a BytesRef
> value to that.
>
> Mike McCandless
>
> http://blog.mikemccandless.com
>
>
> On Wed, Sep 23, 2015 at 10:17 AM, Larry White <lw...@tracelink.com>
> wrote:
> > I'm using Lucene 4.10.3. (I plan to upgrade soon but need to fix an issue
> > on this version today).
> >
> > I switched a Lucene index from using string document ids to byte arrays.
> > The problem I'm having is that the system no longer finds documents by
> > their id. I *suspect* this is because the lucene code is not doing an
> > Array.equals(), but rather a standard equals(). This is the code to add
> the
> > document:
> >
> >     Document doc = new Document();
> >     byte[] key = indexData.getKey().toByteArray();
> >     System.out.println(Arrays.toString(key));
> >     doc.add(new StoredField(DOCUMENT_PRIMARY_KEY, new BytesRef(key)));
> >     writer.addDocument(doc);
> >
> > And this is the code to delete the document. The delete fails because the
> > document is not found (although it does exist in the index).
> >
> >     void prepareDelete(byte[] documentId) throws IOException {
> >         System.out.println(Arrays.toString(documentId));
> >         Term term =
> >                 new Term(DOCUMENT_PRIMARY_KEY, new BytesRef(documentId));
> >         writer.deleteDocuments(term);
> >     }
> >
> > By comparing the output of the print statements, I've determined that the
> > keys are the same (in the sense that they contain the same bytes), but
> they
> > do not share identity.
> >
> > Thanks very much for any and all assistance.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org
>
>


-- 
*Larry White |  TraceLink Inc. | Principal Software Architect*
400 Riverpark Dr. | North Reading, MA | 01864
e: lwhite@tracelink.com
www.tracelink.com


*Protect patients, enable health, grow profits, ensure compliance*

Re: How can I use a byte array as a Lucene index field?

Posted by Michael McCandless <lu...@mikemccandless.com>.
You are indexing your field as a StoredField which means it's not
actually indexed (just stored), so no query (nor IW.deleteDocument)
will ever be able to find it.

Try StringField instead ... in recent versions you can pass a BytesRef
value to that.

Mike McCandless

http://blog.mikemccandless.com


On Wed, Sep 23, 2015 at 10:17 AM, Larry White <lw...@tracelink.com> wrote:
> I'm using Lucene 4.10.3. (I plan to upgrade soon but need to fix an issue
> on this version today).
>
> I switched a Lucene index from using string document ids to byte arrays.
> The problem I'm having is that the system no longer finds documents by
> their id. I *suspect* this is because the lucene code is not doing an
> Array.equals(), but rather a standard equals(). This is the code to add the
> document:
>
>     Document doc = new Document();
>     byte[] key = indexData.getKey().toByteArray();
>     System.out.println(Arrays.toString(key));
>     doc.add(new StoredField(DOCUMENT_PRIMARY_KEY, new BytesRef(key)));
>     writer.addDocument(doc);
>
> And this is the code to delete the document. The delete fails because the
> document is not found (although it does exist in the index).
>
>     void prepareDelete(byte[] documentId) throws IOException {
>         System.out.println(Arrays.toString(documentId));
>         Term term =
>                 new Term(DOCUMENT_PRIMARY_KEY, new BytesRef(documentId));
>         writer.deleteDocuments(term);
>     }
>
> By comparing the output of the print statements, I've determined that the
> keys are the same (in the sense that they contain the same bytes), but they
> do not share identity.
>
> Thanks very much for any and all assistance.

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