Search in sources :

Example 1 with TopDocCollector

use of org.apache.lucene.search.TopDocCollector in project bigbluebutton by bigbluebutton.

the class SearchController method onSubmit.

/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax
	 * .servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse,
	 * java.lang.Object, org.springframework.validation.BindException)
	 */
@Override
protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {
    SearchCriteriaCommand srchCriteriaCommand = (SearchCriteriaCommand) command;
    int startFrom = (new Integer(srchCriteriaCommand.getStartFrom())).intValue();
    int endIndex = 0;
    String queryStr = srchCriteriaCommand.getKeyWords();
    String sortBy = srchCriteriaCommand.getSort();
    String operator = srchCriteriaCommand.getOperator();
    String relRange = srchCriteriaCommand.getRangeValue();
    boolean bSmart = (relRange != null) && (!relRange.isEmpty());
    boolean bSortByScore = sortBy.equalsIgnoreCase("byScore");
    if (logger.isInfoEnabled()) {
        logger.info("---search offset=" + startFrom + " sortBy=" + sortBy + "qryString=" + queryStr + "operator=" + operator);
    }
    Map<String, Object> model = new HashMap<String, Object>();
    LinkedHashMap<String, MatchVO> sortedMap = new LinkedHashMap<String, MatchVO>();
    Map<String, SessionHitsOrganizer> hitsOrganizerMap = new HashMap<String, SessionHitsOrganizer>();
    Map<String, String> resultMap = new HashMap<String, String>();
    synchronized (Index.getInstance()) {
        Search search = Search.getInstance();
        search.startSearch();
        TopDocs tps = null;
        Searcher searcher = null;
        ScoreDoc[] hits = null;
        if (bSortByScore) {
            Search.TopDocCollectorSearchResult result = search.searchByScore(queryStr, startFrom, operator);
            TopDocCollector collector = result.getCollector();
            if (collector != null) {
                tps = collector.topDocs();
            }
            hits = tps.scoreDocs;
            searcher = result.getSearcher();
        } else {
            Search.TopFieldDocsSearchResult result = search.searchBySession(queryStr, startFrom, operator);
            TopFieldDocs tfd = result.getTopFieldDocs();
            if (tfd != null) {
                hits = tfd.scoreDocs;
            }
            searcher = result.getSearcher();
        }
        if (hits == null) {
            if (logger.isInfoEnabled()) {
                logger.info("---No hit");
            }
        } else {
            int start = startFrom;
            int end = hits.length;
            endIndex = end;
            if (logger.isInfoEnabled()) {
                logger.info("total match number=" + endIndex);
            }
            String currentSession = "0";
            String lastSession = "0";
            SessionHitsOrganizer hitsOrganizer = null;
            for (int i = start; i < end; i++) {
                float score = hits[i].score;
                Document doc = searcher.doc(hits[i].doc);
                String path = doc.get("path");
                if (path != null) {
                    MatchVO matchVO = new MatchVO();
                    matchVO.setFilePath(path);
                    String fullContent = doc.get("title");
                    String summary = getKeywordContext(queryStr, fullContent);
                    matchVO.setContentSummary(summary);
                    String fileName = doc.get("fileName");
                    matchVO.setFileName(fileName);
                    String indexSummary = doc.get("summary");
                    matchVO.setIndexingSummary(indexSummary);
                    matchVO.setScore(score);
                    String title = indexSummary + ": " + fileName + " (Match Score = " + score + ")";
                    //String content = doc.get("contents");								
                    String allData = title + "%" + summary;
                    if (doc.get("slideTime") != null) {
                        allData += "%" + doc.get("slideTime");
                        matchVO.setSlidePlayTime(doc.get("slideTime"));
                    }
                    //sortedMap.put(path, allData);
                    sortedMap.put(path, matchVO);
                    //model.put(path, newTitle+"%"+doc.get("summary")+"%"+doc.get("slideTime"));
                    if (logger.isInfoEnabled()) {
                        logger.info("----" + allData);
                        logger.info((i + 1) + ". " + path);
                    }
                    if (title != null) {
                        if (logger.isInfoEnabled()) {
                            logger.info("   Title: " + doc.get("title"));
                        }
                    }
                    if (bSmart) {
                        //Prepare for the grouping results						
                        currentSession = getSessionNumberFromFileURL(path);
                        //get existing current session organizer
                        hitsOrganizer = hitsOrganizerMap.get(currentSession);
                        if (hitsOrganizer == null) {
                            //create a new session organizer object
                            hitsOrganizer = new SessionHitsOrganizer();
                            hitsOrganizer.setSessionNum(currentSession);
                            hitsOrganizerMap.put(currentSession, hitsOrganizer);
                        }
                        hitsOrganizer.setReleventRange((new Float(relRange)).floatValue());
                        hitsOrganizer.addExactHits(path, score);
                        matchVO.setSessionHitOrganier(hitsOrganizer);
                    }
                } else {
                    System.out.println((i + 1) + ". " + "No path for this document");
                }
            }
        }
        search.finishSearch();
        //post processing for result grouping...			
        Iterator hitsOrganizerIt = hitsOrganizerMap.keySet().iterator();
        while (hitsOrganizerIt.hasNext()) {
            String key = (String) hitsOrganizerIt.next();
            SessionHitsOrganizer organizer = hitsOrganizerMap.get(key);
            organizer.generateResultGroup();
        }
        model.put("result", sortedMap);
        if (bSmart) {
            model.put("hitsOrganizer", hitsOrganizerMap);
        }
        model.put("searchKeyword", queryStr);
        model.put("startFrom", (new Integer(startFrom)).toString());
        model.put("endAt", (new Integer(endIndex)).toString());
        model.put("sortBy", sortBy);
        model.put("operator", operator);
        model.put("rangeValue", relRange);
    }
    ModelAndView mav = new ModelAndView(this.getSuccessView(), model);
    return mav;
}
Also used : SearchCriteriaCommand(org.bigbluebutton.webminer.web.model.SearchCriteriaCommand) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ModelAndView(org.springframework.web.servlet.ModelAndView) MatchVO(org.bigbluebutton.webminer.web.model.MatchVO) TopFieldDocs(org.apache.lucene.search.TopFieldDocs) Document(org.apache.lucene.document.Document) LinkedHashMap(java.util.LinkedHashMap) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) SessionHitsOrganizer(org.bigbluebutton.webminer.web.model.SessionHitsOrganizer) Search(org.bigbluebutton.webminer.search.Search) Iterator(java.util.Iterator) Searcher(org.apache.lucene.search.Searcher) TopDocCollector(org.apache.lucene.search.TopDocCollector)

