The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

HydroMan (2) [Avatar] Offline
#1
2.4.7 Multi-valued Fields
Suppose your documents have an author field, but sometimes there’s more than one author for a document. One way to handle this would be to loop through all the authors, appending them into a single String, which you could then use to create a Lucene Field. Another, perhaps more elegant way is to just keep adding the same Field with different value, like this:

Document doc = new Document();
for (int i = 0; i < authors.length; i++)
{
doc.add(new Field("author", authors[i],
Field.Store.YES,
Field.Index.ANALYZED));
}

I think the code above doesn't reflect what the author is trying to say. Its creating additional documents instead of Multi-Valued fields. Can you verify and fix please?

Thanks!
otis (156) [Avatar] Offline
#2
Re: Secrtion : 2.4.7 Multi-valued Fields
I don't have the manuscript handy right now, but the code seems to match the description - note that there is only 1 Document instance outside of the for loop.

Otis
HydroMan (2) [Avatar] Offline
#3
Re: Secrtion : 2.4.7 Multi-valued Fields
I think you are right, I read it wrong. Info and code both match, scratch this.

Cheers!
silvyn (2) [Avatar] Offline
#4
Re: Secrtion : 2.4.7 Multi-valued Fields
doc.add(new Field("author", authors,

should read

doc.add(new Field("author", authors[i] // <-- Index into the authors

Otherwise, you're adding (or trying to add) the collection over and over again! Otherwise, I think it makes sense.
otis (156) [Avatar] Offline
#5
Re: Secrtion : 2.4.7 Multi-valued Fields
Ah, it should be authors[[ii]] in there. (I had to put extra brackets and 2 'i's, otherwise nothing would get shown because it happens to trigger italic markup)