FileDocCategorySizeDatePackage
QueryBean.javaAPI DocExample2817Mon Mar 01 16:04:32 GMT 2004com.develop.ss.search

QueryBean

public class QueryBean extends Object implements Serializable

Fields Summary
private String
query
private String
index
private String
field
private List
results
private static float
THRESHOLD_SCORE
Constructors Summary
public QueryBean(String index, String query, String field)


        
  
      this.field = field;
      this.index = index;
      this.query = query;
  
public QueryBean(String index, String query)

      this(index, query, "contents");
  
Methods Summary
public voidexecute()

    results.clear();
    if (query == null) return;
    if (field == null) throw new IllegalArgumentException("field cannot be null");
    if (index == null) throw new IllegalArgumentException("index cannot be null");
    IndexSearcher indexSearcher = new IndexSearcher(index);
    try {
      Analyzer analyzer = new StandardAnalyzer();
      Query q = QueryParser.parse(query, field, analyzer);
      Hits hits = indexSearcher.search(q);
      for (int n=0; n<hits.length(); n++) {
        if (hits.score(n) < THRESHOLD_SCORE) {
          return;
        }
        Document d = hits.doc(n);
        String title = safeGetFieldString(d, "title");
        results.add(new HitBean(d.getField("url").stringValue(), safeGetFieldString(d, "title"), hits.score(n)));
      }
    } finally {
      indexSearcher.close();
    }
  
public java.lang.StringgetField()

    return field;
  
public java.lang.StringgetIndex()

    return index;
  
public java.lang.StringgetQuery()

    return query;
  
public HitBeangetResult(int index)

    return (HitBean) results.get(index);
  
public HitBean[]getResults()

    return (HitBean[]) results.toArray(new HitBean[results.size()]);
  
private java.lang.StringsafeGetFieldString(org.apache.lucene.document.Document d, java.lang.String field)

    Field f = d.getField(field);
    return (f == null) ? "" : f.stringValue();
  
public voidsetField(java.lang.String field)

    this.field = field;
  
public voidsetIndex(java.lang.String index)

    this.index = index;
  
public voidsetQuery(java.lang.String query)

    this.query = query;