Example 2 with TopDocCollector

use of org.apache.lucene.search.TopDocCollector in project bigbluebutton by bigbluebutton.

the class Search method searchByScore.

public TopDocCollectorSearchResult searchByScore(String queryStr, int startFrom, String operator) {
    try {
        queryStr = queryStr.trim();
        QueryParser parser = new QueryParser("contents", analyzer);
        if (QueryParser.AND_OPERATOR.toString().equalsIgnoreCase(operator)) {
            parser.setDefaultOperator(QueryParser.AND_OPERATOR);
        } else {
            parser.setDefaultOperator(QueryParser.OR_OPERATOR);
        }
        Query query;
        query = parser.parse(queryStr);
        TopDocCollector collector = doPagingSearch(query, startFrom);
        TopDocCollectorSearchResult result = new TopDocCollectorSearchResult(collector, searcher);
        return result;
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
// System.out.println("Searching for: " + query.toString("contents"));
// doPagingSearch(in, searcher, query, hitsPerPage, raw, queries ==
// null);
// }
// reader.close();
}
Also used : QueryParser(org.apache.lucene.queryParser.QueryParser) Query(org.apache.lucene.search.Query) TopDocCollector(org.apache.lucene.search.TopDocCollector) ParseException(org.apache.lucene.queryParser.ParseException) IOException(java.io.IOException)

Example 3 with TopDocCollector

use of org.apache.lucene.search.TopDocCollector in project onebusaway-application-modules by camsys.

the class StopSearchServiceImpl method search.

private SearchResult<AgencyAndId> search(QueryParser parser, String value, int maxResultCount, double minScoreToKeep) throws IOException, ParseException {
    if (_searcher == null)
        return new SearchResult<AgencyAndId>();
    TopDocCollector collector = new TopDocCollector(maxResultCount);
    Query query = parser.parse(value);
    _searcher.search(query, collector);
    TopDocs top = collector.topDocs();
    Map<AgencyAndId, Float> topScores = new HashMap<AgencyAndId, Float>();
    for (ScoreDoc sd : top.scoreDocs) {
        Document document = _searcher.doc(sd.doc);
        if (sd.score < minScoreToKeep)
            continue;
        String agencyId = document.get(StopSearchIndexConstants.FIELD_AGENCY_ID);
        String stopId = document.get(StopSearchIndexConstants.FIELD_STOP_ID);
        AgencyAndId id = new AgencyAndId(agencyId, stopId);
        Float existingScore = topScores.get(id);
        if (existingScore == null || existingScore < sd.score)
            topScores.put(id, sd.score);
    }
    List<AgencyAndId> ids = new ArrayList<AgencyAndId>(top.totalHits);
    double[] scores = new double[top.totalHits];
    int index = 0;
    for (AgencyAndId id : topScores.keySet()) {
        ids.add(id);
        scores[index] = topScores.get(id);
        index++;
    }
    return new SearchResult<AgencyAndId>(ids, scores);
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) Query(org.apache.lucene.search.Query) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SearchResult(org.onebusaway.transit_data_federation.model.SearchResult) Document(org.apache.lucene.document.Document) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) TopDocCollector(org.apache.lucene.search.TopDocCollector)

