I put SearchBean up in contributions to provide for a simple example of sorting.
I will go through and add your suggestions as it seems to be used by some people in their systems.
If you have any other items you think should be changed I'd be up for your suggestions.
Thanks for the code Terry.
--Peter
On Monday, January 20, 2003, at 07:32 AM, Terry Steichen wrote:
Erik,
I got a bit out of sync with the CVS build (because it took a while to
integrate the changes), so I can't quickly do a simple diff. Instead, I'm
including the changed methods in HitsIterator in this message verbatim. Not
real pretty code, I know, but it does work just fine.
Regards and HTH,
Terry
Note: direction can be 'r', 'a' or 'd' (default)
public HitsIterator(Hits hits, String sortField, String direction) throws
IOException{
this.hitsCollection = hits;
if (sortField != null){
if ((direction != null) && (!direction.equals("r"))){
sortByField(sortField, direction);
}
}
totalHits = getTotalHits();
setPageCount();
}
private void sortByField(String fieldName, String direction) throws
IOException{
long start = System.currentTimeMillis();
Comparator c = null;
if (fieldName == null){
return;
}
SortedField sf = SortedField.getSortedField(fieldName);
int dirflag = -1;
if(direction.equals("a")) {
dirflag = 1;
} else if(direction.equals("r")) {
dirflag = 0;
}
if (sf !=null){
c = (Comparator) new CompareDocumentsByField(dirflag);
} else {
arrayOfIndividualHits = null;
return;
}
int i = 0;
int id = 0;
String fv = null;
try{
arrayOfIndividualHits = new Object[hitsCollection.length()];
long first = System.currentTimeMillis();
for (i=0; i<hitsCollection.length(); i++) {
id = hitsCollection.id(i);
try {
fv = sf.getFieldValue(id);
} catch (Exception e) {
fv = null;
}
if(fv != null && fv.length() > 0) {
arrayOfIndividualHits[i] = new IndividualHit(i, fv,
hitsCollection.score(i));
fv = null;
}
}
} catch (Exception e) {
msg("Error ["+fv+"]:"+e);
}
long second = System.currentTimeMillis();
Arrays.sort(arrayOfIndividualHits, c);
}
----- Original Message ----- From: "Erik Hatcher" <[EMAIL PROTECTED]> To: "Lucene Users List" <[EMAIL PROTECTED]> Sent: Monday, January 20, 2003 8:48 AM Subject: Re: SearchBean in action?On Sunday, January 19, 2003, at 11:17 PM, Terry Steichen wrote:I've recently made some specific additional changes (regarding scoring, as I recall). At this point I don't recall which of these have been included in the distribution and which have not.I'm using the latest CVS HEAD version of the searchbean codebase, just for comparisons sake.How are you switching between ascending and descending sort?However, since then I've been using SearchBean extensively and, my version at least, works fine for sorting in either ascending or descending date (or relevance) order on whatever field I choose.
In HitsIterator, its hardcoded to a particular field. Why? I've
removed this restriction and here is the diff:
public HitsIterator(Hits hits, String sortFlag) throws IOException{
this.hitsCollection = hits;
if (sortFlag != null){
- if ((sortFlag != "") &&
(sortFlag.equals(SearchBean.SORT_FIELD_RELEVANCE))){
- file://logger.debug("Sorting hits by field "+sortFlag);
+ if (sortFlag != "") {
+ System.out.println("Sorting hits by field "+sortFlag);
sortByField(sortFlag);
file://logger.debug("Completed sorting by field
"+sortFlag);}Please let me know what kind of problems you're having and I'll see ifSearchBean itself is hardcoded to the StandardAnalyzer. To be
I can
help.
generally useful this needs to be made configurable.
Those were the main issues I've had. I'm assuming you've made changes
in your version of SearchBean to account for at least the field name to
sort on, or you're always using "relevance" :)
Erik
--
To unsubscribe, e-mail:
<mailto:[EMAIL PROTECTED]>For additional commands, e-mail:<mailto:[EMAIL PROTECTED]>
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
