my changes to make it compile, also minor build file cleanup

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@150887 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Erik Hatcher 2003-05-12 14:32:23 +00:00
parent 620516c21c
commit f667465102
4 changed files with 95 additions and 90 deletions

View File

@ -6,15 +6,14 @@
(and without typing -D each time it compiles it --> (and without typing -D each time it compiles it -->
<property file="${user.home}/lucene.build.properties" /> <property file="${user.home}/lucene.build.properties" />
<property file="${user.home}/build.properties" /> <property file="${user.home}/build.properties" />
<property file="${basedir}/build.properties" /> <property file="build.properties" />
<property file="${basedir}/default.properties" /> <property file="default.properties" />
<property name="lib.dir" location="lib"/>
<!-- Build classpath --> <!-- Build classpath -->
<path id="classpath"> <path id="classpath">
<pathelement location="${build.classes}"/>
<pathelement location="${build.test.classes}"/>
<pathelement location="."/> <pathelement location="."/>
<fileset dir="lib.dir"> <fileset dir="${lib.dir}">
<include name="*.jar" /> <include name="*.jar" />
</fileset> </fileset>
</path> </path>
@ -22,9 +21,8 @@
<path id="junit.classpath"> <path id="junit.classpath">
<pathelement location="${junit.classes}" /> <pathelement location="${junit.classes}" />
<pathelement location="${build.classes}"/> <pathelement location="${build.classes}"/>
<fileset dir="lib.dir"> <pathelement location="${build.test.classes}"/>
<include name="*.jar" /> <path refid="classpath"/>
</fileset>
<pathelement path="${java.class.path}" /> <pathelement path="${java.class.path}" />
</path> </path>
@ -35,7 +33,6 @@
<mkdir dir="${build.dir}"/> <mkdir dir="${build.dir}"/>
<mkdir dir="${build.classes}"/> <mkdir dir="${build.classes}"/>
<mkdir dir="${build.src}"/> <mkdir dir="${build.src}"/>
<mkdir dir="lib.dir"/>
<available <available
property="junit.present" property="junit.present"
@ -109,7 +106,7 @@
includes="**/*.java" includes="**/*.java"
destdir="${build.test.classes}" destdir="${build.test.classes}"
debug="${debug}"> debug="${debug}">
<classpath refid="classpath"/> <classpath refid="junit.classpath"/>
</javac> </javac>
</target> </target>
@ -133,7 +130,7 @@
includes="**/*.java" includes="**/*.java"
destdir="${junit.classes}" destdir="${junit.classes}"
debug="${debug}"> debug="${debug}">
<classpath refid="classpath"/> <classpath refid="junit.classpath"/>
</javac> </javac>
<junit printsummary="yes" haltonfailure="no" > <junit printsummary="yes" haltonfailure="no" >

View File

@ -25,37 +25,37 @@ import java.util.Comparator;
* @version 1.0 * @version 1.0
*/ */
public class HitsIterator { public class HitsIterator {
//static Logger logger = Logger.getLogger(HitsIterator.class.getName()); //static Logger logger = Logger.getLogger(HitsIterator.class.getName());
private int currentPosition = 0; private int currentPosition = 0;
private Hits hitsCollection = null; private Hits hitsCollection = null;
private Object[] arrayOfIndividualHits = null; private Object[] arrayOfIndividualHits = null;
private int totalHits = 0; private int totalHits = 0;
private int pageSize = 25; // default page size private int pageSize = 25; // default page size
private int currentPage = 1; // range from 1 to totalHits%pageSize private int currentPage = 1; // range from 1 to totalHits%pageSize
private int totalPages = -1; // set by constructor private int totalPages = -1; // set by constructor
private int endPagePosition = 0; // position currentPage ends private int endPagePosition = 0; // position currentPage ends
/** Creates new HitsIterator */ /** Creates new HitsIterator */
private HitsIterator() { private HitsIterator() {
} }
public HitsIterator(Hits hits) throws IOException{ public HitsIterator(Hits hits) throws IOException{
this(hits,null); this(hits,null);
} }
public HitsIterator(Hits hits, String sortFlag) throws IOException{ public HitsIterator(Hits hits, String sortFlag) throws IOException{
this.hitsCollection = hits; this.hitsCollection = hits;
if (sortFlag != null){ if (sortFlag != null){
if ((sortFlag != "") && (sortFlag.equals(SearchBean.SORT_FIELD_RELEVANCE))){ if (sortFlag != "") {
//logger.debug("Sorting hits by field "+sortFlag); System.out.println("Sorting hits by field "+sortFlag);
sortByField(sortFlag); sortByField(sortFlag);
//logger.debug("Completed sorting by field "+sortFlag); //logger.debug("Completed sorting by field "+sortFlag);
} }
@ -63,7 +63,7 @@ public class HitsIterator {
totalHits = getTotalHits(); totalHits = getTotalHits();
setPageCount(); setPageCount();
} }
/** sorts hits by the given sort flag /** sorts hits by the given sort flag
* fills an interal array * fills an interal array
* @param sortFlag field to sort results on * @param sortFlag field to sort results on
@ -75,12 +75,12 @@ public class HitsIterator {
//logger.error("sort field is null"); //logger.error("sort field is null");
return; return;
} }
SortedField sf = SortedField.getSortedField(fieldName); SortedField sf = SortedField.getSortedField(fieldName);
if (sf !=null){ if (sf !=null){
c = (Comparator) new CompareDocumentsByField(); c = (Comparator) new CompareDocumentsByField();
} else { } else {
//logger.error("Sort field not found"); //logger.error("Sort field not found");
// use default sort of Lucene -- Relevance // use default sort of Lucene -- Relevance
// Should I throw an exception here? // Should I throw an exception here?
arrayOfIndividualHits = null; arrayOfIndividualHits = null;
@ -94,32 +94,32 @@ public class HitsIterator {
} }
long second = System.currentTimeMillis(); long second = System.currentTimeMillis();
//logger.debug("HitsIterator.sortByField(): filling Obj[] took "+(second-first)); //logger.debug("HitsIterator.sortByField(): filling Obj[] took "+(second-first));
Arrays.sort(arrayOfIndividualHits, c); Arrays.sort(arrayOfIndividualHits, c);
//logger.debug("HitsIterator.sortByField(): sort took "+(System.currentTimeMillis()-second)); //logger.debug("HitsIterator.sortByField(): sort took "+(System.currentTimeMillis()-second));
} }
private void setPageCount() { private void setPageCount() {
if (totalHits == 0){ if (totalHits == 0){
totalPages = 0; totalPages = 0;
setCurrentPage(0); setCurrentPage(0);
} else { } else {
totalPages = totalHits / pageSize; totalPages = totalHits / pageSize;
//account for remainder if not exaxtly divisable //account for remainder if not exaxtly divisable
if (totalHits % pageSize != 0) if (totalHits % pageSize != 0)
{ totalPages++;} { totalPages++;}
setCurrentPage(1); // reset currentPage to make sure not over the limit setCurrentPage(1); // reset currentPage to make sure not over the limit
} }
} }
public int getPageCount() { public int getPageCount() {
return totalPages; return totalPages;
} }
public org.apache.lucene.document.Document setPosition(int position) throws IOException{ public org.apache.lucene.document.Document setPosition(int position) throws IOException{
if (position > totalHits) { if (position > totalHits) {
return null; return null;
@ -127,34 +127,34 @@ public class HitsIterator {
currentPosition = position; currentPosition = position;
return getDoc(); return getDoc();
} }
public org.apache.lucene.document.Document next() throws IOException{ public org.apache.lucene.document.Document next() throws IOException{
currentPosition++; currentPosition++;
if (currentPosition > totalHits) { if (currentPosition > totalHits) {
currentPosition = totalHits; currentPosition = totalHits;
return null ; return null ;
} }
return getDoc(); return getDoc();
} }
public org.apache.lucene.document.Document previous() throws IOException{ public org.apache.lucene.document.Document previous() throws IOException{
currentPosition--; currentPosition--;
if (currentPosition < 0) if (currentPosition < 0)
{ return null;} { return null;}
return getDoc(); return getDoc();
} }
public boolean hasNext() { public boolean hasNext() {
if (currentPosition < endPagePosition) if (currentPosition < endPagePosition)
{ return true; } { return true; }
return false; return false;
} }
public org.apache.lucene.document.Document getDoc() throws IOException { public org.apache.lucene.document.Document getDoc() throws IOException {
// Determine if using relevnace or sorting by another field // Determine if using relevnace or sorting by another field
if (arrayOfIndividualHits == null) if (arrayOfIndividualHits == null)
@ -164,7 +164,7 @@ public class HitsIterator {
return hitsCollection.doc(i); return hitsCollection.doc(i);
} }
} }
public int getScore() throws Exception{ public int getScore() throws Exception{
// Determine if using relevnace or sorting by another field // Determine if using relevnace or sorting by another field
if (arrayOfIndividualHits == null) if (arrayOfIndividualHits == null)
@ -172,39 +172,39 @@ public class HitsIterator {
else else
return (int) (((IndividualHit)arrayOfIndividualHits[currentPosition - 1]).getScore()*100.0f); return (int) (((IndividualHit)arrayOfIndividualHits[currentPosition - 1]).getScore()*100.0f);
} }
public int getTotalHits() { public int getTotalHits() {
return hitsCollection.length(); return hitsCollection.length();
} }
public int getCurrentPosition() { public int getCurrentPosition() {
return currentPosition; return currentPosition;
} }
public int getPageSize() { public int getPageSize() {
return pageSize; return pageSize;
} }
public void setPageSize(int pageSize) { public void setPageSize(int pageSize) {
this.pageSize = pageSize; this.pageSize = pageSize;
setPageCount(); setPageCount();
} }
public void setCurrentPage(int currentPage) throws IndexOutOfBoundsException{ public void setCurrentPage(int currentPage) throws IndexOutOfBoundsException{
if (currentPage > totalPages){ if (currentPage > totalPages){
currentPage = totalPages; // don't allow to go over max currentPage = totalPages; // don't allow to go over max
//throw new IndexOutOfBoundsException("currentPage greater than total pages"); //throw new IndexOutOfBoundsException("currentPage greater than total pages");
} }
this.currentPage = currentPage; this.currentPage = currentPage;
currentPosition = ((currentPage - 1) * pageSize); currentPosition = ((currentPage - 1) * pageSize);
endPagePosition = Math.min( ((currentPage - 1)*pageSize) + pageSize, totalHits); endPagePosition = Math.min( ((currentPage - 1)*pageSize) + pageSize, totalHits);
} }
public int getCurrentPage() { public int getCurrentPage() {
return currentPage; return currentPage;
} }
/** /**
* set page number to next page, unless last page, then * set page number to next page, unless last page, then
* always return last page number * always return last page number
@ -214,7 +214,7 @@ public class HitsIterator {
setCurrentPage(currentPage++); setCurrentPage(currentPage++);
return getCurrentPage(); return getCurrentPage();
} }
/** /**
* set page number to previous page, unless first page, * set page number to previous page, unless first page,
* then always return first page number * then always return first page number

View File

@ -1,6 +1,7 @@
package org.apache.lucene.beans; package org.apache.lucene.beans;
import java.util.Iterator; import java.util.Iterator;
import java.io.IOException;
/** /**
* Acts as an adapter for HitsIterator to comply with the Collections * Acts as an adapter for HitsIterator to comply with the Collections
@ -25,7 +26,13 @@ public final class IteratorAdapter implements Iterator
public Object next() public Object next()
{ {
return hitsIterator.next(); Object obj = null;
try {
obj = hitsIterator.next();
} catch (IOException e) {
// ignore for now, returning null might be good enough?
}
return obj;
} }
public void remove() public void remove()

View File

@ -32,7 +32,7 @@ import java.io.IOException;
* @version 1.0 * @version 1.0
*/ */
public class SearchBean extends Object { public class SearchBean extends Object {
static final String SORT_FIELD_RELEVANCE = "relevance"; static final String SORT_FIELD_RELEVANCE = "relevance";
private String queryString = ""; private String queryString = "";
private String querySortField = SORT_FIELD_RELEVANCE; // default private String querySortField = SORT_FIELD_RELEVANCE; // default
@ -44,17 +44,17 @@ public class SearchBean extends Object {
private Searcher searcher = null; private Searcher searcher = null;
// static Logger logger = Logger.getLogger(SearchBean.class.getName()); // static Logger logger = Logger.getLogger(SearchBean.class.getName());
// static Logger searchLogger = Logger.getLogger("searchLog"); // static Logger searchLogger = Logger.getLogger("searchLog");
private SearchBean(){ private SearchBean(){
} }
/** Creates new SearchBean /** Creates new SearchBean
* @param path to index * @param path to index
*/ */
public SearchBean(Directory directory) { public SearchBean(Directory directory) {
this.directory = directory; this.directory = directory;
} }
/** Creates new SearchBean /** Creates new SearchBean
* @param directory index * @param directory index
* @param queryString string to search with * @param queryString string to search with
@ -63,7 +63,7 @@ public class SearchBean extends Object {
this(directory); this(directory);
setQueryString(queryString); setQueryString(queryString);
} }
/** Creates new SearchBean /** Creates new SearchBean
* @param directory index * @param directory index
* @param queryString string to search with * @param queryString string to search with
@ -74,7 +74,7 @@ public class SearchBean extends Object {
setQueryString(queryString); setQueryString(queryString);
setQuerySortField(querySortField); setQuerySortField(querySortField);
} }
/** Creates new SearchBean /** Creates new SearchBean
* @param directory index * @param directory index
* @param queryString string to search with * @param queryString string to search with
@ -87,20 +87,20 @@ public class SearchBean extends Object {
setQuerySortField(querySortField); setQuerySortField(querySortField);
setQueryType(queryType); setQueryType(queryType);
} }
/** main search method /** main search method
*/ */
public HitsIterator search() throws IOException, ParseException{ public HitsIterator search() throws IOException, ParseException{
return search(queryString,querySortField); return search(queryString,querySortField);
} }
/** main search method /** main search method
* @param queryString string to search with * @param queryString string to search with
*/ */
public HitsIterator search(String queryString) throws IOException, ParseException{ public HitsIterator search(String queryString) throws IOException, ParseException{
return search(queryString,queryString); return search(queryString,queryString);
} }
/** main search method /** main search method
* @param queryString string to search with * @param queryString string to search with
* @param querySortField field to sort on * @param querySortField field to sort on
@ -108,7 +108,7 @@ public class SearchBean extends Object {
public HitsIterator search(String queryString, String querySortField) throws IOException, ParseException{ public HitsIterator search(String queryString, String querySortField) throws IOException, ParseException{
return search(queryString, querySortField, queryType); return search(queryString, querySortField, queryType);
} }
/** main search method /** main search method
* @param queryString string to search with * @param queryString string to search with
* @param querySortField field to sort on * @param querySortField field to sort on
@ -117,10 +117,10 @@ public class SearchBean extends Object {
public HitsIterator search(String queryString, String querySortField, String queryType) throws IOException, ParseException { public HitsIterator search(String queryString, String querySortField, String queryType) throws IOException, ParseException {
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
Hits hits = searchHits(queryString, queryType); Hits hits = searchHits(queryString, queryType);
//if (hits == null) {return null;} //if (hits == null) {return null;}
//if (hits.length() == 0) {return null;} //if (hits.length() == 0) {return null;}
HitsIterator hi = new HitsIterator(hits, querySortField); HitsIterator hi = new HitsIterator(hits, querySortField);
long endTime = System.currentTimeMillis(); long endTime = System.currentTimeMillis();
setSearchTime(endTime - startTime); setSearchTime(endTime - startTime);
@ -128,51 +128,52 @@ public class SearchBean extends Object {
//searchLogger.info("queryString = "+queryString + "sort field = "+ querySortField +" #results = "+hits.length()); //searchLogger.info("queryString = "+queryString + "sort field = "+ querySortField +" #results = "+hits.length());
return hi; return hi;
} }
/** does the actual searching /** does the actual searching
*/ */
private Hits searchHits(String queryString, String queryType) throws IOException, ParseException{ private Hits searchHits(String queryString, String queryType) throws IOException, ParseException{
System.out.println("queryString = " + queryString);
if (queryString == "") { if (queryString == "") {
return null; return null;
} }
// Provide for multiple indices in the future // Provide for multiple indices in the future
searcher = new IndexSearcher(directory); searcher = new IndexSearcher(directory);
Query query = getQuery(queryString, defaultSearchField); Query query = getQuery(queryString, defaultSearchField);
//System.out.println("###querystring= "+query.toString(defaultSearchField)); System.out.println("###querystring= "+query.toString(defaultSearchField));
Hits hits = searcher.search(query); Hits hits = searcher.search(query);
//System.out.println("Number hits = "+hits.length()); //System.out.println("Number hits = "+hits.length());
//logger.debug("queryString = "+query.toString(searchField)+" hits = "+hits.length()+" queryType = "+queryType+" indexPath = "+indexPath ); //logger.debug("queryString = "+query.toString(searchField)+" hits = "+hits.length()+" queryType = "+queryType+" indexPath = "+indexPath );
return hits; return hits;
} }
/** /**
* frees resources associated with SearchBean search * frees resources associated with SearchBean search
*/ */
public void close() throws IOException{ public void close() throws IOException{
searcher.close(); searcher.close();
} }
/** <queryString> | <queryType> | <querySortField> /** <queryString> | <queryType> | <querySortField>
*/ */
public String toString(){ public String toString(){
return queryString+"|"+queryType+"|"+querySortField; return queryString+"|"+queryType+"|"+querySortField;
} }
/** setter for queryString /** setter for queryString
*/ */
public void setQueryString public void setQueryString
(String queryString) { (String queryString) {
this.queryString = queryString; this.queryString = queryString;
} }
/** getter for queryString /** getter for queryString
*/ */
public String getQueryString(){ public String getQueryString(){
return queryString; return queryString;
} }
/** getter for Lucene Query /** getter for Lucene Query
*/ */
private Query getQuery(String queryString, String searchField) throws ParseException { private Query getQuery(String queryString, String searchField) throws ParseException {
@ -182,89 +183,89 @@ public class SearchBean extends Object {
//System.out.println(query.toString()); //System.out.println(query.toString());
return query; return query;
} }
/** Getter for property defaulSearchField. /** Getter for property defaulSearchField.
* @return Value of property defaulSearchField. * @return Value of property defaulSearchField.
*/ */
public String getDefaultSearchField() { public String getDefaultSearchField() {
return defaultSearchField; return defaultSearchField;
} }
/** Setter for property defaulSearchField. /** Setter for property defaulSearchField.
* @param defaulSearchField New value of property defaulSearchField. * @param defaulSearchField New value of property defaulSearchField.
*/ */
public void setDefaultSearchField(java.lang.String defaultSearchField) { public void setDefaultSearchField(java.lang.String defaultSearchField) {
this.defaultSearchField = defaultSearchField; this.defaultSearchField = defaultSearchField;
} }
/** Getter for property searchTime. /** Getter for property searchTime.
* @return Value of property searchTime. * @return Value of property searchTime.
*/ */
public long getSearchTime() { public long getSearchTime() {
return searchTime; return searchTime;
} }
/** Setter for property searchTime. /** Setter for property searchTime.
* @param searchTime New value of property searchTime. * @param searchTime New value of property searchTime.
*/ */
public void setSearchTime(long searchTime) { public void setSearchTime(long searchTime) {
this.searchTime = searchTime; this.searchTime = searchTime;
} }
/** Getter for property querySortField. /** Getter for property querySortField.
* @return Value of property querySortField. * @return Value of property querySortField.
*/ */
public java.lang.String getQuerySortField() { public java.lang.String getQuerySortField() {
return querySortField; return querySortField;
} }
/** Setter for property querySortField. /** Setter for property querySortField.
* @param querySortField New value of property querySortField. * @param querySortField New value of property querySortField.
*/ */
public void setQuerySortField(String querySortField) { public void setQuerySortField(String querySortField) {
this.querySortField = querySortField; this.querySortField = querySortField;
} }
/** Getter for property hitsIterator. /** Getter for property hitsIterator.
* @return Value of property hitsIterator. * @return Value of property hitsIterator.
*/ */
public HitsIterator getHitsIterator() { public HitsIterator getHitsIterator() {
return hitsIterator; return hitsIterator;
} }
/** Setter for property hitsIterator. /** Setter for property hitsIterator.
* @param hitsIterator New value of property hitsIterator. * @param hitsIterator New value of property hitsIterator.
*/ */
public void setHitsIterator(HitsIterator hitsIterator) { public void setHitsIterator(HitsIterator hitsIterator) {
this.hitsIterator = hitsIterator; this.hitsIterator = hitsIterator;
} }
/** Getter for property queryType. /** Getter for property queryType.
* @return Value of property queryType. * @return Value of property queryType.
*/ */
public java.lang.String getQueryType() { public java.lang.String getQueryType() {
return queryType; return queryType;
} }
/** Setter for property queryType. /** Setter for property queryType.
* @param queryType New value of property queryType. * @param queryType New value of property queryType.
*/ */
public void setQueryType(java.lang.String queryType) { public void setQueryType(java.lang.String queryType) {
this.queryType = queryType; this.queryType = queryType;
} }
/** Getter for property directory. /** Getter for property directory.
* @return Value of property directory. * @return Value of property directory.
*/ */
public org.apache.lucene.store.Directory getDirectory() { public org.apache.lucene.store.Directory getDirectory() {
return directory; return directory;
} }
/** Setter for property directory. /** Setter for property directory.
* @param directory New value of property directory. * @param directory New value of property directory.
*/ */
public void setDirectory(org.apache.lucene.store.Directory directory) { public void setDirectory(org.apache.lucene.store.Directory directory) {
this.directory = directory; this.directory = directory;
} }
} }