Search in sources :

Example 1 with Searcher

use of org.apache.lucene.search.Searcher in project graphdb by neo4j-attic.

the class LuceneIndex method query.

protected IndexHits<T> query(Query query, String keyForDirectLookup, Object valueForDirectLookup, QueryContext additionalParametersOrNull) {
    List<Long> ids = new ArrayList<Long>();
    LuceneXaConnection con = getReadOnlyConnection();
    LuceneTransaction luceneTx = con != null ? con.getLuceneTx() : null;
    Collection<Long> removedIds = Collections.emptySet();
    Searcher additionsSearcher = null;
    if (luceneTx != null) {
        if (keyForDirectLookup != null) {
            ids.addAll(luceneTx.getAddedIds(this, keyForDirectLookup, valueForDirectLookup));
        } else {
            additionsSearcher = luceneTx.getAdditionsAsSearcher(this, additionalParametersOrNull);
        }
        removedIds = keyForDirectLookup != null ? luceneTx.getRemovedIds(this, keyForDirectLookup, valueForDirectLookup) : luceneTx.getRemovedIds(this, query);
    }
    service.dataSource().getReadLock();
    IndexHits<Long> idIterator = null;
    IndexSearcherRef searcher = null;
    try {
        searcher = service.dataSource().getIndexSearcher(identifier, true);
        if (searcher != null) {
            boolean foundInCache = false;
            LruCache<String, Collection<Long>> cachedIdsMap = null;
            if (keyForDirectLookup != null) {
                cachedIdsMap = service.dataSource().getFromCache(identifier, keyForDirectLookup);
                foundInCache = fillFromCache(cachedIdsMap, ids, keyForDirectLookup, valueForDirectLookup.toString(), removedIds);
            }
            if (!foundInCache) {
                DocToIdIterator searchedIds = new DocToIdIterator(search(searcher, query, additionalParametersOrNull, additionsSearcher, removedIds), removedIds, searcher);
                if (ids.isEmpty()) {
                    idIterator = searchedIds;
                } else {
                    Collection<IndexHits<Long>> iterators = new ArrayList<IndexHits<Long>>();
                    iterators.add(searchedIds);
                    iterators.add(new ConstantScoreIterator<Long>(ids, Float.NaN));
                    idIterator = new CombinedIndexHits<Long>(iterators);
                }
            }
        }
    } finally {
        // The DocToIdIterator closes the IndexSearchRef instance anyways,
        // or the LazyIterator if it's a lazy one. So no need here.
        service.dataSource().releaseReadLock();
    }
    idIterator = idIterator == null ? new ConstantScoreIterator<Long>(ids, 0) : idIterator;
    return new IdToEntityIterator<T>(idIterator) {

        @Override
        protected T underlyingObjectToObject(Long id) {
            return getById(id);
        }

        protected void itemDodged(Long item) {
            abandonedIds.add(item);
        }
    };
}
Also used : MultiSearcher(org.apache.lucene.search.MultiSearcher) Searcher(org.apache.lucene.search.Searcher) ArrayList(java.util.ArrayList) IndexHits(org.neo4j.graphdb.index.IndexHits) Collection(java.util.Collection)

Example 2 with Searcher

use of org.apache.lucene.search.Searcher 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 3 with Searcher

use of org.apache.lucene.search.Searcher in project zm-mailbox by Zimbra.

the class RemoteMailQueue method search0.

