KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > search > Hit


1 /**
2  * Copyright 2005 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.lucene.search;
18
19 import java.io.IOException JavaDoc;
20
21 import org.apache.lucene.document.Document;
22
23 /**
24  * Wrapper used by {@link HitIterator} to provide a lazily loaded hit
25  * from {@link Hits}.
26  *
27  * @author Jeremy Rayner
28  */

29 public class Hit implements java.io.Serializable JavaDoc {
30
31   private Document doc = null;
32
33   private boolean resolved = false;
34
35   private Hits hits = null;
36   private int hitNumber;
37
38   /**
39    * Constructed from {@link HitIterator}
40    * @param hits Hits returned from a search
41    * @param hitNumber Hit index in Hits
42    */

43   Hit(Hits hits, int hitNumber) {
44     this.hits = hits;
45     this.hitNumber = hitNumber;
46   }
47
48   /**
49    * Returns document for this hit.
50    *
51    * @see Hits#doc(int)
52    */

53   public Document getDocument() throws IOException JavaDoc {
54     if (!resolved) fetchTheHit();
55     return doc;
56   }
57
58   /**
59    * Returns score for this hit.
60    *
61    * @see Hits#score(int)
62    */

63   public float getScore() throws IOException JavaDoc {
64     return hits.score(hitNumber);
65   }
66
67   /**
68    * Returns id for this hit.
69    *
70    * @see Hits#id(int)
71    */

72   public int getId() throws IOException JavaDoc {
73     return hits.id(hitNumber);
74   }
75
76   private void fetchTheHit() throws IOException JavaDoc {
77     doc = hits.doc(hitNumber);
78     resolved = true;
79   }
80
81   // provide some of the Document style interface (the simple stuff)
82

83   /**
84    * Returns the boost factor for this hit on any field of the underlying document.
85    *
86    * @see Document#getBoost()
87    */

88   public float getBoost() throws IOException JavaDoc {
89     return getDocument().getBoost();
90   }
91
92   /**
93    * Returns the string value of the field with the given name if any exist in
94    * this document, or null. If multiple fields exist with this name, this
95    * method returns the first value added. If only binary fields with this name
96    * exist, returns null.
97    *
98    * @see Document#get(String)
99    */

100   public String JavaDoc get(String JavaDoc name) throws IOException JavaDoc {
101     return getDocument().get(name);
102   }
103
104   /**
105    * Prints the parameters to be used to discover the promised result.
106    */

107   public String JavaDoc toString() {
108     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
109     buffer.append("Hit<");
110     buffer.append(hits.toString());
111     buffer.append(" [");
112     buffer.append(hitNumber);
113     buffer.append("] ");
114     if (resolved) {
115         buffer.append("resolved");
116     } else {
117         buffer.append("unresolved");
118     }
119     buffer.append(">");
120     return buffer.toString();
121   }
122
123
124 }
125
Popular Tags