vgritsenko 2002/09/05 20:35:54 Modified: . changes.xml src/documentation/xdocs/userdocs/generators search-generator.xml src/java/org/apache/cocoon/generation SearchGenerator.java Log: Add <field/> to the <hit/> to allow context in the search result Revision Changes Path 1.240 +7 -1 xml-cocoon2/changes.xml Index: changes.xml =================================================================== RCS file: /home/cvs/xml-cocoon2/changes.xml,v retrieving revision 1.239 retrieving revision 1.240 diff -u -r1.239 -r1.240 --- changes.xml 6 Sep 2002 00:35:16 -0000 1.239 +++ changes.xml 6 Sep 2002 03:35:53 -0000 1.240 @@ -39,6 +39,12 @@ </devs> <release version="@version@" date="@date@"> + <action dev="VG" type="update"> + SearchGenerator now includes <field name=""/> elements into <hit/> + element. These elements contain stored fields of a found Document, + making it possible to provide additional info about document in the + search result list (only when index created with some stored fields). + </action> <action dev="OP" type="add"> New user login and preferences sample to show the control flow layer. 1.2 +5 -0 xml-cocoon2/src/documentation/xdocs/userdocs/generators/search-generator.xml Index: search-generator.xml =================================================================== RCS file: /home/cvs/xml-cocoon2/src/documentation/xdocs/userdocs/generators/search-generator.xml,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- search-generator.xml 3 Jan 2002 12:31:04 -0000 1.1 +++ search-generator.xml 6 Sep 2002 03:35:54 -0000 1.2 @@ -213,6 +213,11 @@ uri CDATA #IMPLIED > +<!ELEMENT field (#PCDATA)> +<!ATTLIST field + name CDATA #REQUIRED +> + <!ELEMENT navigation (navigation-page)*> <!ATTLIST navigation total-count CDATA #IMPLIED 1.9 +50 -128 xml-cocoon2/src/java/org/apache/cocoon/generation/SearchGenerator.java Index: SearchGenerator.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/generation/SearchGenerator.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- SearchGenerator.java 8 Mar 2002 04:12:14 -0000 1.8 +++ SearchGenerator.java 6 Sep 2002 03:35:54 -0000 1.9 @@ -71,6 +71,7 @@ import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.document.Document; +import org.apache.lucene.document.Field; import org.apache.lucene.search.Hits; import org.apache.lucene.store.Directory; @@ -82,6 +83,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Enumeration; /** * Generates an XML representation of a search result. @@ -98,22 +100,26 @@ * </p> * <pre><tt> * <?xml version="1.0" encoding="UTF-8"?> + * * <search:results date="1008437081064" query-string="cocoon" - * start-index="0" page-length="10" - * xmlns:search="http://apache.org/cocoon/search/1.0" - * xmlns:xlink="http://www.w3.org/1999/xlink"> - * <search:hits total-count="125" count-of-pages="13"> - * <search:hit rank="0" score="1.0" - * uri="http://localhost:8080/cocoon/documents/hosting.html"/> - * ... - * </search:hits> - * <search:navigation total-count="125" count-of-pages="13" - * has-next="true" has-previous="false" next-index="10" previous-index="0"> - * <search:navigation-page start-index="0"/> - * <search:navigation-page start-index="10"/> - * ... - * <search:navigation-page start-index="120"/> - * </search:navigation> + * start-index="0" page-length="10" + * xmlns:search="http://apache.org/cocoon/search/1.0" + * xmlns:xlink="http://www.w3.org/1999/xlink"> + * <search:hits total-count="125" count-of-pages="13"> + * <search:hit rank="0" score="1.0" + * uri="http://localhost:8080/cocoon/documents/hosting.html"> + * <search:field name="title">Document Title<search:field/> + * <search:hit/> + * ... + * </search:hits> + * + * <search:navigation total-count="125" count-of-pages="13" + * has-next="true" has-previous="false" next-index="10" previous-index="0"> + * <search:navigation-page start-index="0"/> + * <search:navigation-page start-index="10"/> + * ... + * <search:navigation-page start-index="120"/> + * </search:navigation> * </search:results> * </tt></pre> * @@ -127,29 +133,21 @@ /** * The XML namespace for the output document. - * - * @since */ protected final static String namespace = "http://apache.org/cocoon/search/1.0"; /** * The XML namespace for xlink - * - * @since */ protected final static String xlinkNamespace = "http://www.w3.org/1999/xlink"; /** * Description of the Field - * - * @since */ protected final static String CDATA = "CDATA"; /** * Root element of generated xml content, ie <code>results</code>. - * - * @since */ protected final static String RESULTS_ELEMENT = "results"; @@ -157,64 +155,48 @@ * Attribute <code>date</code> of <code>results</code> element. * It contains the date a long value, indicating when a search * generated this xml content. - * - * @since */ protected final static String DATE_ATTRIBUTE = "date"; /** * Attribute <code>query-string</code> of <code>results</code> element. * Echos the <code>queryString</code> query parameter. - * - * @since */ protected final static String QUERY_STRING_ATTRIBUTE = "query-string"; /** * Attribute <code>start-index</code> of <code>results</code> element. * Echos the <code>startIndex</code> query parameter. - * - * @since */ protected final static String START_INDEX_ATTRIBUTE = "start-index"; /** * Attribute <code>page-length</code> of <code>results</code> element. * Echos the <code>pageLenth</code> query parameter. - * - * @since */ protected final static String PAGE_LENGTH_ATTRIBUTE = "page-length"; /** * Child element of generated xml content, ie <code>hits</code>. * This element describes all hits. - * - * @since */ protected final static String HITS_ELEMENT = "hits"; /** * Attribute <code>total-count</code> of <code>hits</code> element. * The value describes total number of hits found by the search engine. - * - * @since */ protected final static String TOTAL_COUNT_ATTRIBUTE = "total-count"; /** * Attribute <code>count-of-pages</code> of <code>hits</code> element. * The value describes number of pages needed for all hits. - * - * @since */ protected final static String COUNT_OF_PAGES_ATTRIBUTE = "count-of-pages"; /** * Child element of generated xml content, ie <code>hit</code>. * This element describes a single hit. - * - * @since */ protected final static String HIT_ELEMENT = "hit"; @@ -222,146 +204,116 @@ * Attribute <code>rank</code> of <code>hit</code> element. * The value describes the count index of this hits, ranging between 0, and * total-count minus 1. - * - * @since */ protected final static String RANK_ATTRIBUTE = "rank"; /** * Attribute <code>score</code> of <code>hit</code> element. * The value describes the score of this hits, ranging between 0, and 1.0. - * - * @since */ protected final static String SCORE_ATTRIBUTE = "score"; /** * Attribute <code>uri</code> of <code>hit</code> element. * The value describes the uri of a document matching the search query. - * - * @since */ protected final static String URI_ATTRIBUTE = "uri"; /** + * Child element <code>field</code> of the <code>hit</code> element. + * This element contains value of the stored field of a hit. + * + * @since 2.0.4 + */ + protected final static String FIELD_ELEMENT = "field"; + + /** * Child element of generated xml content, ie <code>navigation</code>. * This element describes some hints for easier navigation. - * - * @since */ protected final static String NAVIGATION_ELEMENT = "navigation"; /** * Child element of generated xml content, ie <code>navigation</code>. * This element describes the start-index of page containing hits. - * - * @since */ protected final static String NAVIGATION_PAGE_ELEMENT = "navigation-page"; /** * Attribute <code>has-next</code> of <code>navigation-page</code> element. * The value is true if a next navigation control should be presented. - * - * @since */ protected final static String HAS_NEXT_ATTRIBUTE = "has-next"; /** * Attribute <code>has-next</code> of <code>navigation-page</code> element. * The value is true if a previous navigation control should be presented. - * - * @since */ protected final static String HAS_PREVIOUS_ATTRIBUTE = "has-previous"; /** * Attribute <code>next-index</code> of <code>navigation-page</code> element. * The value describes the start-index of the next-to-be-presented page. - * - * @since */ protected final static String NEXT_INDEX_ATTRIBUTE = "next-index"; /** * Attribute <code>previous-index</code> of <code>navigation-page</code> element. * The value describes the start-index of the previous-to-be-presented page. - * - * @since */ protected final static String PREVIOUS_INDEX_ATTRIBUTE = "previous-index"; /** * Setup parameter name of index directory, ie <code>index</code>. - * - * @since */ protected final static String INDEX_PARAM = "index"; /** * Default value of setup parameter <code>index</code>, ie <code>index</code>. - * - * @since */ protected final static String INDEX_PARAM_DEFAULT = "index"; /** * Setup parameter name specifying the name of query-string query parameter, * ie <code>query-string</code>. - * - * @since */ protected final static String QUERY_STRING_PARAM = "query-string"; /** * Default value of setup parameter <code>query-string</code>, ie <code>queryString</code>. - * - * @since */ protected final static String QUERY_STRING_PARAM_DEFAULT = "queryString"; /** * Setup parameter name specifying the name of start-index query parameter, * ie <code>start-index</code>. - * - * @since */ protected final static String START_INDEX_PARAM = "start-index"; /** - *Description of the Field - * - * @since + * Default value of setup parameter <code>start-index</code>, ie <code>startIndex</code>. */ protected final static String START_INDEX_PARAM_DEFAULT = "startIndex"; /** * Setup parameter name specifying the name of start-next-index query parameter, * ie <code>start-next-index</code>. - * - * @since */ protected final static String START_INDEX_NEXT_PARAM = "start-next-index"; /** - *Description of the Field - * - * @since + * Default value of setup parameter <code>start-next-index</code>, ie <code>startNextIndex</code>. */ protected final static String START_INDEX_NEXT_PARAM_DEFAULT = "startNextIndex"; /** * Setup parameter name specifying the name of start-previous-index query parameter, * ie <code>start-previous-index</code>. - * - * @since */ protected final static String START_INDEX_PREVIOUS_PARAM = "start-previous-index"; /** - *Description of the Field - * - * @since + * Default value of setup parameter <code>start-previous-index</code>, ie <code>startPreviousIndex</code>. */ protected final static String START_INDEX_PREVIOUS_PARAM_DEFAULT = "startPreviousIndex"; @@ -375,8 +327,6 @@ /** * Setup parameter name specifying the name of page-length query parameter, * ie <code>page-length</code>. - * - * @since */ protected final static String PAGE_LENGTH_PARAM = "page-length"; @@ -405,50 +355,36 @@ * <p> * By default this directory is set to the <code>WORKING_DIR</code> of Cocoon. * </p> - * - * @since */ private File workDir = null; /** * The avalon component to use for searching. - * - * @since */ private LuceneCocoonSearcher lcs; /** * Absolute filesystem directory of lucene index directory - * - * @since */ private File index = null; /** * Query-string to search for - * - * @since */ private String queryString = ""; /** * Attributes used when generating xml content. - * - * @since */ private final AttributesImpl atts = new AttributesImpl(); /** * startIndex of query parameter - * - * @since */ private Integer startIndex = null; /** * pageLength of query parameter - * - * @since */ private Integer pageLength = null; @@ -585,31 +521,6 @@ /** - * Create an Integer from a String. - * <p> - * Create an Integer from String s, iff conversion fails use default_value. - * </p> - * - * @param s Converting s to an Integer - * @param default_value Default value to use, iff conversion of s fails. - * @return Integer converted value originating from s, or default_value - * @since - */ - private Integer createInteger(String s, int default_value) { - Integer i = new Integer(default_value); - try { - i = new Integer(s); - } catch (NumberFormatException nfe) { - // ignore it, write only warning - if (getLogger().isWarnEnabled()) { - getLogger().warn("Cannot convert " + s + " to Integer", nfe); - } - } - return i; - } - - - /** * Create an Integer. * <p> * Create an Integer from String s, iff conversion fails return null. @@ -680,9 +591,8 @@ * @param pager the LuceneContentPager with the search results * @since * @throws SAXException when there is a problem creating the output SAX events. - * @throws ProcessingException when there is a problem obtaining the hits */ - private void generateHits(LuceneCocoonPager pager) throws SAXException, ProcessingException { + private void generateHits(LuceneCocoonPager pager) throws SAXException { if (pager != null && pager.hasNext()) { atts.clear(); atts.addAttribute(namespace, TOTAL_COUNT_ATTRIBUTE, TOTAL_COUNT_ATTRIBUTE, @@ -702,9 +612,8 @@ * @param pager the LuceneCocoonPager with the search results. * @since * @throws SAXException when there is a problem creating the output SAX events. - * @throws ProcessingException when there is a problem obtaining the hits */ - private void generateHit(LuceneCocoonPager pager) throws SAXException, ProcessingException { + private void generateHit(LuceneCocoonPager pager) throws SAXException { // get the off set to start from int counter = pager.getStartIndex(); @@ -726,6 +635,20 @@ String.valueOf(uri)); contentHandler.startElement(namespace, HIT_ELEMENT, HIT_ELEMENT, atts); // fix me, add here a summary of this hit + for (Enumeration e = doc.fields(); e.hasMoreElements(); ) { + Field field = (Field)e.nextElement(); + if (field.isStored()) { + if (LuceneXMLIndexer.URL_FIELD.equals(field.name())) + continue; + atts.clear(); + atts.addAttribute(namespace, "name", "name", CDATA, field.name()); + contentHandler.startElement(namespace, FIELD_ELEMENT, FIELD_ELEMENT, atts); + String value = field.stringValue(); + contentHandler.characters(value.toCharArray(), 0, value.length()); + contentHandler.endElement(namespace, FIELD_ELEMENT, FIELD_ELEMENT); + } + } + contentHandler.endElement(namespace, HIT_ELEMENT, HIT_ELEMENT); } } @@ -736,10 +659,9 @@ * * @param pager Description of Parameter * @exception SAXException Description of Exception - * @exception ProcessingException Description of Exception * @since */ - private void generateNavigation(LuceneCocoonPager pager) throws SAXException, ProcessingException { + private void generateNavigation(LuceneCocoonPager pager) throws SAXException { if (pager != null) { // generate navigation element
---------------------------------------------------------------------- In case of troubles, e-mail: [EMAIL PROTECTED] To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]