Ignore:
Timestamp:
2011-10-07T11:36:07+13:00 (13 years ago)
Author:
sjm84
Message:

Lucene 3.x version of code accidentally commited rolling back to 2.x compatible version

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone2/common-src/indexers/lucene-gs/src/org/greenstone/LuceneWrapper/LuceneQueryResult.java

    r24725 r24731  
    2929import java.util.Vector;
    3030
    31 /** Opportunity to fine tune QueryResult for lucene search
     31/** a QueryResult class for a lucene search
    3232 *
    3333 */
    34 
    35 public class LuceneQueryResult extends SharedSoleneQueryResult {
    36    
    37     // Currently no fine tuning -- rely on underlying shared Solr/Lucene base class
     34public class LuceneQueryResult {
     35   
     36    public static final int NO_ERROR = 0;
     37    public static final int PARSE_ERROR = 1;
     38    public static final int TOO_MANY_CLAUSES_ERROR = 2;
     39    public static final int IO_ERROR = 3;
     40    public static final int OTHER_ERROR = 4;
     41   
     42    /** the list of DocInfo */
     43    protected Vector docs_=null;
     44    /** the list of TermInfo */
     45    protected Vector terms_=null;
     46    /** the list of stopwords found in the query */
     47    protected Vector stopwords_ = null;
     48    /** the total number of docs found - not necessarily the size of docs_*/
     49    protected int total_num_docs_=0;
     50    /** the start result number if we are retrieving only a portion of the results */
     51    protected int start_results_ = 0;
     52    /** the end result number if we are retrieving only a portion of the results */
     53    protected int end_results_ = 0;
     54    /** whether an error has occurred and what kind it is*/
     55    protected int error_ = NO_ERROR;
     56
    3857    LuceneQueryResult() {
    39     super();
    40     }
     58    docs_ = new Vector();
     59    terms_ = new Vector();
     60    stopwords_ = new Vector();
     61    }
     62   
     63    /** clear the info from the last query - should be called before setting any new docs/terms */
     64    public void clear() {
     65    total_num_docs_=0;
     66    docs_.clear();
     67    terms_.clear();
     68    stopwords_.clear();
     69    error_ = NO_ERROR;
     70    }
     71
     72    /** returns the result as a String - useful for printing out results */
     73    public String toString() {
     74   
     75    String result = "";
     76    result += "docs (ranks): ";
     77    for (int i=0; i<docs_.size(); i++) {
     78        result += ((DocInfo)docs_.elementAt(i)).toString()+", ";
     79    }
     80    result += "\nterms: ";
     81    for (int i=0; i<terms_.size(); i++) {
     82        result += ((TermInfo)terms_.elementAt(i)).toString()+", ";
     83    }
     84    result += "\nactual number of docs found = "+total_num_docs_;
     85   
     86    return result;
     87    }
     88    /** a shorter representation - just terms and total docs - not the
     89    individual docnums and ranks */
     90    public String toShortString() {
     91    String result = "";
     92    result += "\nterms: ";
     93    for (int i=0; i<terms_.size(); i++) {
     94        result += ((TermInfo)terms_.elementAt(i)).toString()+", ";
     95    }
     96    result += "\nactual number of docs found = "+total_num_docs_;
     97    return result;
     98    }
     99   
     100    public void setTotalDocs(int num) {
     101    total_num_docs_=num;
     102    }
     103   
     104    public void setStartResults(int start) {
     105    start_results_ = start;
     106    }
     107
     108    public void setEndResults(int end) {
     109    end_results_ = end;
     110    }
     111
     112    public void addDoc(String id, float rank, int termfreq)
     113    {
     114    docs_.add(new DocInfo(id, rank, termfreq));
     115    }
     116   
     117    public void addTerm(String term, String field, int match, int freq) {
     118    TermInfo ti = new TermInfo();
     119    ti.term_=term;
     120    ti.field_=field;
     121    ti.match_docs_=match;
     122    ti.term_freq_=freq;
     123    terms_.add(ti);
     124    }
     125    public void addStopWord(String stopword) {
     126    stopwords_.add(stopword);
     127    }
     128    public Vector getDocs() {
     129    return docs_;
     130    }
     131   
     132    public int getError() {
     133    return error_;
     134    }
     135   
     136    public String getErrorString() {
     137    if (error_ == PARSE_ERROR) {
     138        return "PARSE_EXCEPTION";
     139    }
     140    if (error_ == TOO_MANY_CLAUSES_ERROR) {
     141        return "TOO_MANY_CLAUSES";
     142    }
     143    if (error_ == IO_ERROR) {
     144        return "IO_ERROR";
     145    }
     146    if (error_ == NO_ERROR) {
     147        return "NO_ERROR";
     148    }
     149    return "UNKNOWN";
     150    }
     151
     152    public Vector getTerms() {
     153    return terms_;
     154    }
     155   
     156    public Vector getStopWords() {
     157    return stopwords_;
     158    }
     159    public int getTotalDocs() {
     160    return total_num_docs_;
     161    }
     162   
     163    public void setError(int error) {
     164    error_ = error;
     165    }
     166   
     167    public String getXMLString() {
     168    StringBuffer buffer = new StringBuffer();
     169
     170    // terms
     171    buffer.append("<QueryTermsInfo num=\"" + terms_.size() + "\"/>\n");
     172    for (int i=0; i<terms_.size(); i++) {
     173        buffer.append(((TermInfo)terms_.elementAt(i)).toXMLString()+"\n");
     174    }
     175
     176    // stopwords
     177    for (int i=0; i<stopwords_.size(); i++) {
     178        buffer.append("<StopWord value=\"" + (String)stopwords_.elementAt(i)+"\" />\n");
     179    }
     180   
     181    // results
     182    buffer.append("<MatchingDocsInfo num=\"" + total_num_docs_ + "\"/>\n");
     183    buffer.append("<StartResults num=\"" + start_results_ + "\"/>\n");
     184    buffer.append("<EndResults num=\"" + end_results_ + "\"/>\n");
     185   
     186    for (int i=0; i< docs_.size(); i++) {
     187        buffer.append(((DocInfo)docs_.elementAt(i)).toXMLString()+"\n");
     188    }
     189
     190    return buffer.toString();
     191    }
     192
     193 
     194    public class TermInfo {
     195   
     196    /** the term itself */
     197    public String term_=null;
     198    /** the field for which this term was queried */
     199    public String field_=null;
     200    /** the number of documents containing this term */
     201    public int match_docs_=0;
     202    /** overall term freq for this term */
     203    public int term_freq_=0;
     204   
     205    public TermInfo() {
     206    }
     207   
     208    /** output the class as a string */
     209    public String toString() {
     210        String result="";
     211        result +="<"+field_+">\""+term_+" docs("+match_docs_;
     212        result +=")freq("+term_freq_+")";
     213        return result;
     214    }
     215
     216    /** output as an XML element */
     217    public String toXMLString() {
     218        return "<Term value=\"" + xmlSafe(term_) + "\" field=\"" + field_ + "\" freq=\"" + term_freq_ + "\" />";
     219    }
     220    }
     221
     222
     223    public class DocInfo
     224    {
     225    public String id_ = "";
     226    public float rank_ = 0;
     227    public int termfreq_ = 0;
     228
     229    public DocInfo (String id, float rank, int termfreq)
     230    {
     231        id_ = id;
     232        rank_ = rank;
     233        termfreq_ = termfreq;
     234    }
     235
     236    public String toString()
     237    {
     238        return "" + id_ + " (" + rank_ + ") (" + termfreq_ + ")";
     239    }
     240
     241    public String toXMLString()
     242    {
     243        return "<Match id=\"" + id_ + "\" rank=\"" + rank_ + "\" termfreq=\"" + termfreq_ + "\" />";
     244    }
     245    }
     246
     247
     248    // where should this go???
     249    public static String xmlSafe(String text) {
     250    text = text.replaceAll("&","&amp;amp;");
     251    text = text.replaceAll("<","&amp;lt;");
     252    text = text.replaceAll(">","&amp;gt;");
     253    text = text.replaceAll("'","&amp;#039;");
     254    text = text.replaceAll("\\\"","&amp;quot;");
     255    return text;
     256    }
     257 
    41258}
Note: See TracChangeset for help on using the changeset viewer.