1 /* 2 +-----------------------------------------------------------------------------+ 3 | ILIAS open source | 4 +-----------------------------------------------------------------------------+ 5 | Copyright (c) 1998-2001 ILIAS open source, University of Cologne | 6 | | 7 | This program is free software; you can redistribute it and/or | 8 | modify it under the terms of the GNU General Public License | 9 | as published by the Free Software Foundation; either version 2 | 10 | of the License, or (at your option) any later version. | 11 | | 12 | This program is distributed in the hope that it will be useful, | 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 15 | GNU General Public License for more details. | 16 | | 17 | You should have received a copy of the GNU General Public License | 18 | along with this program; if not, write to the Free Software | 19 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | 20 +-----------------------------------------------------------------------------+ 21 */ 22 23 package de.ilias.services.lucene.search; 24 25 import java.io.IOException; 26 import java.util.Vector; 27 28 import org.apache.logging.log4j.LogManager; 29 import org.apache.lucene.document.Document; 30 import org.apache.lucene.index.CorruptIndexException; 31 import org.apache.lucene.search.IndexSearcher; 32 import org.apache.lucene.search.ScoreDoc; 33 import org.jdom.output.XMLOutputter; 34 35 import de.ilias.services.settings.ConfigurationException; 36 import org.apache.logging.log4j.Logger; 37 38 /** 39 * 40 * 41 * @author Stefan Meyer <smeyer.ilias@gmx.de> 42 * @version $Id$ 43 */ 44 public class SearchResultWriter { 45 46 protected Logger logger = LogManager.getLogger(SearchResultWriter.class); 47 48 private IndexSearcher searcher = null; 49 private ScoreDoc[] hits = null; 50 private SearchHits result = null; 51 private int offset = 0; 52 53 /** 54 * @param hits 55 * @throws ConfigurationException 56 * @throws IOException 57 */ SearchResultWriter(ScoreDoc[] hits)58 public SearchResultWriter(ScoreDoc[] hits) throws IOException, ConfigurationException { 59 60 this.hits = hits; 61 62 searcher = SearchHolder.getInstance().getSearcher(); 63 result = new SearchHits(); 64 } 65 66 /** 67 * @throws IOException 68 * @throws CorruptIndexException 69 * 70 */ write()71 public void write() throws CorruptIndexException, IOException { 72 73 result.setTotalHits(hits.length); 74 logger.info("Found " + result.getTotalHits() + " hits!"); 75 result.setLimit(SearchHolder.SEARCH_LIMIT); 76 77 SearchObject object; 78 Document hitDoc; 79 for(int i = 0; i < hits.length;i++) { 80 // Set max score 81 if(i == 0) { 82 result.setMaxScore(hits[i].score); 83 } 84 if(i < getOffset()) { 85 continue; 86 } 87 if(i >= (getOffset() + SearchHolder.SEARCH_LIMIT)) { 88 logger.debug("Reached result limit. Aborting!"); 89 break; 90 } 91 try { 92 logger.debug("Added object"); 93 object = new SearchObject(); 94 hitDoc = searcher.doc(hits[i].doc); 95 object.setId(Integer.parseInt(hitDoc.get("objId"))); 96 object.setAbsoluteScore(hits[i].score); 97 result.addObject(object); 98 } 99 catch (NumberFormatException e) { 100 logger.warn("Found invalid document (missing objId) with document id: " + hits[i].doc); 101 } 102 } 103 } 104 105 /** 106 * @return 107 */ toXML()108 public String toXML() { 109 110 org.jdom.Document doc = new org.jdom.Document(result.addXML()); 111 XMLOutputter outputter = new XMLOutputter(); 112 return outputter.outputString(doc); 113 114 } 115 116 /** 117 * @param offset the offset to set 118 */ setOffset(int offset)119 public void setOffset(int offset) { 120 this.offset = offset; 121 } 122 123 /** 124 * @return the offset 125 */ getOffset()126 public int getOffset() { 127 return offset; 128 } 129 130 } 131