Bugzilla #20081 - added Bryan LaPlante's patch

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@150889 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Erik Hatcher 2003-05-21 01:22:22 +00:00
parent 36893fb9a6
commit f3858ea831
6 changed files with 204 additions and 52 deletions

View File

@ -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;
}
}

View File

@ -5,6 +5,7 @@ import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*; import javax.servlet.jsp.tagext.*;
import javax.servlet.http.*; import javax.servlet.http.*;
import java.io.*; import java.io.*;
import org.apache.lucene.analysis.*; import org.apache.lucene.analysis.*;
import org.apache.lucene.document.*; import org.apache.lucene.document.*;
import org.apache.lucene.index.*; import org.apache.lucene.index.*;
@ -29,26 +30,34 @@ public class SearchTag extends BodyTagSupport{
private String criteria = ""; private String criteria = "";
private Iterator searchItr = null; private Iterator searchItr = null;
private Enumeration fields = null; private Enumeration fields = null;
private HashMap aField = null; private HashMap aField = new HashMap();
private int ROWCOUNT = 0; private int ROWCOUNT = 0;
private int PAGECOUNT = 1; private int PAGECOUNT = 1;
private int HITCOUNT = 0; private int HITCOUNT = 0;
private boolean abort = false;
private Analyzer analyzer = null;
public int startRow = 0; public int startRow = 0;
public int maxRows = 50; public int maxRows = 50;
public String rowCount = ""; public String rowCount = "0";
public String pageCount = "1"; public String pageCount = "1";
public String hitCount = ""; public String hitCount = "0";
public String firstPage = ""; public String firstPage = "";
public String nextPage = ""; public String nextPage = "";
public String previousPage = ""; public String previousPage = "";
public String lastPage = ""; public String lastPage = "";
public LinkedList pageList = null; public LinkedList pageList = new LinkedList();
public boolean throwOnException = false;
public int doStartTag() throws JspException{ public int doStartTag() throws JspException{
doSearch(); doSearch();
if(abort){
rowCount = new Integer(startRow + ROWCOUNT).toString();
pageContext.setAttribute(getId(),this,PageContext.PAGE_SCOPE);
return SKIP_BODY;
}
searchItr = hitArray.iterator(); searchItr = hitArray.iterator();
if(searchItr.hasNext()){ if(searchItr.hasNext()){
aField = (HashMap) searchItr.next(); aField = (HashMap) searchItr.next();
@ -60,16 +69,25 @@ public class SearchTag extends BodyTagSupport{
} }
public void doInitBody() throws JspException{ public void doInitBody() throws JspException{
doSearch(); if(!abort){
searchItr = hitArray.iterator(); doSearch();
if(searchItr.hasNext()){ searchItr = hitArray.iterator();
aField = (HashMap) searchItr.next(); if(searchItr.hasNext()){
rowCount = new Integer(startRow + ROWCOUNT).toString(); aField = (HashMap) searchItr.next();
pageContext.setAttribute(getId(),this,PageContext.PAGE_SCOPE); rowCount = new Integer(startRow + ROWCOUNT).toString();
pageContext.setAttribute(getId(),this,PageContext.PAGE_SCOPE);
}
} }
} }
public int doAfterBody() throws JspException{ public int doAfterBody() throws JspException{
if(abort){
rowCount = new Integer(startRow + ROWCOUNT).toString();
pageContext.setAttribute(getId(),this,PageContext.PAGE_SCOPE);
return SKIP_BODY;
}
try{ try{
getBodyContent().writeOut(getPreviousOut()); getBodyContent().writeOut(getPreviousOut());
getBodyContent().clearBody(); getBodyContent().clearBody();
@ -88,6 +106,11 @@ public class SearchTag extends BodyTagSupport{
} }
public int doEndTag() throws JspException{ public int doEndTag() throws JspException{
if(abort){
return EVAL_PAGE;
}
try{ try{
HttpServletRequest req = (HttpServletRequest) pageContext.getRequest(); HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
String relativePath = req.getRequestURI(); String relativePath = req.getRequestURI();
@ -124,47 +147,83 @@ public class SearchTag extends BodyTagSupport{
} }
public Set getFields(){ public Set getFields(){
if(aField != null){ return aField.keySet();
return aField.keySet();
}
return null;
} }
public void doSearch() throws JspException{ public void doSearch() throws JspException{
try {
searcher = new IndexSearcher(IndexReader.open(collection)); try {
Analyzer analyzer = new StopAnalyzer(); 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 { try {
query = QueryParser.parse(criteria, "contents", analyzer); query = QueryParser.parse(criteria, "contents", analyzer);
hits = searcher.search(query); } catch (ParseException e) {
hitCount = new Integer(hits.length()).toString(); if(throwOnException){
HITCOUNT = hits.length(); throw new JspException("QueryParser.parse(criteria,contents,analyzer): " + e);
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(); abort = true;
for (int i = startRow; i < (thispage + startRow); i++) { }
hitMap = new HashMap(); if(!abort){
Document doc = hits.doc(i); try {
hitMap.put("score",new Float(hits.score(i)).toString()); hits = searcher.search(query);
fields = doc.fields(); } catch (IOException e) {
while(fields.hasMoreElements()){ if(throwOnException){
Field field = (Field) fields.nextElement(); throw new JspException("searcher.search(query): " + e);
String fieldName = field.name(); }
hitMap.put(fieldName,doc.get(fieldName)); 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; this.collection = collection;
} }
public void setThrowOnException(String bool){
this.throwOnException = new Boolean(bool).booleanValue();
}
/* getters */ /* getters */
public int getStartRow(){ public int getStartRow(){

View File

@ -22,6 +22,11 @@
<required>true</required> <required>true</required>
<rtexprvalue>true</rtexprvalue> <rtexprvalue>true</rtexprvalue>
</attribute> </attribute>
<attribute>
<name>throwOnException</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute> <attribute>
<name>criteria</name> <name>criteria</name>
<required>true</required> <required>true</required>
@ -29,7 +34,7 @@
</attribute> </attribute>
<attribute> <attribute>
<name>collection</name> <name>collection</name>
<required>true</required> <required>false</required>
<rtexprvalue>true</rtexprvalue> <rtexprvalue>true</rtexprvalue>
</attribute> </attribute>
<attribute> <attribute>
@ -43,4 +48,16 @@
<rtexprvalue>true</rtexprvalue> <rtexprvalue>true</rtexprvalue>
</attribute> </attribute>
</tag> </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> </taglib>

View File

@ -0,0 +1,8 @@
<html>
<head>
<title>Hello World</title>
</head>
<body>
Hello World
</body>
</html>

View File

@ -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"%> <%@include file="header.jsp"%>
<% /* Author: Andrew C. Oliver (acoliver2@users.sourceforge.net) */ %> <% /* Author: Andrew C. Oliver (acoliver2@users.sourceforge.net) */ %>
<center> <center>

View File

@ -1,5 +1,4 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<%@ taglib uri="/WEB-INF/lucene-taglib.tld" prefix="JSP"%> <%@ taglib uri="/WEB-INF/lucene-taglib.tld" prefix="JSP"%>
<%@ include file="header.jsp"%> <%@ include file="header.jsp"%>
<%@ page import="java.util.*"%> <%@ page import="java.util.*"%>
@ -15,34 +14,43 @@
catch(Exception e){ catch(Exception e){
} }
%> %>
<table border=3> <table border=3>
<JSP:Search throwOnException="false" id="rs" collection="E:/opt/lucene/index" criteria="<%= query %>" startRow="<%= startRow %>" maxRows="<%= maxRows %>">
<JSP:Search id="rs" collection="E:/opt/lucene/index" criteria="<%= query %>" startRow="<%= startRow %>" maxRows="<%= maxRows %>">
<% <%
Set allFields = rs.getFields(); Set allFields = rs.getFields();
int fieldSize = allFields.size(); int fieldSize = allFields.size();
Iterator fieldIter = allFields.iterator(); Iterator fieldIter = allFields.iterator();
while(fieldIter.hasNext()){ while(fieldIter.hasNext()){
String nextField = (String) fieldIter.next(); String nextField = (String) fieldIter.next();
if(!nextField.equalsIgnoreCase("summary")){ if(!nextField.equalsIgnoreCase("summary")){
%> %>
<tr><td><b><%= nextField %></b></td><td><%= rs.getField(nextField) %></td></tr> <tr><td><b><%= nextField %></b></td><td><%= rs.getField(nextField) %></td></tr>
<% <%
}else{ }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> <tr><td colspan="2"><%= rs.getField(nextField) %></td></tr>
<% <%
} }
} }
%> %>
</JSP:Search> </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> <tr>
<td colspan=2>No results were found</td> <td colspan=2>No results have been found</td>
</tr> </tr>
<% <%
} }