001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.search;
019:
020: import java.util.ArrayList;
021: import java.util.Collections;
022: import java.util.Iterator;
023: import java.util.List;
024:
025: import org.apache.lucene.document.Document;
026:
027: import de.finix.contelligent.logging.LoggingService;
028:
029: public class SearchResult implements java.io.Serializable {
030:
031: final static org.apache.log4j.Logger log = LoggingService
032: .getLogger(SearchResult.class);
033:
034: private List items = new ArrayList(10);
035:
036: /** The total number of results found */
037: private int total = 0;
038:
039: /** The starting number requested */
040: private int start = 0;
041:
042: private int end;
043:
044: private String query;
045:
046: private int filtered;
047:
048: public SearchResult(int total, int start) {
049: this .total = total;
050: this .start = start;
051: }
052:
053: public void add(Document document, float score) {
054: items.add(new SearchResultItem(document, score));
055:
056: if (log.isDebugEnabled())
057: log.debug("add document Score: " + score);
058:
059: }
060:
061: public Iterator iterator() {
062: return Collections.unmodifiableList(items).iterator();
063: }
064:
065: public String toString() {
066: StringBuffer buffer = new StringBuffer();
067: Iterator iterator = iterator();
068: SearchResultItem item;
069: int i = 0;
070:
071: if (query != null) {
072: buffer.append("Query: ").append(query).append("\n\n");
073: }
074: buffer.append(getFilteredTotal()).append(
075: " matching documents (" + total + "/" + start + " - "
076: + end + ")\n");
077:
078: while (iterator.hasNext()) {
079: i++;
080: item = (SearchResultItem) iterator.next();
081: buffer.append(i).append(". ").append(item.getPath())
082: .append(" Score: ").append(item.score).append("\n");
083: }
084: return buffer.toString();
085: }
086:
087: /**
088: * This is the total length of the query result without filtering. If a
089: * {@link SearchResultCollector} is specified, this might not match with the
090: * total length of the filtered query result.
091: *
092: * @return
093: */
094: public int getTotalLength() {
095: return total;
096: }
097:
098: public int getLength() {
099: return items.size();
100: }
101:
102: public int getStart() {
103: return start;
104: }
105:
106: public int getEnd() {
107: return end;
108: }
109:
110: public void setEnd(int end) {
111: this .end = end;
112: }
113:
114: public String getQuery() {
115: return query;
116: }
117:
118: public void setQuery(String query) {
119: this .query = query;
120: }
121:
122: public void setFiltered(int filtered) {
123: this .filtered = filtered;
124: }
125:
126: public int getFiltered() {
127: return filtered;
128: }
129:
130: public int getFilteredTotal() {
131: return getTotalLength() - getFiltered();
132: }
133:
134: public class SearchResultItem implements java.io.Serializable {
135:
136: private String path;
137:
138: private float score;
139:
140: private Document document;
141:
142: private String type;
143:
144: public SearchResultItem() {
145: }
146:
147: public SearchResultItem(Document document, float score) {
148: this .path = document.get("path");
149: this .score = score;
150: this .document = document;
151: type = document.get("type");
152: }
153:
154: public String getPath() {
155: return path;
156: }
157:
158: public float getScore() {
159: return score;
160: }
161:
162: public String getType() {
163: return type;
164: }
165:
166: public String getFieldValue(String fieldName) {
167: return document.get(fieldName);
168: }
169:
170: public Document getDocument() {
171: return document;
172: }
173: }
174: }
|