QueryBeanpublic 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 void | execute()
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.String | getField()
return field;
| public java.lang.String | getIndex()
return index;
| public java.lang.String | getQuery()
return query;
| public HitBean | getResult(int index)
return (HitBean) results.get(index);
| public HitBean[] | getResults()
return (HitBean[]) results.toArray(new HitBean[results.size()]);
| private java.lang.String | safeGetFieldString(org.apache.lucene.document.Document d, java.lang.String field)
Field f = d.getField(field);
return (f == null) ? "" : f.stringValue();
| public void | setField(java.lang.String field)
this.field = field;
| public void | setIndex(java.lang.String index)
this.index = index;
| public void | setQuery(java.lang.String query)
this.query = query;
|
|