#19834 - Taglib contribution from Bryan LaPlante

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@150886 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Erik Hatcher 2003-05-12 14:02:36 +00:00
parent c83d0478cf
commit 620516c21c
10 changed files with 472 additions and 0 deletions

View File

@ -0,0 +1,53 @@
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Lucene" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Lucene", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/

View File

@ -0,0 +1,218 @@
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 = null;
private int ROWCOUNT = 0;
private int PAGECOUNT = 1;
private int HITCOUNT = 0;
public int startRow = 0;
public int maxRows = 50;
public String rowCount = "";
public String pageCount = "1";
public String hitCount = "";
public String firstPage = "";
public String nextPage = "";
public String previousPage = "";
public String lastPage = "";
public LinkedList pageList = null;
public int doStartTag() 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);
return EVAL_BODY_AGAIN;
}
return SKIP_BODY;
}
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);
}
}
public int doAfterBody() throws JspException{
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{
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(){
if(aField != null){
return aField.keySet();
}
return null;
}
public void doSearch() throws JspException{
try {
searcher = new IndexSearcher(IndexReader.open(collection));
Analyzer 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;
}
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));
}
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());
}
}
/* 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;
}
/* getters */
public int getStartRow(){
return startRow;
}
public int getMaxRows(){
return maxRows;
}
}

View File

@ -0,0 +1,26 @@
package com.netwebapps.taglib.search;
/**
* @author Network Web Application
* @author Bryan LaPlante
*
*/
import javax.servlet.jsp.tagext.*;
public class SearchTagTei extends TagExtraInfo
{
public SearchTagTei(){
}
/*
* VariableInfo is provided by the servlet container and allows the
* SearchTag class to output it's tag variables to the PageContext at runtime
* @see javax.servlet.jsp.tagext.TagExtraInfo#getVariableInfo(javax.servlet.jsp.tagext.TagData)
*/
public VariableInfo[] getVariableInfo(TagData tagdata)
{
VariableInfo avariableinfo[] = new VariableInfo[1];
avariableinfo[0] = new VariableInfo(tagdata.getId(),"com.netwebapps.taglib.search.SearchTag", true, VariableInfo.AT_BEGIN);
return avariableinfo;
}
}

View File

@ -0,0 +1,46 @@
<?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>criteria</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>collection</name>
<required>true</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>

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
</web-app>

View File

@ -0,0 +1,2 @@
</body>
</html>

View File

@ -0,0 +1,10 @@
<html>
<header>
<title>Network Web Applications Search taglib</title>
</header>
<body>
<center>
<p>
Welcome to the Lucene Template application. (This is the header)
</p>
</center>

View File

@ -0,0 +1,14 @@
<%@include file="header.jsp"%>
<% /* Author: Andrew C. Oliver (acoliver2@users.sourceforge.net) */ %>
<center>
<form name="search" action="result.jsp" method="get">
<p>
<input name="query" size="44"/>&nbsp;Search Criteria
</p>
<p>
<input name="maxRows" size="4" value="10"/>&nbsp;Results Per Page&nbsp;
<input type="submit" value="Search"/>
</p>
</form>
</center>
<%@include file="footer.jsp"%>

View File

@ -0,0 +1,33 @@
INTRODUCTION
The lucene-taglib project provides a tag library for searching
a lucene-index.
INSTRUCTIONS
1. download this project and create a context in your favorite
servelt container called lucene-taglib.
2. copy the file under the taglib directory into your new context.
3. open result.jsp and locate the collection attribute of the
<LUCENE:Search> tag.
4. change the collection attribute to point to a lucene-index
that you created using the system appropriate path.
5. open index.jsp in your browser and enter search criteria
and click the submit button.
DOCUMENTATION:
you will find documentation and an over view of the tag library in
the docs folder of this project and the javadocs in the api folder
respectivley.
BUGS:
I tried to create a .war file for this project but I am having trouble
getting it to deploy properly.
PLANNED:
I am planning to document the result.jsp file line for line to explain
how to display a search result when you do not know what the names of
the search fields stored in the lucene-index. That is the way the result
page is currently written.
Time permitting I want to write a couple of child tags for telling the search
tag that there are multiple index to be searched and to let it do the other
types of searches such as fuzzy and range queries.

View File

@ -0,0 +1,56 @@
<!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.*"%>
<%
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 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"><%= rs.getField(nextField) %></td></tr>
<%
}
}
%>
</JSP:Search>
<%
if(new Integer(rs.hitCount).intValue() <= 0){
%>
<tr>
<td colspan=2>No results were found</td>
</tr>
<%
}
%>
<tr>
<td colspan=2><%= rs.hitCount %></td>
</tr>
</table>
<%@include file="footer.jsp"%>