mirror of https://github.com/apache/lucene.git
Now if there are no hits, I return a HitsIterator that will return 0 for all appropriate fields instead of returning null
I put the string "relevance" into a static variable. git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@150810 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
0a7e5168e5
commit
18d1f6e5df
|
@ -54,7 +54,7 @@ public class HitsIterator {
|
||||||
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 !="relevance")){
|
if ((sortFlag != "") && (sortFlag.equals(SearchBean.SORT_FIELD_RELEVANCE))){
|
||||||
//logger.debug("Sorting hits by field "+sortFlag);
|
//logger.debug("Sorting hits by field "+sortFlag);
|
||||||
sortByField(sortFlag);
|
sortByField(sortFlag);
|
||||||
//logger.debug("Completed sorting by field "+sortFlag);
|
//logger.debug("Completed sorting by field "+sortFlag);
|
||||||
|
@ -81,6 +81,8 @@ public class HitsIterator {
|
||||||
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
|
||||||
|
// Should I throw an exception here?
|
||||||
arrayOfIndividualHits = null;
|
arrayOfIndividualHits = null;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -100,15 +102,18 @@ public class HitsIterator {
|
||||||
|
|
||||||
|
|
||||||
private void setPageCount() {
|
private void setPageCount() {
|
||||||
|
if (totalHits == 0){
|
||||||
|
totalPages = 0;
|
||||||
|
setCurrentPage(0);
|
||||||
|
} 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;
|
||||||
|
@ -151,6 +156,7 @@ public class HitsIterator {
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
if (arrayOfIndividualHits == null)
|
if (arrayOfIndividualHits == null)
|
||||||
return hitsCollection.doc(currentPosition - 1);
|
return hitsCollection.doc(currentPosition - 1);
|
||||||
else {
|
else {
|
||||||
|
@ -160,6 +166,7 @@ public class HitsIterator {
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getScore() throws Exception{
|
public int getScore() throws Exception{
|
||||||
|
// Determine if using relevnace or sorting by another field
|
||||||
if (arrayOfIndividualHits == null)
|
if (arrayOfIndividualHits == null)
|
||||||
return (int) (hitsCollection.score(currentPosition - 1)*100.0f);
|
return (int) (hitsCollection.score(currentPosition - 1)*100.0f);
|
||||||
else
|
else
|
||||||
|
@ -185,7 +192,8 @@ public class HitsIterator {
|
||||||
|
|
||||||
public void setCurrentPage(int currentPage) throws IndexOutOfBoundsException{
|
public void setCurrentPage(int currentPage) throws IndexOutOfBoundsException{
|
||||||
if (currentPage > totalPages){
|
if (currentPage > totalPages){
|
||||||
throw new IndexOutOfBoundsException("currentPage greater than total pages");
|
currentPage = totalPages; // don't allow to go over max
|
||||||
|
//throw new IndexOutOfBoundsException("currentPage greater than total pages");
|
||||||
}
|
}
|
||||||
|
|
||||||
this.currentPage = currentPage;
|
this.currentPage = currentPage;
|
||||||
|
|
Loading…
Reference in New Issue