001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.lucene.search;
017:
018: import java.io.IOException;
019:
020: import org.apache.lucene.document.Document;
021: import org.apache.lucene.index.CorruptIndexException;
022:
023: /**
024: * Wrapper used by {@link HitIterator} to provide a lazily loaded hit
025: * from {@link Hits}.
026: *
027: * @author Jeremy Rayner
028: */
029: public class Hit implements java.io.Serializable {
030:
031: private Document doc = null;
032:
033: private boolean resolved = false;
034:
035: private Hits hits = null;
036: private int hitNumber;
037:
038: /**
039: * Constructed from {@link HitIterator}
040: * @param hits Hits returned from a search
041: * @param hitNumber Hit index in Hits
042: */
043: Hit(Hits hits, int hitNumber) {
044: this .hits = hits;
045: this .hitNumber = hitNumber;
046: }
047:
048: /**
049: * Returns document for this hit.
050: *
051: * @see Hits#doc(int)
052: * @throws CorruptIndexException if the index is corrupt
053: * @throws IOException if there is a low-level IO error
054: */
055: public Document getDocument() throws CorruptIndexException,
056: IOException {
057: if (!resolved)
058: fetchTheHit();
059: return doc;
060: }
061:
062: /**
063: * Returns score for this hit.
064: *
065: * @see Hits#score(int)
066: */
067: public float getScore() throws IOException {
068: return hits.score(hitNumber);
069: }
070:
071: /**
072: * Returns id for this hit.
073: *
074: * @see Hits#id(int)
075: */
076: public int getId() throws IOException {
077: return hits.id(hitNumber);
078: }
079:
080: private void fetchTheHit() throws CorruptIndexException,
081: IOException {
082: doc = hits.doc(hitNumber);
083: resolved = true;
084: }
085:
086: // provide some of the Document style interface (the simple stuff)
087:
088: /**
089: * Returns the boost factor for this hit on any field of the underlying document.
090: *
091: * @see Document#getBoost()
092: * @throws CorruptIndexException if the index is corrupt
093: * @throws IOException if there is a low-level IO error
094: */
095: public float getBoost() throws CorruptIndexException, IOException {
096: return getDocument().getBoost();
097: }
098:
099: /**
100: * Returns the string value of the field with the given name if any exist in
101: * this document, or null. If multiple fields exist with this name, this
102: * method returns the first value added. If only binary fields with this name
103: * exist, returns null.
104: *
105: * @see Document#get(String)
106: * @throws CorruptIndexException if the index is corrupt
107: * @throws IOException if there is a low-level IO error
108: */
109: public String get(String name) throws CorruptIndexException,
110: IOException {
111: return getDocument().get(name);
112: }
113:
114: /**
115: * Prints the parameters to be used to discover the promised result.
116: */
117: public String toString() {
118: StringBuffer buffer = new StringBuffer();
119: buffer.append("Hit<");
120: buffer.append(hits.toString());
121: buffer.append(" [");
122: buffer.append(hitNumber);
123: buffer.append("] ");
124: if (resolved) {
125: buffer.append("resolved");
126: } else {
127: buffer.append("unresolved");
128: }
129: buffer.append(">");
130: return buffer.toString();
131: }
132:
133: }
|