Apache Lucene open-source search software
Bugzilla #20081 - added Bryan LaPlante's patch
Erik Hatcherf3858ea8316c
diff --git a/sandbox/contributions/taglib/WEB-INF/classes/com/netwebapps/taglib/search/CollectionTag.java b/sandbox/contributions/taglib/WEB-INF/classes/com/netwebapps/taglib/search/CollectionTag.javanew file mode 100755index 00000000000..80e40e894bb--- /dev/null+++ b/sandbox/contributions/taglib/WEB-INF/classes/com/netwebapps/taglib/search/CollectionTag.java@@ -0,0 +1,49 @@+/*+ * Created on May 18, 2003+ *+ */+package com.netwebapps.taglib.search;++import java.lang.reflect.Method;++import javax.servlet.jsp.JspException;+import javax.servlet.jsp.tagext.TagSupport;+;++/**+ * @author blaplante+ *+ */+public class CollectionTag extends TagSupport{+ String directory = "";+ /* (non-Javadoc)+ * @see javax.servlet.jsp.tagext.Tag#doStartTag()+ */+ public int doStartTag() throws JspException {+ Object parent = getParent();+ if(parent != null){+ try{+ Method call = parent.getClass().getMethod("addCollection", new Class[] {Class.forName("java.lang.String")});+ call.invoke(parent, new String[] {directory});+ }+ catch(Exception e){+ throw new JspException("An error occured while trying to add a new collection path: " + e.getCause());+ }+ }+ return SKIP_BODY;+ }++ /* (non-Javadoc)+ * @see javax.servlet.jsp.tagext.Tag#release()+ */+ public void release() {+ directory = null;+ }+ /**+ * @param string+ */+ public void setDirectory(String dir) {+ this.directory = dir;+ }++}diff --git a/sandbox/contributions/taglib/WEB-INF/classes/com/netwebapps/taglib/search/SearchTag.java b/sandbox/contributions/taglib/WEB-INF/classes/com/netwebapps/taglib/search/SearchTag.javaindex 2ecc99b1283..8defe139312 100755--- a/sandbox/contributions/taglib/WEB-INF/classes/com/netwebapps/taglib/search/SearchTag.java+++ b/sandbox/contributions/taglib/WEB-INF/classes/com/netwebapps/taglib/search/SearchTag.java@@ -5,6 +5,7 @@ import javax.servlet.jsp.*;import javax.servlet.jsp.tagext.*;import javax.servlet.http.*;import java.io.*;+import org.apache.lucene.analysis.*;import org.apache.lucene.document.*;import org.apache.lucene.index.*;@@ -29,26 +30,34 @@ public class SearchTag extends BodyTagSupport{private String criteria = "";private Iterator searchItr = null;private Enumeration fields = null;- private HashMap aField = null;+ private HashMap aField = new HashMap();private int ROWCOUNT = 0;private int PAGECOUNT = 1;private int HITCOUNT = 0;+ private boolean abort = false;+ private Analyzer analyzer = null;public int startRow = 0;public int maxRows = 50;- public String rowCount = "";+ public String rowCount = "0";public String pageCount = "1";- public String hitCount = "";+ public String hitCount = "0";public String firstPage = "";public String nextPage = "";public String previousPage = "";public String lastPage = "";- public LinkedList pageList = null;+ public LinkedList pageList = new LinkedList();+ public boolean throwOnException = false;public int doStartTag() throws JspException{-+doSearch();+ if(abort){+ rowCount = new Integer(startRow + ROWCOUNT).toString();+ pageContext.setAttribute(getId(),this,PageContext.PAGE_SCOPE);+ return SKIP_BODY;+ }searchItr = hitArray.iterator();if(searchItr.hasNext()){aField = (HashMap) searchItr.next();@@ -60,16 +69,25 @@ public class SearchTag extends BodyTagSupport{}public void doInitBody() throws JspException{- doSearch();- searchItr = hitArray.iterator();- if(searchItr.hasNext()){- aField = (HashMap) searchItr.next();- rowCount = new Integer(startRow + ROWCOUNT).toString();- pageContext.setAttribute(getId(),this,PageContext.PAGE_SCOPE);+ if(!abort){+ doSearch();+ searchItr = hitArray.iterator();+ if(searchItr.hasNext()){+ aField = (HashMap) searchItr.next();+ rowCount = new Integer(startRow + ROWCOUNT).toString();+ pageContext.setAttribute(getId(),this,PageContext.PAGE_SCOPE);+ }}}public int doAfterBody() throws JspException{++ if(abort){+ rowCount = new Integer(startRow + ROWCOUNT).toString();+ pageContext.setAttribute(getId(),this,PageContext.PAGE_SCOPE);+ return SKIP_BODY;+ }+try{getBodyContent().writeOut(getPreviousOut());getBodyContent().clearBody();@@ -88,6 +106,11 @@ public class SearchTag extends BodyTagSupport{}public int doEndTag() throws JspException{++ if(abort){+ return EVAL_PAGE;+ }+try{HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();String relativePath = req.getRequestURI();@@ -124,47 +147,83 @@ public class SearchTag extends BodyTagSupport{}public Set getFields(){- if(aField != null){- return aField.keySet();- }- return null;+ return aField.keySet();}public void doSearch() throws JspException{- try {- searcher = new IndexSearcher(IndexReader.open(collection));- Analyzer analyzer = new StopAnalyzer();++ try {+ searcher = new IndexSearcher(IndexReader.open(collection));+ } catch (IOException e) {+ if(throwOnException){+ throw new JspException("IndexSearcher(IndexReader.open(collection)): " + e);+ }+ abort = true;+ }+ if(!abort){+ analyzer = new StopAnalyzer();+try {- query = QueryParser.parse(criteria, "contents", analyzer);- hits = searcher.search(query);- hitCount = new Integer(hits.length()).toString();- HITCOUNT = hits.length();- PAGECOUNT = PAGECOUNT = (int) (( (double) startRow) / maxRows );- pageCount = new Integer(PAGECOUNT).toString();- thispage = maxRows;- if ((startRow + maxRows) > hits.length()) {- thispage = hits.length() - startRow;+ query = QueryParser.parse(criteria, "contents", analyzer);+ } catch (ParseException e) {+ if(throwOnException){+ throw new JspException("QueryParser.parse(criteria,contents,analyzer): " + e);}- hitArray = new ArrayList();- for (int i = startRow; i < (thispage + startRow); i++) {- hitMap = new HashMap();- Document doc = hits.doc(i);- hitMap.put("score",new Float(hits.score(i)).toString());- fields = doc.fields();- while(fields.hasMoreElements()){- Field field = (Field) fields.nextElement();- String fieldName = field.name();- hitMap.put(fieldName,doc.get(fieldName));+ abort = true;+ }+ if(!abort){+ try {+ hits = searcher.search(query);+ } catch (IOException e) {+ if(throwOnException){+ throw new JspException("searcher.search(query): " + e);+ }+ abort = true;+ }++ if(!abort){+ hitCount = new Integer(hits.length()).toString();+ HITCOUNT = hits.length();+ PAGECOUNT = PAGECOUNT = (int) (( (double) startRow) / maxRows );+ pageCount = new Integer(PAGECOUNT).toString();+ thispage = maxRows;+ if ((startRow + maxRows) > hits.length()) {+ thispage = hits.length() - startRow;+ }+ hitArray = new ArrayList();+ for (int i = startRow; i < (thispage + startRow); i++) {+ hitMap = new HashMap();+ Document doc = null;+ try {+ doc = hits.doc(i);+ } catch (IOException e) {+ if(throwOnException){+ throw new JspException("hits.doc(i) : " + e);+ }+ abort = true;+ }+ if(!abort){+ try {+ hitMap.put("score",new Float(hits.score(i)).toString());+ } catch (IOException e) {+ if(throwOnException){+ throw new JspException("hitMap.put(score,new Float(hits.score(i)).toString()); : " + e);+ }+ abort = true;+ }+ if(!abort){+ fields = doc.fields();+ while(fields.hasMoreElements()){+ Field field = (Field) fields.nextElement();+ String fieldName = field.name();+ hitMap.put(fieldName,doc.get(fieldName));+ }+ hitArray.add(hitMap);+ }+ }}- hitArray.add(hitMap);}}- catch (Exception e){- throw new JspException("An error occurred while parsing the index : " + e.toString());- }- }- catch (Exception e) {- throw new JspException("An error occurred while trying to open the search index: " + e.toString());}}@@ -206,6 +265,10 @@ public class SearchTag extends BodyTagSupport{this.collection = collection;}+ public void setThrowOnException(String bool){+ this.throwOnException = new Boolean(bool).booleanValue();+ }+/* getters */public int getStartRow(){diff --git a/sandbox/contributions/taglib/WEB-INF/lucene-taglib.tld b/sandbox/contributions/taglib/WEB-INF/lucene-taglib.tldindex e8d131ba16e..1a28a3dec27 100755--- a/sandbox/contributions/taglib/WEB-INF/lucene-taglib.tld+++ b/sandbox/contributions/taglib/WEB-INF/lucene-taglib.tld@@ -22,6 +22,11 @@<required>true</required><rtexprvalue>true</rtexprvalue></attribute>+ <attribute>+ <name>throwOnException</name>+ <required>false</required>+ <rtexprvalue>true</rtexprvalue>+ </attribute><attribute><name>criteria</name><required>true</required>@@ -29,7 +34,7 @@</attribute><attribute><name>collection</name>- <required>true</required>+ <required>false</required><rtexprvalue>true</rtexprvalue></attribute><attribute>@@ -43,4 +48,16 @@<rtexprvalue>true</rtexprvalue></attribute></tag>+ <tag>+ <name>Collection</name>+ <tagclass>com.netwebapps.taglib.search.CollectionTag</tagclass>+ <info>+ Adds an additional collection to the search tag.+ </info>+ <attribute>+ <name>directory</name>+ <required>true</required>+ <rtexprvalue>true</rtexprvalue>+ </attribute>+ </tag></taglib>diff --git a/sandbox/contributions/taglib/index.html b/sandbox/contributions/taglib/index.htmlnew file mode 100755index 00000000000..265a9d4e3b9--- /dev/null+++ b/sandbox/contributions/taglib/index.html@@ -0,0 +1,8 @@+<html>+ <head>+ <title>Hello World</title>+ </head>+ <body>+ Hello World+ </body>+</html>diff --git a/sandbox/contributions/taglib/index.jsp b/sandbox/contributions/taglib/index.jspindex 79061259963..6594bdcbbc1 100755--- a/sandbox/contributions/taglib/index.jsp+++ b/sandbox/contributions/taglib/index.jsp@@ -1,3 +1,10 @@++<%+response.setHeader("Pragma", "No-cache");+response.setDateHeader("Expires", 0);+response.setHeader("Cache-Control", "no-cache");+response.setHeader("Cache-Control", "private");+%><%@include file="header.jsp"%><% /* Author: Andrew C. Oliver ([email protected]) */ %><center>diff --git a/sandbox/contributions/taglib/result.jsp b/sandbox/contributions/taglib/result.jspindex 3d54345d4e3..dba4e9da30a 100755--- a/sandbox/contributions/taglib/result.jsp+++ b/sandbox/contributions/taglib/result.jsp@@ -1,5 +1,4 @@<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">-<%@ taglib uri="/WEB-INF/lucene-taglib.tld" prefix="JSP"%><%@ include file="header.jsp"%><%@ page import="java.util.*"%>@@ -15,34 +14,43 @@catch(Exception e){}%>-<table border=3>+<table border=3>- <JSP:Search id="rs" collection="E:/opt/lucene/index" criteria="<%= query %>" startRow="<%= startRow %>" maxRows="<%= maxRows %>">+ <JSP:Search throwOnException="false" id="rs" collection="E:/opt/lucene/index" criteria="<%= query %>" startRow="<%= startRow %>" maxRows="<%= maxRows %>"><%+Set allFields = rs.getFields();int fieldSize = allFields.size();Iterator fieldIter = allFields.iterator();+while(fieldIter.hasNext()){String nextField = (String) fieldIter.next();if(!nextField.equalsIgnoreCase("summary")){- %>+ %><tr><td><b><%= nextField %></b></td><td><%= rs.getField(nextField) %></td></tr><%}else{%>- <tr><td colspan="2"><b><%= nextField %></b></td></tr>+ <tr><td colspan="2"><b><%= rs.hitCount %>|<%= nextField %></b></td></tr><tr><td colspan="2"><%= rs.getField(nextField) %></td></tr><%}}+%></JSP:Search><%- if(new Integer(rs.hitCount).intValue() <= 0){+ int count = 0;+ try{+ count = new Integer(rs.hitCount).intValue();+ }catch(Exception e){+ out.print(e);+ }+ if(count <= 0){%><tr>- <td colspan=2>No results were found</td>+ <td colspan=2>No results have been found</td></tr><%}