Example 4 with TopDocCollector

use of org.apache.lucene.search.TopDocCollector in project onebusaway-application-modules by camsys.

the class RouteCollectionSearchServiceImpl method search.

private SearchResult<AgencyAndId> search(QueryParser parser, String value, int maxResultCount, double minScoreToKeep) throws IOException, ParseException {
    if (_searcher == null)
        return new SearchResult<AgencyAndId>();
    TopDocCollector collector = new TopDocCollector(maxResultCount);
    Query query = parser.parse(value);
    _searcher.search(query, collector);
    TopDocs top = collector.topDocs();
    Map<AgencyAndId, Float> topScores = new HashMap<AgencyAndId, Float>();
    String lowerCaseQueryValue = value.toLowerCase();
    for (ScoreDoc sd : top.scoreDocs) {
        Document document = _searcher.doc(sd.doc);
        String routeShortName = document.get(RouteCollectionSearchIndexConstants.FIELD_ROUTE_SHORT_NAME);
        Set<String> tokens = new HashSet<String>();
        if (routeShortName != null) {
            for (String token : routeShortName.toLowerCase().split("\\b")) {
                if (!token.isEmpty())
                    tokens.add(token);
            }
        }
        // Result must have a minimum score to qualify
        if (sd.score < minScoreToKeep && !tokens.contains(lowerCaseQueryValue))
            continue;
        // Keep the best score for a particular id
        String agencyId = document.get(RouteCollectionSearchIndexConstants.FIELD_ROUTE_COLLECTION_AGENCY_ID);
        String id = document.get(RouteCollectionSearchIndexConstants.FIELD_ROUTE_COLLECTION_ID);
        AgencyAndId routeId = new AgencyAndId(agencyId, id);
        Float score = topScores.get(routeId);
        if (score == null || score < sd.score)
            topScores.put(routeId, sd.score);
    }
    List<AgencyAndId> ids = new ArrayList<AgencyAndId>(topScores.size());
    double[] scores = new double[topScores.size()];
    int index = 0;
    for (AgencyAndId id : topScores.keySet()) {
        ids.add(id);
        scores[index] = topScores.get(id);
        index++;
    }
    return new SearchResult<AgencyAndId>(ids, scores);
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) Query(org.apache.lucene.search.Query) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SearchResult(org.onebusaway.transit_data_federation.model.SearchResult) Document(org.apache.lucene.document.Document) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) TopDocCollector(org.apache.lucene.search.TopDocCollector) HashSet(java.util.HashSet)

Example 5 with TopDocCollector

use of org.apache.lucene.search.TopDocCollector in project bigbluebutton by bigbluebutton.

the class Search method doPagingSearch.

private TopDocCollector doPagingSearch(Query query, int startFrom) throws IOException {
    TopDocCollector collector = new TopDocCollector(startFrom + 10);
    searcher.search(query, collector);
    if (logger.isDebugEnabled()) {
        logger.debug(query.toString());
        logger.debug(collector.toString());
    }
    return collector;
}
Also used : TopDocCollector(org.apache.lucene.search.TopDocCollector)

Aggregations

TopDocCollector (org.apache.lucene.search.TopDocCollector)5 HashMap (java.util.HashMap)3 Document (org.apache.lucene.document.Document)3 Query (org.apache.lucene.search.Query)3 ScoreDoc (org.apache.lucene.search.ScoreDoc)3 TopDocs (org.apache.lucene.search.TopDocs)3 ArrayList (java.util.ArrayList)2 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)2 SearchResult (org.onebusaway.transit_data_federation.model.SearchResult)2 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 LinkedHashMap (java.util.LinkedHashMap)1 ParseException (org.apache.lucene.queryParser.ParseException)1 QueryParser (org.apache.lucene.queryParser.QueryParser)1 Searcher (org.apache.lucene.search.Searcher)1 TopFieldDocs (org.apache.lucene.search.TopFieldDocs)1 Search (org.bigbluebutton.webminer.search.Search)1 MatchVO (org.bigbluebutton.webminer.web.model.MatchVO)1 SearchCriteriaCommand (org.bigbluebutton.webminer.web.model.SearchCriteriaCommand)1