Source code: com/RuntimeCollective/search/bean/SearchResult.java
1 /* $Header: /home/CVS/rjp/src/com/RuntimeCollective/search/bean/SearchResult.java,v 1.6 2003/09/30 15:12:57 joe Exp $
2 * $Revision: 1.6 $
3 * $Date: 2003/09/30 15:12:57 $
4 *
5 * ====================================================================
6 *
7 * Josephine : http://www.runtime-collective.com/josephine/index.html
8 *
9 * Copyright (C) 2003 Runtime Collective
10 *
11 * This product includes software developed by the
12 * Apache Software Foundation (http://www.apache.org/).
13 *
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 *
28 */
29
30 package com.RuntimeCollective.search.bean;
31
32 import org.apache.lucene.document.Document;
33 import com.RuntimeCollective.webapps.RuntimeParameters;
34 import com.RuntimeCollective.search.bean.Searchable;
35
36 /**
37 * A single search result.
38 *
39 * @version $Id: SearchResult.java,v 1.6 2003/09/30 15:12:57 joe Exp $
40 */
41 public class SearchResult {
42
43 private float score;
44 private Document doc;
45 private Searchable searchable;
46 private int index;
47
48 /**
49 * Make a new SearchResult from a Lucene <code>document</code>, and this document's score.
50 * Also give it an index number, for ranking
51 */
52 public SearchResult(Document doc, float score, int index) {
53 this.score = score;
54 this.doc = doc;
55 this.index = index;
56 }
57
58 /**
59 * Get the Searchable object that this search result refers to.
60 */
61 public Searchable getSearchable() {
62 if (searchable == null) {
63 String className = doc.get("ClassName");
64 int id = Integer.parseInt(doc.get("Id"));
65 try {
66 searchable = (Searchable) RuntimeParameters.getStore().get(className, id);
67 } catch (RuntimeException e) {
68 RuntimeParameters.logError(this, "Searchable refers to a non-existent EntityBean: "+className+" "+id);
69 return null;
70 }
71 }
72 return searchable;
73 }
74
75 /**
76 * Get the Document, useful for doing some complex post-processing.
77 * Example: sort the results without having to load all the Searchables.
78 */
79 public Document getDocument() {
80 return doc;
81 }
82
83 /**
84 * Get the score, or ranking, of this search result.
85 */
86 public float getScore() {
87 return score;
88 }
89
90 /**
91 * Get the index of this search result.
92 */
93 public int getIndex() {
94 return index;
95 }
96
97 /**
98 * Set the index of this search result.
99 */
100 public void setIndex(int pos) {
101 index = pos;
102 }
103
104 /**
105 * Get a link that will display this search result
106 */
107 public String getLink() {
108 return doc.get("Link");
109 }
110
111 /**
112 * Get the title of the this search result.
113 */
114 public String getTitle() {
115 return doc.get("SearchTitle");
116 }
117
118 /**
119 * Get the summary of this search result.
120 */
121 public String getSummary() {
122 return doc.get("SearchSummary");
123 }
124
125 /**
126 * Get the full text of the this search result.
127 */
128 public String getSearchText() {
129 return doc.get("SearchText");
130 }
131 }