private void search0(SearchResult result, IndexReader indexReader, Query query, int offset, int limit) throws IOException {
    if (ZimbraLog.rmgmt.isDebugEnabled()) {
        ZimbraLog.rmgmt.debug("searching query=" + query + " offset=" + offset + " limit=" + limit + " " + this);
    }
    Searcher searcher = null;
    try {
        searcher = new IndexSearcher(indexReader);
        TopDocs topDocs = searcher.search(query, (Filter) null, limit);
        ScoreDoc[] hits = topDocs.scoreDocs;
        if (offset < hits.length) {
            int n;
            if (limit <= 0) {
                n = hits.length;
            } else {
                n = Math.min(offset + limit, hits.length);
            }
            for (int i = offset; i < n; i++) {
                Document doc = searcher.doc(hits[i].doc);
                Map<QueueAttr, String> qitem = docToQueueItem(doc);
                result.qitems.add(qitem);
            }
        }
        result.hits = hits.length;
    } finally {
        if (searcher != null) {
            searcher.close();
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TopDocs(org.apache.lucene.search.TopDocs) Searcher(org.apache.lucene.search.Searcher) IndexSearcher(org.apache.lucene.search.IndexSearcher) Document(org.apache.lucene.document.Document) ScoreDoc(org.apache.lucene.search.ScoreDoc)

Example 4 with Searcher

use of org.apache.lucene.search.Searcher in project graphdb by neo4j-attic.

the class LuceneIndex method search.

private IndexHits<Document> search(IndexSearcherRef searcherRef, Query query, QueryContext additionalParametersOrNull, Searcher additionsSearcher, Collection<Long> removed) {
    try {
        if (additionsSearcher != null && !removed.isEmpty()) {
            letThroughAdditions(additionsSearcher, query, removed);
        }
        Searcher searcher = additionsSearcher == null ? searcherRef.getSearcher() : new MultiSearcher(searcherRef.getSearcher(), additionsSearcher);
        IndexHits<Document> result = null;
        if (additionalParametersOrNull != null && additionalParametersOrNull.getTop() > 0) {
            result = new TopDocsIterator(query, additionalParametersOrNull, searcher);
        } else {
            Sort sorting = additionalParametersOrNull != null ? additionalParametersOrNull.getSorting() : null;
            boolean forceScore = additionalParametersOrNull == null || !additionalParametersOrNull.getTradeCorrectnessForSpeed();
            Hits hits = new Hits(searcher, query, null, sorting, forceScore);
            result = new HitsIterator(hits);
        }
        return result;
    } catch (IOException e) {
        throw new RuntimeException("Unable to query " + this + " with " + query, e);
    }
}
Also used : MultiSearcher(org.apache.lucene.search.MultiSearcher) IndexHits(org.neo4j.graphdb.index.IndexHits) MultiSearcher(org.apache.lucene.search.MultiSearcher) Searcher(org.apache.lucene.search.Searcher) Sort(org.apache.lucene.search.Sort) IOException(java.io.IOException) Document(org.apache.lucene.document.Document)

Example 5 with Searcher

use of org.apache.lucene.search.Searcher in project greplin-lucene-utils by Cue.

the class ConstantQueryNormBoostingQuery method rewrite.

@Override
public Query rewrite(final IndexReader reader) throws IOException {
    BooleanQuery result = new BooleanQuery() {

        @Override
        public Similarity getSimilarity(final Searcher searcher) {
            final Similarity base = searcher.getSimilarity();
            return new DefaultSimilarity() {

                @Override
                public float queryNorm(final float sumOfSquaredWeights) {
                    return base.queryNorm(sumOfSquaredWeights);
                }

                @Override
                public float coord(final int overlap, final int max) {
                    switch(overlap) {
                        case // matched only one clause
                        1:
                            return 1.0f;
                        case // matched both clauses
                        2:
                            return ConstantQueryNormBoostingQuery.this.boost;
                        default:
                            return 0.0f;
                    }
                }
            };
        }
    };
    result.add(this.match, BooleanClause.Occur.MUST);
    result.add(this.context, BooleanClause.Occur.SHOULD);
    return result;
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) Similarity(org.apache.lucene.search.Similarity) DefaultSimilarity(org.apache.lucene.search.DefaultSimilarity) Searcher(org.apache.lucene.search.Searcher) DefaultSimilarity(org.apache.lucene.search.DefaultSimilarity)

Aggregations

Searcher (org.apache.lucene.search.Searcher)5 Document (org.apache.lucene.document.Document)3 MultiSearcher (org.apache.lucene.search.MultiSearcher)2 ScoreDoc (org.apache.lucene.search.ScoreDoc)2 TopDocs (org.apache.lucene.search.TopDocs)2 IndexHits (org.neo4j.graphdb.index.IndexHits)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 LinkedHashMap (java.util.LinkedHashMap)1 BooleanQuery (org.apache.lucene.search.BooleanQuery)1 DefaultSimilarity (org.apache.lucene.search.DefaultSimilarity)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1 Similarity (org.apache.lucene.search.Similarity)1 Sort (org.apache.lucene.search.Sort)1 TopDocCollector (org.apache.lucene.search.TopDocCollector)1 TopFieldDocs (org.apache.lucene.search.TopFieldDocs)1 Search (org.bigbluebutton.webminer.search.Search)1