Mailing List Archive

org.apache.lucene.document.Document.getFields(String name) method?
Hi,

The method org.apache.lucene.document.Document.getField(String name) is
documented as this :

"Returns a field with the given name if any exist in this document, or
null. If multiple fields may exist with this name, this method returns
the last added such added."

I'm wondering why the limit for multiple fields. In some cases, I'm
interested in retrieving all the values for a field. Looking at the
code, I see that it returns a field as soon as it finds one with the
right name :

-----
for (DocumentFieldList list = fieldList; list != null; list =
list.next)
if (list.field.name().equals(name))
return list.field;
-----

Would it be possible to add something like a "public Field[]
getFields(String name)" method to the Document class? It is pretty
simple to write, I can send the code if needed.

Thank's,

Martin Sévigny


--
To unsubscribe, e-mail: <mailto:lucene-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:lucene-dev-help@jakarta.apache.org>
Re: org.apache.lucene.document.Document.getFields(String name) method? [ In reply to ]
I'm not sure...
Can you really add multiple fields with the same name to the index?

Check this method comment in Document.java:
/** Adds a field to a document. Several fields may be added with
* the same name. In this case, if the fields are indexed, their
* text is treated as though appended for the purposes of search.
*/
public final void add(Field field)

It sounds like indexed fields are just concatenated (into a single
field with the given name). I'm not sure what happens with non-indexed
fields.

Does Lucene store multiple values for the same field name in such a way
that you can retrieve them separately?

Thanks,
Otis


--- Martin_Sévigny <sevigny@ajlsm.com> wrote:
> Hi,
>
> The method org.apache.lucene.document.Document.getField(String name)
> is
> documented as this :
>
> "Returns a field with the given name if any exist in this document,
> or
> null. If multiple fields may exist with this name, this method
> returns
> the last added such added."
>
> I'm wondering why the limit for multiple fields. In some cases, I'm
> interested in retrieving all the values for a field. Looking at the
> code, I see that it returns a field as soon as it finds one with the
> right name :
>
> -----
> for (DocumentFieldList list = fieldList; list != null; list =
> list.next)
> if (list.field.name().equals(name))
> return list.field;
> -----
>
> Would it be possible to add something like a "public Field[]
> getFields(String name)" method to the Document class? It is pretty
> simple to write, I can send the code if needed.
>
> Thank's,
>
> Martin Sévigny


__________________________________________________
Do You Yahoo!?
Yahoo! Health - your guide to health and wellness
http://health.yahoo.com

--
To unsubscribe, e-mail: <mailto:lucene-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:lucene-dev-help@jakarta.apache.org>
RE : org.apache.lucene.document.Document.getFields(String name) method? [ In reply to ]
Otis and others,

> Can you really add multiple fields with the same name to the index?

Sure.

> It sounds like indexed fields are just concatenated (into a single
> field with the given name). I'm not sure what happens with
> non-indexed
> fields.

True for tokenized fields, but not for untokenized fields. That's one of
the great features of Lucene.

> Does Lucene store multiple values for the same field name in
> such a way
> that you can retrieve them separately?

Exactly. If you store "Reagan, Ronald" and then "Clinton, Bill" in an
"author" field, untokenized, and if you list the author field content
with an indexreader you will get both values independantly. It just
doesn't work with a Document object, where only the last value is
returned, unfortunately. Just because the lookup stops at the first
occurrence (which seems the last one indexed).

Martin


--
To unsubscribe, e-mail: <mailto:lucene-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:lucene-dev-help@jakarta.apache.org>