Package proj.zoie.admin.client.search

Examples of proj.zoie.admin.client.search.SearchResult


  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);
View Full Code Here


      log.error(e);
      result = null;
    }
    if ((result==null) || (result.getHits()==null))
    {
      SearchResult res = new SearchResult();
      res.setHits(new SearchHit[0]);
      return res;
    }
    SearchResult res = new SearchResult();
    proj.zoie.service.api.SearchHit[] oldHits = result.getHits();
    SearchHit[] hits = new SearchHit[result.getHits().length];
    for(int i=0; i < oldHits.length; i++)
    {
      hits[i] = new SearchHit();
      hits[i].setFields(oldHits[i].getFields());
      hits[i].setScore(oldHits[i].getScore());
    }
    res.setHits(hits);
    res.setTime(result.getTime());
    res.setTotalDocs(result.getTotalDocs());
    res.setTotalHits(result.getTotalHits());
    return res;
  }
View Full Code Here

TOP

Related Classes of proj.zoie.admin.client.search.SearchResult

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.