lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Bryan LaPlante" <blapla...@netwebapps.com>
Subject SearchTag fixed
Date Tue, 20 May 2003 19:06:32 GMT
Hi all,
I have been working on a few issues with the ability to crash the JSP:Search
tag when no criteria have been submitted. With out imposing validation
restrictions on the tag itself I put in some more robust exception handling
and put into place some fail safe logic that will abort the tag if there is
a problem. I added a new attribute called throwOnException="true|false" so
that if you want to do debugging you can turn it on. Each line of lucene
code that throws exceptions looks like this.

try{
    //volatile code here
}catch(SomeException e)
    if(throwOnException){
        throw new JspException("class name: " + e);
    }
    abort = true;
}

if any of the code fails the tag expose the current variable state into the
PAGE_SCOPE and exit. I am having a problem with caching. I am not sure if it
is a problem with Tom Cat or the search engine. I hope you can give me some
advice.

When I run the tag and pass it good criteria the tag responds with the
results as expected. When I hit the back button and submit an empty form
field the tag stops processing as expected but still shows the hitCount from
the previous query, ever there after I can not get the tag to produce
results from an criteria, the values from the previous run are cached. Is
this a Tom Cat setting or am I doing something wrong in the way I am
processing the tag?

I am posting the entire tag at the bottom of this email but if there is a
better way to provide the example please let me know. I will also submit it
as a bug report.

Bryan LaPlante

<%-- SearchTag.java --%>
package com.netwebapps.taglib.search;

import java.util.*;

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.*;

import org.apache.lucene.search.*;

import org.apache.lucene.queryParser.*;

/*

*

* @author Bryan LaPlante

* @param

*

*/

public class SearchTag extends BodyTagSupport{


private HashMap hitMap = null;

private ArrayList hitArray = null;

private String collection = "";

private IndexSearcher searcher = null;

private Query query = null;

private Hits hits = null;

private int thispage = 0;

private String criteria = "";

private Iterator searchItr = null;

private Enumeration fields = 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 = "0";

public String pageCount = "1";

public String hitCount = "0";

public String firstPage = "";

public String nextPage = "";

public String previousPage = "";

public String lastPage = "";

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();

rowCount = new Integer(startRow + ROWCOUNT++).toString();

pageContext.setAttribute(getId(),this,PageContext.PAGE_SCOPE);

return EVAL_BODY_AGAIN;

}

return SKIP_BODY;

}


public void doInitBody() throws JspException{

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();

}

catch(IOException e){

throw new JspException(e.toString());

}


if(searchItr.hasNext()){

aField = (HashMap) searchItr.next();

rowCount = new Integer(startRow + ROWCOUNT++).toString();

pageContext.setAttribute(getId(),this,PageContext.PAGE_SCOPE);

return EVAL_BODY_AGAIN;

}

return SKIP_BODY;

}


public int doEndTag() throws JspException{

if(abort){

return EVAL_PAGE;

}


try{

HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();

String relativePath = req.getRequestURI();

firstPage = relativePath + "?startRow=0&maxRows=" + maxRows;

nextPage = relativePath + "?startRow=" + ((startRow + maxRows <= HITCOUNT)?
startRow + maxRows : startRow) + "&maxRows=" + maxRows;

previousPage = relativePath + "?startRow=" + ((startRow - maxRows >=0)?
startRow - maxRows : 0 ) + "&maxRows=" + maxRows;

lastPage = relativePath + "?startRow=" + (((HITCOUNT - maxRows) >= 0)?
HITCOUNT - maxRows : 0) + "&maxRows=" + maxRows;

if(HITCOUNT > 0){

pageList = new LinkedList();

for(int i=0; i < (HITCOUNT / maxRows); i++){

String tempURL = relativePath + "?startRow=" + (maxRows * i) + "&maxRows=" +
maxRows;

pageList.add(tempURL);

}

}

}

catch(Exception e){

throw new JspException("A problem occured durring doEndTag: " +
e.toString());

}

pageContext.setAttribute(getId(),this,PageContext.PAGE_SCOPE);

return EVAL_PAGE;

}


public void release(){

}


public String getField(String name){

if(aField != null){

if(aField.containsKey(name)){

return aField.get((String) name).toString();

}

}

return "";

}


public Set getFields(){

return aField.keySet();

}


public void doSearch() throws JspException{

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

} catch (ParseException e) {

if(throwOnException){

throw new JspException("QueryParser.parse(criteria,contents,analyzer): " +
e);

}

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

}

}

}

}

}

}

}


/* setters */




public void setCriteria(String criteria){

this.criteria = criteria;

}


public void setStartRow(String startRow){

try{

this.startRow = Integer.parseInt(startRow);

}

catch(Exception e){

this.startRow = 0;

}

}


public void setStartRow(int startRow){

this.startRow = startRow;

}


public void setMaxRows(String maxRows){

try{

this.maxRows = Integer.parseInt(maxRows);

}

catch(Exception e){

this.maxRows = 10;

}

}


public void setMaxRows(int maxRows){

this.maxRows = maxRows;

}


public void setCollection(String collection){

this.collection = collection;

}


public void setThrowOnException(String bool){

this.throwOnException = new Boolean(bool).booleanValue();

}


/* getters */


public int getStartRow(){

return startRow;

}


public int getMaxRows(){

return maxRows;

}

}

<%-- END SearchTag.java --%>



<%-- Result.jsp --%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<%@ taglib uri="/WEB-INF/lucene-taglib.tld" prefix="JSP"%>

<%@ page import="java.util.*"%>

<%

String startRow = "0";

String maxRows = "10";

String query = request.getParameter("query");

try{

startRow = request.getParameter("startRow");

maxRows = request.getParameter("maxRows");

}

catch(Exception e){

}

%>

<table border=3>

<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><%= rs.hitCount %>|<%= nextField %></b></td></tr>

<tr><td colspan="2"><%= rs.getField(nextField) %></td></tr>

<%

}

}


%>

</JSP:Search>

<%

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 have been found</td>

</tr>

<%

}

%>

<tr>

<td colspan=2><%= rs.hitCount %></td>

</tr>

</table>

<%-- END result.jsp --%>



<%-- lucene-taglib.tld --%>

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE taglib

PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"

"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">

<taglib>

<tlibversion>1.1</tlibversion>

<jspversion>1.2</jspversion>

<shortname>JSP</shortname>

<info>

Lucene search engine tag library

</info>

<tag>

<name>Search</name>

<tagclass>com.netwebapps.taglib.search.SearchTag</tagclass>

<teiclass>com.netwebapps.taglib.search.SearchTagTei</teiclass>

<bodycontent>JSP</bodycontent>

<info>

Outputs select options from a query.

</info>

<attribute>

<name>id</name>

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

<rtexprvalue>true</rtexprvalue>

</attribute>

<attribute>

<name>collection</name>

<required>false</required>

<rtexprvalue>true</rtexprvalue>

</attribute>

<attribute>

<name>startRow</name>

<required>false</required>

<rtexprvalue>true</rtexprvalue>

</attribute>

<attribute>

<name>maxRows</name>

<required>false</required>

<rtexprvalue>true</rtexprvalue>

</attribute>

</tag>

</taglib>



<%-- END lucene-taglib.tld --%>





---------------------------------------------------------------------
To unsubscribe, e-mail: lucene-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: lucene-dev-help@jakarta.apache.org


Mime
View raw message