mirror of https://github.com/apache/lucene.git
Initial submit of SerachBean and related classes
git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@150776 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
7a3b5acf37
commit
d27d39befb
|
@ -0,0 +1,391 @@
|
|||
<?xml version="1.0"?>
|
||||
|
||||
<project name="SearchBean" default="jar" basedir=".">
|
||||
|
||||
<!-- Give user a chance to override without editing this file
|
||||
(and without typing -D each time it compiles it -->
|
||||
<property file="${user.home}/lucene.build.properties" />
|
||||
<property file="${user.home}/build.properties" />
|
||||
<property file="${basedir}/build.properties" />
|
||||
<property file="${basedir}/default.properties" />
|
||||
|
||||
<!-- Build classpath -->
|
||||
<path id="classpath">
|
||||
<pathelement location="${build.classes}"/>
|
||||
<pathelement location="${build.test.classes}"/>
|
||||
<pathelement location="."/>
|
||||
<fileset dir="lib">
|
||||
<include name="*.jar" />
|
||||
</fileset>
|
||||
</path>
|
||||
|
||||
<path id="junit.classpath">
|
||||
<pathelement location="${junit.classes}" />
|
||||
<pathelement location="${build.classes}"/>
|
||||
<fileset dir="lib">
|
||||
<include name="*.jar" />
|
||||
</fileset>
|
||||
<pathelement path="${java.class.path}" />
|
||||
</path>
|
||||
|
||||
<!-- ================================================================== -->
|
||||
<!-- Prepares the build directory -->
|
||||
<!-- ================================================================== -->
|
||||
<target name="init">
|
||||
<mkdir dir="${build.dir}"/>
|
||||
<mkdir dir="${build.classes}"/>
|
||||
<mkdir dir="${build.src}"/>
|
||||
|
||||
<available
|
||||
property="junit.present"
|
||||
classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask"
|
||||
/>
|
||||
</target>
|
||||
|
||||
|
||||
|
||||
<!-- ================================================================== -->
|
||||
<!-- C O M P I L E -->
|
||||
<!-- ================================================================== -->
|
||||
<!-- -->
|
||||
<!-- ================================================================== -->
|
||||
<target name="compile" depends="init" >
|
||||
|
||||
<javac
|
||||
encoding="${build.encoding}"
|
||||
srcdir="${src.dir}"
|
||||
includes="org/**/*.java"
|
||||
destdir="${build.classes}"
|
||||
debug="${debug}">
|
||||
<classpath refid="classpath"/>
|
||||
</javac>
|
||||
</target>
|
||||
|
||||
<!-- ================================================================== -->
|
||||
<!-- J A R -->
|
||||
<!-- ================================================================== -->
|
||||
<!-- -->
|
||||
<!-- ================================================================== -->
|
||||
<target name="jar" depends="compile" >
|
||||
<jar
|
||||
jarfile="${build.dir}/${final.name}.jar"
|
||||
basedir="${build.classes}"
|
||||
excludes="**/*.java"
|
||||
/>
|
||||
</target>
|
||||
|
||||
|
||||
<!-- ================================================================== -->
|
||||
<!-- J A R S O U R C E -->
|
||||
<!-- ================================================================== -->
|
||||
<!-- -->
|
||||
<!-- ================================================================== -->
|
||||
<target name="jar-src" depends="init">
|
||||
<jar jarfile="${build.dir}/${final.name}-src.jar">
|
||||
<fileset dir="${build.dir}" includes="**/*.java"/>
|
||||
</jar>
|
||||
</target>
|
||||
|
||||
|
||||
<!-- ================================================================== -->
|
||||
<!-- B U I L D T E S T -->
|
||||
<!-- ================================================================== -->
|
||||
<!-- -->
|
||||
<!-- ================================================================== -->
|
||||
<target name="test" depends="compile">
|
||||
<mkdir dir="${build.test}"/>
|
||||
|
||||
<copy todir="${build.test.src}">
|
||||
<fileset dir="${test.src}">
|
||||
<include name="**/*.java"/>
|
||||
</fileset>
|
||||
</copy>
|
||||
|
||||
<mkdir dir="${build.test.classes}"/>
|
||||
|
||||
<javac
|
||||
encoding="${build.encoding}"
|
||||
srcdir="${build.test.src}"
|
||||
includes="**/*.java"
|
||||
destdir="${build.test.classes}"
|
||||
debug="${debug}">
|
||||
<classpath refid="classpath"/>
|
||||
</javac>
|
||||
</target>
|
||||
|
||||
<!-- ================================================================== -->
|
||||
<!-- R U N T E S T S -->
|
||||
<!-- ================================================================== -->
|
||||
<!-- -->
|
||||
<!-- ================================================================== -->
|
||||
<target name="test-unit" depends="compile,test" if="junit.present">
|
||||
<!-- Import JUnit task -->
|
||||
<taskdef
|
||||
name="junit"
|
||||
classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask"
|
||||
/>
|
||||
|
||||
<mkdir dir="${junit.classes}"/>
|
||||
<mkdir dir="${junit.reports}"/>
|
||||
<javac
|
||||
encoding="${build.encoding}"
|
||||
srcdir="${junit.src}"
|
||||
includes="**/*.java"
|
||||
destdir="${junit.classes}"
|
||||
debug="${debug}">
|
||||
<classpath refid="classpath"/>
|
||||
</javac>
|
||||
|
||||
<junit printsummary="yes" haltonfailure="no" >
|
||||
<classpath refid="junit.classpath"/>
|
||||
<formatter type="plain" />
|
||||
<batchtest fork="yes" todir="${junit.reports}">
|
||||
<fileset dir="${junit.src}" includes="**/Test*.java" />
|
||||
</batchtest>
|
||||
</junit>
|
||||
</target>
|
||||
|
||||
<!-- ================================================================== -->
|
||||
<!-- J A V A D O C -->
|
||||
<!-- ================================================================== -->
|
||||
<!-- -->
|
||||
<!-- ================================================================== -->
|
||||
<target name="javadocs" depends="compile" >
|
||||
<mkdir dir="${build.javadocs}"/>
|
||||
<javadoc
|
||||
sourcepath="${src.dir}"
|
||||
overview="${src.dir}/overview.html"
|
||||
packagenames="${packages}"
|
||||
destdir="${build.javadocs}"
|
||||
author="true"
|
||||
version="true"
|
||||
use="true"
|
||||
link="${javadoc.link}"
|
||||
windowtitle="${Name} ${version} API"
|
||||
doctitle="${Name} ${version} API"
|
||||
bottom="Copyright &copy; ${year} Apache Software Foundation. All Rights Reserved."
|
||||
>
|
||||
<classpath refid="classpath"/>
|
||||
</javadoc>
|
||||
</target>
|
||||
|
||||
<!-- ================================================================== -->
|
||||
<!-- D I S T R I B U T I O N -->
|
||||
<!-- ================================================================== -->
|
||||
<!-- -->
|
||||
<!-- ================================================================== -->
|
||||
<target name="package" depends="jar, javadocs">
|
||||
<mkdir dir="${dist.dir}"/>
|
||||
<mkdir dir="${dist.dir}/docs"/>
|
||||
<mkdir dir="${dist.dir}/docs/api"/>
|
||||
<mkdir dir="${dist.dir}/src/jsp"/>
|
||||
|
||||
<copy todir="${dist.dir}/docs">
|
||||
<fileset dir="${docs.dir}"/>
|
||||
</copy>
|
||||
<copy todir="${dist.dir}/docs/api">
|
||||
<fileset dir="${build.javadocs}"/>
|
||||
</copy>
|
||||
|
||||
<copy todir="${dist.dir}/src/jsp">
|
||||
<fileset dir="src/jsp"/>
|
||||
</copy>
|
||||
<copy todir="${dist.dir}/lib">
|
||||
<fileset dir="lib"/>
|
||||
</copy>
|
||||
<copy todir="${dist.dir}">
|
||||
<fileset dir=".">
|
||||
<include name="*.txt" />
|
||||
</fileset>
|
||||
</copy>
|
||||
<copy file="${build.dir}/${final.name}.jar" todir="${dist.dir}"/>
|
||||
</target>
|
||||
|
||||
<!-- ================================================================== -->
|
||||
<!-- Packages the distribution with zip -->
|
||||
<!-- ================================================================== -->
|
||||
<!-- -->
|
||||
<!-- ================================================================== -->
|
||||
<target name="package-zip" depends="package"
|
||||
description="--> Generates the SearchBean distribution as .zip">
|
||||
|
||||
<delete file="${basedir}/${final.name}.zip"/>
|
||||
<zip
|
||||
zipfile="${basedir}/${final.name}.zip"
|
||||
basedir="${basedir}/"
|
||||
includes="**/${final.name}/**"
|
||||
/>
|
||||
</target>
|
||||
|
||||
<!-- ================================================================== -->
|
||||
<!-- packages the distribution with tar-gzip -->
|
||||
<!-- ================================================================== -->
|
||||
<!-- -->
|
||||
<!-- ================================================================== -->
|
||||
<target name="package-tgz" depends="package"
|
||||
description="--> generates the SearchBean distribution as .tar.gz">
|
||||
|
||||
<delete file="${basedir}/${final.name}.tar"/>
|
||||
<delete file="${basedir}/${final.name}.tar.gz"/>
|
||||
<tar
|
||||
tarfile="${basedir}/${final.name}.tar"
|
||||
basedir="${basedir}/"
|
||||
includes="**/${final.name}/**"
|
||||
/>
|
||||
|
||||
<gzip
|
||||
zipfile="${basedir}/${final.name}.tar.gz"
|
||||
src="${basedir}/${final.name}.tar"
|
||||
/>
|
||||
</target>
|
||||
|
||||
<!-- ================================================================== -->
|
||||
<!-- packages the distribution with zip and tar-gzip -->
|
||||
<!-- ================================================================== -->
|
||||
<!-- -->
|
||||
<!-- ================================================================== -->
|
||||
<target name="package-all" depends="package-zip, package-tgz"
|
||||
description="--> generates the .tar.gz and .zip distributions">
|
||||
</target>
|
||||
|
||||
<!-- ================================================================== -->
|
||||
<!-- same as package-all. it is just here for compatibility. -->
|
||||
<!-- ================================================================== -->
|
||||
<!-- -->
|
||||
<!-- ================================================================== -->
|
||||
<target name="dist" depends="package-all">
|
||||
</target>
|
||||
|
||||
<!-- ================================================================== -->
|
||||
<!-- S O U R C E D I S T R I B U T I O N -->
|
||||
<!-- ================================================================== -->
|
||||
<!-- -->
|
||||
<!-- ================================================================== -->
|
||||
<target name="package-src" depends="jar-src">
|
||||
<mkdir dir="${dist-src.dir}"/>
|
||||
<copy todir="${dist-src.dir}/src">
|
||||
<fileset dir="src"/>
|
||||
</copy>
|
||||
<copy todir="${dist-src.dir}/lib">
|
||||
<fileset dir="lib"/>
|
||||
</copy>
|
||||
<copy todir="${dist-src.dir}/" file="build.xml"/>
|
||||
<copy todir="${dist-src.dir}/" file="default.properties"/>
|
||||
<copy todir="${dist-src.dir}">
|
||||
<fileset dir=".">
|
||||
<include name="*.txt" />
|
||||
</fileset>
|
||||
</copy>
|
||||
<copy file="${build.dir}/${final.name}-src.jar" todir="${dist-src.dir}"/>
|
||||
</target>
|
||||
|
||||
<!-- ================================================================== -->
|
||||
<!-- Packages the sources with zip -->
|
||||
<!-- ================================================================== -->
|
||||
<!-- -->
|
||||
<!-- ================================================================== -->
|
||||
<target name="package-zip-src" depends="package-src"
|
||||
description="--> Generates the SearchBean sources as .zip">
|
||||
|
||||
<delete file="${basedir}/${final.name}-src.zip"/>
|
||||
<zip
|
||||
zipfile="${basedir}/${final.name}-src.zip"
|
||||
basedir="${basedir}/"
|
||||
includes="**/${final.name}-src/**"
|
||||
/>
|
||||
</target>
|
||||
|
||||
<!-- ================================================================== -->
|
||||
<!-- Packages the sources with tar-gzip -->
|
||||
<!-- ================================================================== -->
|
||||
<!-- -->
|
||||
<!-- ================================================================== -->
|
||||
<target name="package-tgz-src" depends="package-src"
|
||||
description="--> Generates the SearchBean distribution as .tar.gz">
|
||||
|
||||
<delete file="${basedir}/${final.name}-src.tar"/>
|
||||
<delete file="${basedir}/${final.name}-src.tar.gz"/>
|
||||
<tar
|
||||
tarfile="${basedir}/${final.name}-src.tar"
|
||||
basedir="${basedir}/"
|
||||
includes="**/${final.name}-src/**"
|
||||
/>
|
||||
|
||||
<gzip
|
||||
zipfile="${basedir}/${final.name}-src.tar.gz"
|
||||
src="${basedir}/${final.name}-src.tar"
|
||||
/>
|
||||
</target>
|
||||
|
||||
<!-- ================================================================== -->
|
||||
<!-- Packages the sources with zip and tar-gzip -->
|
||||
<!-- ================================================================== -->
|
||||
<!-- -->
|
||||
<!-- ================================================================== -->
|
||||
<target name="package-all-src" depends="package-zip-src, package-tgz-src"
|
||||
description="--> Generates the .tar.gz and .zip source distributions">
|
||||
</target>
|
||||
|
||||
<!-- ================================================================== -->
|
||||
<!-- same as package-all-src. it is just here for compatibility. -->
|
||||
<!-- ================================================================== -->
|
||||
<!-- -->
|
||||
<!-- ================================================================== -->
|
||||
<target name="dist-src" depends="package-all-src">
|
||||
</target>
|
||||
|
||||
<!-- ================================================================== -->
|
||||
<!-- C L E A N -->
|
||||
<!-- ================================================================== -->
|
||||
<!-- -->
|
||||
<!-- ================================================================== -->
|
||||
<target name="clean" depends="init">
|
||||
<delete dir="${build.dir}"/>
|
||||
<delete dir="${dist.dir}"/>
|
||||
<delete file="${basedir}/${final.name}.tar"/>
|
||||
<delete file="${basedir}/${final.name}.tar.gz"/>
|
||||
<delete file="${basedir}/${final.name}.zip"/>
|
||||
<delete dir="${dist.dir}-src"/>
|
||||
<delete file="${basedir}/${final.name}-src.tar"/>
|
||||
<delete file="${basedir}/${final.name}-src.tar.gz"/>
|
||||
<delete file="${basedir}/${final.name}-src.zip"/>
|
||||
</target>
|
||||
|
||||
|
||||
|
||||
<!-- ================================================================== -->
|
||||
<!-- Build the DocWeb app -->
|
||||
<!-- ================================================================== -->
|
||||
<target name="docweb-init">
|
||||
<!-- <mkdir dir="${}"/> -->
|
||||
</target>
|
||||
|
||||
<!--
|
||||
<target name="docweb-index" depends="javadocs,anttask-compile,docweb-init">
|
||||
<taskdef name="index"
|
||||
classname="org.apache.lucene.ant.IndexTask">
|
||||
<classpath>
|
||||
<pathelement location="${anttask.classes}"/>
|
||||
<path refid="classpath"/>
|
||||
</classpath>
|
||||
</taskdef>
|
||||
|
||||
<index index="${build.docweb}/index"
|
||||
overwrite="false">
|
||||
<fileset dir="${docs.dir}"/>
|
||||
<fileset dir="${build.javadocs}"/>
|
||||
</index>
|
||||
|
||||
</target>
|
||||
|
||||
<target name="docweb-war" depends="jar,jardemo,docweb-index">
|
||||
<war warfile="${build.docweb}/${build.docweb.war.name}.war"
|
||||
webxml="${demo.jsp}/WEB-INF/web.xml">
|
||||
<fileset dir="${demo.jsp}" excludes="WEB-INF/"/>
|
||||
<lib dir="${build.demo}" includes="*.jar"/>
|
||||
<lib dir="${build.dir}" includes="*.jar"/>
|
||||
<webinf dir="${build.docweb}" includes="index/"/>
|
||||
</war>
|
||||
</target>
|
||||
-->
|
||||
</project>
|
|
@ -0,0 +1,49 @@
|
|||
# ---------------------------------------------------------
|
||||
# S E A R C H B E A N B U I L D P R O P E R T I E S
|
||||
# ---------------------------------------------------------
|
||||
name=SearchBean
|
||||
Name=SearchBean
|
||||
version=.1
|
||||
year=2001-2002
|
||||
final.name=${name}-${version}
|
||||
debug=off
|
||||
|
||||
project.name = site
|
||||
docs.src = ./xdocs
|
||||
docs.dest = ./docs
|
||||
|
||||
src.dir = ./src/java
|
||||
demo.src = ./src/demo
|
||||
demo.jsp = ./src/jsp
|
||||
test.src = ./src/test
|
||||
docs.dir = ./docs
|
||||
lib.dir = ./lib
|
||||
dist.dir=${final.name}
|
||||
dist-src.dir = ${final.name}-src
|
||||
|
||||
# javadoc packages
|
||||
packages=org.apache.lucene.*
|
||||
|
||||
# javadoc link
|
||||
javadoc.link=http://java.sun.com/products/jdk/1.3/docs/api/
|
||||
|
||||
build.compiler.pedantic=false
|
||||
|
||||
build.dir = ./bin
|
||||
build.classes = ${build.dir}/classes
|
||||
build.lib = ${build.dir}/lib
|
||||
build.javadocs = ${build.dir}/docs/api
|
||||
build.src = ${build.dir}/src
|
||||
build.encoding = ISO-8859-1
|
||||
|
||||
build.anttask = ${build.dir}/anttask
|
||||
build.anttask.classes = ${build.anttask}/classes
|
||||
|
||||
|
||||
build.test = ${build.dir}/test
|
||||
build.test.src = ${build.test}/src
|
||||
build.test.classes = ${build.test}/classes
|
||||
|
||||
junit.src = ${basedir}/src/test
|
||||
junit.classes = ${build.dir}/unit-classes
|
||||
junit.reports = ${build.dir}/unit-reports
|
|
@ -0,0 +1,32 @@
|
|||
package org.apache.lucene.beans;
|
||||
|
||||
import org.apache.lucene.document.Document;
|
||||
import org.apache.lucene.document.DateField;
|
||||
import org.apache.lucene.beans.IndividualHit;
|
||||
import java.util.Date;
|
||||
|
||||
//import org.apache.log4j.Logger;
|
||||
|
||||
public class CompareDocumentsByField implements java.util.Comparator
|
||||
{
|
||||
// static private Logger logger = Logger.getLogger(CompareDocumentsByDate.class.getName());
|
||||
public CompareDocumentsByField()
|
||||
{
|
||||
}
|
||||
|
||||
public int compare(Object hit1, Object hit2)
|
||||
{
|
||||
String myDate1 = ((IndividualHit)hit1).getField();
|
||||
String myDate2 = ((IndividualHit)hit2).getField();
|
||||
if ((myDate1 == null) || (myDate2 == null)){
|
||||
//logger.error("A date was null, the score is "+((IndividualHit) hit1).getScore());
|
||||
//return -1;
|
||||
}
|
||||
return -1*(myDate1.compareTo(myDate2)); //sort in descending order
|
||||
}
|
||||
|
||||
public boolean equals(Object o1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,219 @@
|
|||
/*
|
||||
* HitsIterator.java
|
||||
* Provides an Iterator class around Lucene Hits
|
||||
* It also supports paging
|
||||
* Created on November 1, 2001, 8:53 PM
|
||||
*/
|
||||
|
||||
package org.apache.lucene.beans;
|
||||
|
||||
import org.apache.lucene.beans.SortedField;
|
||||
import org.apache.lucene.beans.CompareDocumentsByField;
|
||||
|
||||
import org.apache.lucene.document.Document;
|
||||
import org.apache.lucene.search.Hits;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
|
||||
//import org.apache.log4j.Logger;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Peter Carlson
|
||||
* @version 1.0
|
||||
*/
|
||||
public class HitsIterator {
|
||||
|
||||
//static Logger logger = Logger.getLogger(HitsIterator.class.getName());
|
||||
|
||||
private int currentPosition = 0;
|
||||
|
||||
private Hits hitsCollection = null;
|
||||
private Object[] arrayOfIndividualHits = null;
|
||||
|
||||
private int totalHits = 0;
|
||||
|
||||
private int pageSize = 25; // default page size
|
||||
|
||||
private int currentPage = 1; // range from 1 to totalHits%pageSize
|
||||
|
||||
private int totalPages = -1; // set by constructor
|
||||
|
||||
private int endPagePosition = 0; // position currentPage ends
|
||||
|
||||
/** Creates new HitsIterator */
|
||||
private HitsIterator() {
|
||||
}
|
||||
|
||||
public HitsIterator(Hits hits) throws IOException{
|
||||
this(hits,null);
|
||||
}
|
||||
|
||||
public HitsIterator(Hits hits, String sortFlag) throws IOException{
|
||||
this.hitsCollection = hits;
|
||||
if (sortFlag != null){
|
||||
if ((sortFlag != "") && (sortFlag !="relevance")){
|
||||
//logger.debug("Sorting hits by field "+sortFlag);
|
||||
sortByField(sortFlag);
|
||||
//logger.debug("Completed sorting by field "+sortFlag);
|
||||
}
|
||||
}
|
||||
totalHits = getTotalHits();
|
||||
setPageCount();
|
||||
}
|
||||
|
||||
/** sorts hits by the given sort flag
|
||||
* fills an interal array
|
||||
* @param sortFlag field to sort results on
|
||||
*/
|
||||
private void sortByField(String fieldName) throws IOException{
|
||||
long start = System.currentTimeMillis();
|
||||
Comparator c = null;
|
||||
if (fieldName == null){
|
||||
//logger.error("sort field is null");
|
||||
return;
|
||||
}
|
||||
|
||||
SortedField sf = SortedField.getSortedField(fieldName);
|
||||
if (sf !=null){
|
||||
c = (Comparator) new CompareDocumentsByField();
|
||||
} else {
|
||||
//logger.error("Sort field not found");
|
||||
arrayOfIndividualHits = null;
|
||||
return;
|
||||
}
|
||||
arrayOfIndividualHits = new Object[hitsCollection.length()];
|
||||
long first = System.currentTimeMillis();
|
||||
for (int i=0; i<hitsCollection.length(); i++) {
|
||||
int id = hitsCollection.id(i);
|
||||
arrayOfIndividualHits[i] = new IndividualHit(i, sf.getFieldValue(id), hitsCollection.score(i));
|
||||
}
|
||||
long second = System.currentTimeMillis();
|
||||
//logger.debug("HitsIterator.sortByField(): filling Obj[] took "+(second-first));
|
||||
|
||||
Arrays.sort(arrayOfIndividualHits, c);
|
||||
//logger.debug("HitsIterator.sortByField(): sort took "+(System.currentTimeMillis()-second));
|
||||
|
||||
}
|
||||
|
||||
|
||||
private void setPageCount() {
|
||||
|
||||
totalPages = totalHits / pageSize;
|
||||
|
||||
//account for remainder if not exaxtly divisable
|
||||
if (totalHits % pageSize != 0)
|
||||
{ totalPages++;}
|
||||
|
||||
setCurrentPage(1); // reset currentPage to make sure not over the limit
|
||||
}
|
||||
|
||||
public int getPageCount() {
|
||||
return totalPages;
|
||||
}
|
||||
|
||||
|
||||
public org.apache.lucene.document.Document setPosition(int position) throws IOException{
|
||||
if (position > totalHits) {
|
||||
return null;
|
||||
}
|
||||
currentPosition = position;
|
||||
return getDoc();
|
||||
}
|
||||
|
||||
public org.apache.lucene.document.Document next() throws IOException{
|
||||
currentPosition++;
|
||||
|
||||
if (currentPosition > totalHits) {
|
||||
currentPosition = totalHits;
|
||||
return null ;
|
||||
}
|
||||
|
||||
return getDoc();
|
||||
}
|
||||
|
||||
public org.apache.lucene.document.Document previous() throws IOException{
|
||||
currentPosition--;
|
||||
|
||||
if (currentPosition < 0)
|
||||
{ return null;}
|
||||
|
||||
return getDoc();
|
||||
}
|
||||
|
||||
public boolean hasNext() {
|
||||
if (currentPosition < endPagePosition)
|
||||
{ return true; }
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public org.apache.lucene.document.Document getDoc() throws IOException {
|
||||
if (arrayOfIndividualHits == null)
|
||||
return hitsCollection.doc(currentPosition - 1);
|
||||
else {
|
||||
int i = ((IndividualHit)arrayOfIndividualHits[currentPosition - 1]).getIndex();
|
||||
return hitsCollection.doc(i);
|
||||
}
|
||||
}
|
||||
|
||||
public int getScore() throws Exception{
|
||||
if (arrayOfIndividualHits == null)
|
||||
return (int) (hitsCollection.score(currentPosition - 1)*100.0f);
|
||||
else
|
||||
return (int) (((IndividualHit)arrayOfIndividualHits[currentPosition - 1]).getScore()*100.0f);
|
||||
}
|
||||
|
||||
public int getTotalHits() {
|
||||
return hitsCollection.length();
|
||||
}
|
||||
|
||||
public int getCurrentPosition() {
|
||||
return currentPosition;
|
||||
}
|
||||
|
||||
public int getPageSize() {
|
||||
return pageSize;
|
||||
}
|
||||
|
||||
public void setPageSize(int pageSize) {
|
||||
this.pageSize = pageSize;
|
||||
setPageCount();
|
||||
}
|
||||
|
||||
public void setCurrentPage(int currentPage) throws IndexOutOfBoundsException{
|
||||
if (currentPage > totalPages){
|
||||
throw new IndexOutOfBoundsException("currentPage greater than total pages");
|
||||
}
|
||||
|
||||
this.currentPage = currentPage;
|
||||
currentPosition = ((currentPage - 1) * pageSize);
|
||||
endPagePosition = Math.min( ((currentPage - 1)*pageSize) + pageSize, totalHits);
|
||||
}
|
||||
|
||||
public int getCurrentPage() {
|
||||
return currentPage;
|
||||
}
|
||||
|
||||
/**
|
||||
* set page number to next page, unless last page, then
|
||||
* always return last page number
|
||||
*@return current page number
|
||||
*/
|
||||
public int nextPage() {
|
||||
setCurrentPage(currentPage++);
|
||||
return getCurrentPage();
|
||||
}
|
||||
|
||||
/**
|
||||
* set page number to previous page, unless first page,
|
||||
* then always return first page number
|
||||
*@return current page number
|
||||
*/
|
||||
public int previousPage() {
|
||||
setCurrentPage(currentPage--);
|
||||
return getCurrentPage();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package org.apache.lucene.beans;
|
||||
|
||||
import org.apache.lucene.document.Document;
|
||||
|
||||
public class IndividualHit
|
||||
{
|
||||
private float score;
|
||||
private String field;
|
||||
private int index;
|
||||
|
||||
private IndividualHit()
|
||||
{
|
||||
}
|
||||
|
||||
public IndividualHit(int inIndex, String field, float inScore)
|
||||
{
|
||||
this.index = inIndex;
|
||||
this.field = field;
|
||||
this.score = inScore;
|
||||
}
|
||||
|
||||
public int getIndex()
|
||||
{
|
||||
return this.index;
|
||||
}
|
||||
|
||||
public String getField()
|
||||
{
|
||||
return this.field;
|
||||
}
|
||||
|
||||
public float getScore()
|
||||
{
|
||||
return this.score;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,261 @@
|
|||
/*
|
||||
* SearchBean.java
|
||||
*
|
||||
* Created on November 1, 2001, 10:31 AM
|
||||
*/
|
||||
|
||||
package org.apache.lucene.beans;
|
||||
|
||||
import org.apache.lucene.search.Searcher;
|
||||
import org.apache.lucene.search.IndexSearcher;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.apache.lucene.search.Hits;
|
||||
import org.apache.lucene.search.HitCollector;
|
||||
import org.apache.lucene.store.Directory;
|
||||
import org.apache.lucene.analysis.Analyzer;
|
||||
import org.apache.lucene.analysis.StopAnalyzer;
|
||||
import org.apache.lucene.analysis.standard.StandardAnalyzer;
|
||||
import org.apache.lucene.queryParser.QueryParser;
|
||||
import org.apache.lucene.queryParser.ParseException;
|
||||
|
||||
import java.util.Vector;
|
||||
|
||||
import org.apache.lucene.beans.HitsIterator;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
//import org.apache.log4j.Logger;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author peter carlson
|
||||
* @version 1.0
|
||||
*/
|
||||
public class SearchBean extends Object {
|
||||
|
||||
private String queryString = "";
|
||||
private String querySortField = "relevance"; // default
|
||||
private String queryType = "";
|
||||
private Directory directory;
|
||||
private HitsIterator hitsIterator = null;
|
||||
private String defaultSearchField = "text";
|
||||
private long searchTime = 0;
|
||||
// static Logger logger = Logger.getLogger(SearchBean.class.getName());
|
||||
// static Logger searchLogger = Logger.getLogger("searchLog");
|
||||
|
||||
private SearchBean(){
|
||||
}
|
||||
|
||||
/** Creates new SearchBean
|
||||
* @param path to index
|
||||
*/
|
||||
public SearchBean(Directory directory) {
|
||||
this.directory = directory;
|
||||
}
|
||||
|
||||
/** Creates new SearchBean
|
||||
* @param directory index
|
||||
* @param queryString string to search with
|
||||
*/
|
||||
public SearchBean(Directory directory, String queryString) {
|
||||
this(directory);
|
||||
setQueryString(queryString);
|
||||
}
|
||||
|
||||
/** Creates new SearchBean
|
||||
* @param directory index
|
||||
* @param queryString string to search with
|
||||
* @param querySortField field to sort on
|
||||
*/
|
||||
public SearchBean(Directory directory, String queryString, String querySortField) {
|
||||
this(directory);
|
||||
setQueryString(queryString);
|
||||
setQuerySortField(querySortField);
|
||||
}
|
||||
|
||||
/** Creates new SearchBean
|
||||
* @param directory index
|
||||
* @param queryString string to search with
|
||||
* @param querySortField field to sort on
|
||||
* @param queryType used to indicate which index and default Field
|
||||
*/
|
||||
public SearchBean(Directory directory, String queryString, String querySortField, String queryType){
|
||||
this(directory);
|
||||
setQueryString(queryString);
|
||||
setQuerySortField(querySortField);
|
||||
setQueryType(queryType);
|
||||
}
|
||||
|
||||
/** main search method
|
||||
*/
|
||||
public HitsIterator search() throws IOException, ParseException{
|
||||
return search(queryString,querySortField);
|
||||
}
|
||||
|
||||
/** main search method
|
||||
* @param queryString string to search with
|
||||
*/
|
||||
public HitsIterator search(String queryString) throws IOException, ParseException{
|
||||
return search(queryString,queryString);
|
||||
}
|
||||
|
||||
/** main search method
|
||||
* @param queryString string to search with
|
||||
* @param querySortField field to sort on
|
||||
*/
|
||||
public HitsIterator search(String queryString, String querySortField) throws IOException, ParseException{
|
||||
return search(queryString, querySortField, queryType);
|
||||
}
|
||||
|
||||
/** main search method
|
||||
* @param queryString string to search with
|
||||
* @param querySortField field to sort on
|
||||
* @param queryType used to indicate the index to search
|
||||
*/
|
||||
public HitsIterator search(String queryString, String querySortField, String queryType) throws IOException, ParseException {
|
||||
long startTime = System.currentTimeMillis();
|
||||
Hits hits = searchHits(queryString, queryType);
|
||||
|
||||
if (hits == null) {return null;}
|
||||
if (hits.length() == 0) {return null;}
|
||||
|
||||
HitsIterator hi = new HitsIterator(hits, querySortField);
|
||||
long endTime = System.currentTimeMillis();
|
||||
setSearchTime(endTime - startTime);
|
||||
setHitsIterator(hi);
|
||||
//searchLogger.info("queryString = "+queryString + "sort field = "+ querySortField +" #results = "+hits.length());
|
||||
return hi;
|
||||
}
|
||||
|
||||
/** does the actual searching
|
||||
*/
|
||||
private Hits searchHits(String queryString, String queryType) throws IOException, ParseException{
|
||||
if (queryString == "") {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Provide for multiple indices in the future
|
||||
|
||||
Searcher searcher = new IndexSearcher(directory);
|
||||
Query query = getQuery(queryString, defaultSearchField);
|
||||
//System.out.println("###querystring= "+query.toString(defaultSearchField));
|
||||
Hits hits = searcher.search(query);
|
||||
//System.out.println("Number hits = "+hits.length());
|
||||
//logger.debug("queryString = "+query.toString(searchField)+" hits = "+hits.length()+" queryType = "+queryType+" indexPath = "+indexPath );
|
||||
return hits;
|
||||
}
|
||||
|
||||
/** <queryString> | <queryType> | <querySortField>
|
||||
*/
|
||||
public String toString(){
|
||||
return queryString+"|"+queryType+"|"+querySortField;
|
||||
}
|
||||
|
||||
/** setter for queryString
|
||||
*/
|
||||
public void setQueryString
|
||||
(String queryString) {
|
||||
this.queryString = queryString;
|
||||
}
|
||||
|
||||
/** getter for queryString
|
||||
*/
|
||||
public String getQueryString(){
|
||||
return queryString;
|
||||
}
|
||||
|
||||
/** getter for Lucene Query
|
||||
*/
|
||||
private Query getQuery(String queryString, String searchField) throws ParseException {
|
||||
//String defaultSearchField = "text";
|
||||
Analyzer analyzer = new StandardAnalyzer();
|
||||
Query query = QueryParser.parse(queryString, searchField, analyzer);
|
||||
//System.out.println(query.toString());
|
||||
return query;
|
||||
}
|
||||
|
||||
/** Getter for property defaulSearchField.
|
||||
* @return Value of property defaulSearchField.
|
||||
*/
|
||||
public String getDefaultSearchField() {
|
||||
return defaultSearchField;
|
||||
}
|
||||
|
||||
/** Setter for property defaulSearchField.
|
||||
* @param defaulSearchField New value of property defaulSearchField.
|
||||
*/
|
||||
public void setDefaultSearchField(java.lang.String defaultSearchField) {
|
||||
this.defaultSearchField = defaultSearchField;
|
||||
}
|
||||
|
||||
/** Getter for property searchTime.
|
||||
* @return Value of property searchTime.
|
||||
*/
|
||||
public long getSearchTime() {
|
||||
return searchTime;
|
||||
}
|
||||
|
||||
/** Setter for property searchTime.
|
||||
* @param searchTime New value of property searchTime.
|
||||
*/
|
||||
public void setSearchTime(long searchTime) {
|
||||
this.searchTime = searchTime;
|
||||
}
|
||||
|
||||
/** Getter for property querySortField.
|
||||
* @return Value of property querySortField.
|
||||
*/
|
||||
public java.lang.String getQuerySortField() {
|
||||
return querySortField;
|
||||
}
|
||||
|
||||
/** Setter for property querySortField.
|
||||
* @param querySortField New value of property querySortField.
|
||||
*/
|
||||
public void setQuerySortField(String querySortField) {
|
||||
this.querySortField = querySortField;
|
||||
}
|
||||
|
||||
/** Getter for property hitsIterator.
|
||||
* @return Value of property hitsIterator.
|
||||
*/
|
||||
public HitsIterator getHitsIterator() {
|
||||
return hitsIterator;
|
||||
}
|
||||
|
||||
/** Setter for property hitsIterator.
|
||||
* @param hitsIterator New value of property hitsIterator.
|
||||
*/
|
||||
public void setHitsIterator(HitsIterator hitsIterator) {
|
||||
this.hitsIterator = hitsIterator;
|
||||
}
|
||||
|
||||
/** Getter for property queryType.
|
||||
* @return Value of property queryType.
|
||||
*/
|
||||
public java.lang.String getQueryType() {
|
||||
return queryType;
|
||||
}
|
||||
|
||||
/** Setter for property queryType.
|
||||
* @param queryType New value of property queryType.
|
||||
*/
|
||||
public void setQueryType(java.lang.String queryType) {
|
||||
this.queryType = queryType;
|
||||
}
|
||||
|
||||
/** Getter for property directory.
|
||||
* @return Value of property directory.
|
||||
*/
|
||||
public org.apache.lucene.store.Directory getDirectory() {
|
||||
return directory;
|
||||
}
|
||||
|
||||
/** Setter for property directory.
|
||||
* @param directory New value of property directory.
|
||||
*/
|
||||
public void setDirectory(org.apache.lucene.store.Directory directory) {
|
||||
this.directory = directory;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,103 @@
|
|||
/*
|
||||
* SortedField.java
|
||||
*
|
||||
* Created on May 20, 2002, 4:15 PM
|
||||
*/
|
||||
|
||||
package org.apache.lucene.beans;
|
||||
|
||||
|
||||
import org.apache.lucene.index.IndexReader;
|
||||
import org.apache.lucene.store.Directory;
|
||||
|
||||
import java.util.Hashtable;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author carlson
|
||||
*/
|
||||
public class SortedField {
|
||||
|
||||
private String fieldName;
|
||||
|
||||
private String[] fieldValues;
|
||||
|
||||
private static Hashtable fieldList = new Hashtable(); //keeps track of all fields
|
||||
|
||||
/** Creates a new instance of SortedField */
|
||||
public SortedField() {
|
||||
}
|
||||
|
||||
/** add a field so that is can be used to sort
|
||||
* @param fieldName the name of the field to add
|
||||
* @param indexPath path to Lucene index directory
|
||||
*/
|
||||
public static void addField(String fieldName, String indexPath) throws IOException{
|
||||
IndexReader ir = IndexReader.open(indexPath);
|
||||
addField(fieldName, ir);
|
||||
}
|
||||
|
||||
/** add a field so that is can be used to sort
|
||||
* @param fieldName the name of the field to add
|
||||
* @param indexFile File pointing to Lucene index directory
|
||||
*/
|
||||
public static void addField(String fieldName, File indexFile) throws IOException{
|
||||
IndexReader ir = IndexReader.open(indexFile);
|
||||
addField(fieldName, ir);
|
||||
}
|
||||
|
||||
|
||||
/** add a field so that is can be used to sort
|
||||
* @param fieldName the name of the field to add
|
||||
* @param directory Lucene Directory
|
||||
*/
|
||||
public static void addField(String fieldName, Directory directory) throws IOException{
|
||||
IndexReader ir = IndexReader.open(directory);
|
||||
addField(fieldName, ir);
|
||||
}
|
||||
|
||||
private static void addField(String fieldName, IndexReader ir) throws IOException{
|
||||
SortedField sortedField = new SortedField();
|
||||
sortedField.addSortedField(fieldName,ir);
|
||||
//long start = System.currentTimeMillis();
|
||||
fieldList.put(fieldName, sortedField);
|
||||
//logger.info("adding data from field "+fieldName+" took "+(System.currentTimeMillis()-start));
|
||||
}
|
||||
|
||||
/** adds the data from the index into a string array
|
||||
*/
|
||||
private void addSortedField(String fieldName, IndexReader ir) throws IOException{
|
||||
int numDocs = ir.numDocs();
|
||||
fieldValues = new String[numDocs];
|
||||
for (int i=0; i<numDocs; i++) {
|
||||
fieldValues[i] = ir.document(i).get(fieldName);
|
||||
}
|
||||
ir.close();
|
||||
}
|
||||
|
||||
/** returns the value of the field
|
||||
* @param globalID Lucene's global document ID
|
||||
* @return value of field
|
||||
*/
|
||||
public String getFieldValue(int globalID) {
|
||||
return fieldValues[globalID];
|
||||
}
|
||||
|
||||
/** provides way to retrieve a SortedField once you add it
|
||||
* @param fieldName name of field to lookup
|
||||
* @return SortedField field to use when sorting
|
||||
*/
|
||||
public static SortedField getSortedField(String fieldName){
|
||||
return (SortedField) fieldList.get(fieldName);
|
||||
}
|
||||
|
||||
/** Getter for property fieldName.
|
||||
* @return Value of property fieldName.
|
||||
*/
|
||||
public java.lang.String getFieldName() {
|
||||
return fieldName;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue