lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ian Lea <ian....@gmail.com>
Subject Re: Need help regarding Lucene Document Iteration...
Date Tue, 05 Oct 2010 09:17:27 GMT
What exactly is the problem?  The standard idiom nowadays for
iterating through a List is what you're using in "for (Field field :
...)".  I haven't used an iterator for a long time.

But perhaps your iteration code is working and the problem is in your
search code.  The javadoc for search(query, n) finds the top n hits
for the query.  Maybe you need a larger number than 1.


--
Ian.


On Mon, Oct 4, 2010 at 10:44 PM, Altaf Vasi <altafvasi@gmail.com> wrote:
> Hi,
>
> i am doing the following in java.
>
> I am searching through a lucene index and getting a Collection of Documents.
>
>
> Below is the code that i have written.
>
>
> Collection<org.apache.lucene.document.Document> docCollection= new
> ArrayList<org.apache.lucene.document.Document>();
>
>        try {
>            Query query = new TermQuery(new Term(nameType, queryStr));
>            ScoreDoc[] hits = searcher.search(query, 1).scoreDocs;
>            org.apache.lucene.document.Document doc = new
> org.apache.lucene.document.Document();
>            for (int i = 0; i < hits.length; i++) {
>                 int docId = hits[i].doc;
>                 doc = searcher.doc(docId);
>                 docCollection.add(doc);
>                }
>        }  catch (IOException e) {
>            e.printStackTrace();
>        }
>        return docCollection;
>
>
> The above method snippet is returning a collection of Documents. The next
> step for me is to iterate through the collection of lucene documents that i
> got and convert the same into XML using javax.xml.stream.
>
> The issue is that i cannot figure out how to iterate through the Collection
> of documents....
>
> *Below is the code where i am facing the problem*
>
>
> public void write(XMLStreamWriter writer, Collection<Document>
> docCollection) throws XMLStreamException {
>
>        writer.writeStartDocument();
>        writer.writeStartElement("documents");
>        *Iterator<Document> document = docCollection.iterator();*
>
>        while (document.hasNext()) {
>        writer.writeStartElement("document");
>  *          for (Field field : (List<Field>) document.getFields()) {*
>                writer.writeStartElement("field");
>                writer.writeAttribute("name", field.name());
>                writer.writeAttribute("value", field.stringValue());
>                writer.writeEndElement();
>            }
>            writer.writeEndElement();
>        }
>
>        writer.writeEndElement();
>        writer.writeEndDocument();
>
>
>
> Request you to correct the above code snippet..... How do i iterate through
> the Collection of Lucene docs?
> Please help !!!!
>
> Thanks,
> Altaf
>

---------------------------------------------------------------------
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