This is the XML search class ... Steve Carton ----------------------- www.retrievalsystems.com 703/749-0012 Retrieval Systems Corporation 2071 Chain Bridge Road, Suite 510 Vienna, VA 22182
package rsc;
import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.document.Field; import java.io.*; import org.apache.lucene.search.*; import org.w3c.dom.*; import java.util.Enumeration; import org.apache.xerces.dom.DocumentImpl; /** * <p>Title: XIndexSearcher</p> * <p>Description: Extends the IndexSearcher class to provide a methods that performs a search and returns the results as a DOM document</p> * <p>Company: Retrieval Systems Corporation</p> * @author Steve Carton * @version 2.0 */ public class XIndexSearcher extends IndexSearcher { /** * XIndexSearcher -- constructor with path to Lucene indexes * * @param path String * @throws IOException */ public XIndexSearcher(String path) throws IOException { super(path); } /** * xsearch -- search method that returns an XML DOM result. * * <p>Passes the query to the IndexSearcher.search method. Then turns the resulting hits to XML. * * @param query Query * @throws IOException * @return Document */ public org.w3c.dom.Document xsearch(Query query) throws IOException { return hits2XML(super.search(query),query); } /** * hits2XML -- converts the fields in the Hits object to an XML DOM document and returns that. * * @param hits Hits * @param query Query * @throws IOException * @return Document */ public org.w3c.dom.Document hits2XML(Hits hits, Query query) throws IOException { org.w3c.dom.Document XML = newDocument("HITS"); Element results = XML.getDocumentElement(); if (hits != null) { results.setAttribute("LENGTH", "" + hits.length()); results.setAttribute("QUERY",query.toString()); int i=0; while (i<hits.length()) { Element hit = makeXMLElement(results,"HIT"); hit.setAttribute("SCORE",""+hits.score(i)); org.apache.lucene.document.Document doc = hits.doc(i); Enumeration it = doc.fields(); while (it.hasMoreElements()) { Field field = (Field)it.nextElement(); Element efield = makeXMLElement(hit,"FIELD"); efield.setAttribute("NAME",field.name()); // efield.setAttribute("BOOST",""+field.getBoost()); efield.setAttribute("ISINDEXED",""+field.isIndexed()); efield.setAttribute("ISSTORED",""+field.isStored()); efield.setAttribute("ISTOKENIZED",""+field.isTokenized()); if (field.isStored()) { efield.appendChild(efield.getOwnerDocument().createTextNode(field.stringValue())); } } i++; } } else results.setAttribute("LENGTH", "0"); return XML; } /** * newDocument -- creates a new DOM document with a given document element. Uses the Apache Xerces DocumentImpl() * class to create the DOM. * * @param root String * @return Document */ private Document newDocument(String root) { Document XML = new DocumentImpl(); XML.appendChild(XML.createElement(root)); return XML; } /** * makeXMLElement -- creates a new element in a DOM under a specified node. * * @param parent Node * @param nm String * @return Element * @author */ private Element makeXMLElement(Node parent, String nm) { Element oNode = parent.getOwnerDocument().createElement(nm); parent.appendChild(oNode); return oNode; } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]