public SearchResult search(SearchRequest req){
String queryString=req.getQuery();
Analyzer analyzer=_idxReaderFactory.getAnalyzer();
QueryParser qparser=new QueryParser(Version.LUCENE_CURRENT,"content",analyzer);
SearchResult result=new SearchResult();
List<R> readers=null;
MultiReader multiReader=null;
Searcher searcher = null;
try
{
Query q=null;
if (queryString == null || queryString.length() ==0)
{
q = new MatchAllDocsQuery();
}
else
{
q = qparser.parse(queryString);
}
readers=_idxReaderFactory.getIndexReaders();
multiReader=new MultiReader(readers.toArray(new IndexReader[readers.size()]), false);
searcher=new IndexSearcher(multiReader);
long start=System.currentTimeMillis();
TopDocs docs=searcher.search(q, null, 10);
long end=System.currentTimeMillis();
result.setTime(end-start);
result.setTotalDocs(multiReader.numDocs());
result.setTotalHits(docs.totalHits);
ScoreDoc[] scoreDocs=docs.scoreDocs;
ArrayList<SearchHit> hitList=new ArrayList<SearchHit>(scoreDocs.length);
for (ScoreDoc scoreDoc : scoreDocs)
{
SearchHit hit=new SearchHit();
hit.setScore(scoreDoc.score);
int docid=scoreDoc.doc;
Document doc=multiReader.document(docid);
String content=doc.get("content");
Scorer qs=new QueryScorer(q);
SimpleHTMLFormatter formatter=new SimpleHTMLFormatter("<span class=\"hl\">","</span>");
Highlighter hl=new Highlighter(formatter,qs);
String[] fragments=hl.getBestFragments(analyzer, "content",content, 1);
Map<String,String[]> fields=convert(doc);
fields.put("fragment",fragments);
hit.setFields(fields);
hitList.add(hit);
}
result.setHits(hitList.toArray(new SearchHit[hitList.size()]));
return result;
}
catch(Exception e)
{
log.error(e.getMessage(),e);