KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > nutch > searcher > Hit


1 /* Copyright (c) 2003 The Nutch Organization. All rights reserved. */
2 /* Use subject to the conditions in http://www.nutch.org/LICENSE.txt. */
3
4 package net.nutch.searcher;
5
6 import java.io.DataInput JavaDoc;
7 import java.io.DataOutput JavaDoc;
8 import java.io.IOException JavaDoc;
9
10 import net.nutch.io.Writable;
11 import net.nutch.io.UTF8;
12
13 import java.util.logging.Logger JavaDoc;
14 import net.nutch.util.LogFormatter;
15
16 /** A document which matched a query in an index. */
17 public class Hit implements Writable, Comparable JavaDoc {
18   private static final Logger JavaDoc LOG =
19     LogFormatter.getLogger("net.nutch.searcher.Hit");
20
21   private int indexNo; // index id
22
private int indexDocNo; // index-relative id
23
private float score; // its score for the query
24
private String JavaDoc site; // its website name
25
private boolean moreFromSiteExcluded;
26
27   public Hit() {}
28
29   public Hit(int indexNo, int indexDocNo, float score, String JavaDoc site) {
30     this(indexDocNo, score, site);
31     this.indexNo = indexNo;
32   }
33   public Hit(int indexDocNo, float score, String JavaDoc site) {
34     this.indexDocNo = indexDocNo;
35     this.score = score;
36     // 20041006, xing
37
// The following fixes a bug that causes cached.jsp, text.jsp, etc.,
38
// to fail in distributed search. "Release 0.6, note 14" in CHANGES.txt
39
if (site == null)
40       site = "";
41     this.site = site;
42   }
43
44   /** Return the index number that this hit came from. */
45   public int getIndexNo() { return indexNo; }
46   public void setIndexNo(int indexNo) { this.indexNo = indexNo; }
47
48   /** Return the document number of this hit within an index. */
49   public int getIndexDocNo() { return indexDocNo; }
50
51   /** Return the degree to which this document matched the query. */
52   public float getScore() { return score; }
53
54   /** Return the name of this this document's website. */
55   public String JavaDoc getSite() { return site; }
56
57   /** True iff other, lower-scoring, hits from the same site have been excluded
58    * from the list which contains this hit.. */

59   public boolean moreFromSiteExcluded() { return moreFromSiteExcluded; }
60
61   /** True iff other, lower-scoring, hits from the same site have been excluded
62    * from the list which contains this hit.. */

63   public void setMoreFromSiteExcluded(boolean more){moreFromSiteExcluded=more;}
64
65   public void write(DataOutput JavaDoc out) throws IOException JavaDoc {
66     out.writeInt(indexDocNo);
67     out.writeFloat(score);
68     UTF8.writeString(out, site);
69   }
70
71   public void readFields(DataInput JavaDoc in) throws IOException JavaDoc {
72     indexDocNo = in.readInt();
73     score = in.readFloat();
74     site = UTF8.readString(in);
75   }
76
77   /** Display as a string. */
78   public String JavaDoc toString() {
79     return "#" + indexDocNo;
80   }
81
82   public boolean equals(Object JavaDoc o) {
83     if (!(o instanceof Hit))
84       return false;
85     Hit other = (Hit)o;
86     return this.indexNo == other.indexNo
87       && this.indexDocNo == other.indexDocNo;
88   }
89
90   public int hashCode() {
91     return indexNo ^ indexDocNo;
92   }
93
94   public int compareTo(Object JavaDoc o) {
95     Hit other = (Hit)o;
96     if (other.score > this.score) { // prefer higher scores
97
return 1;
98     } else if (other.score < this.score) {
99       return -1;
100     } else if (other.indexNo != this.indexNo) {
101       return other.indexNo - this.indexNo; // prefer later indexes
102
} else {
103       return other.indexDocNo - this.indexDocNo; // prefer later docs
104
}
105   }
106 }
107
Popular Tags