Search in sources :

Example 1 with Function

use of com.tuplejump.stargate.lucene.query.function.Function in project stargate-core by tuplejump.

the class SearchSupport method getRows.

protected List<Row> getRows(final ExtendedFilter filter, final Search search, final String queryString) {
    final SearchSupport searchSupport = this;
    AbstractBounds<RowPosition> keyRange = filter.dataRange.keyRange();
    final Range<Token> filterRange = new Range<>(keyRange.left.getToken(), keyRange.right.getToken());
    final boolean isSingleToken = filterRange.left.equals(filterRange.right);
    final boolean isFullRange = isSingleToken && baseCfs.partitioner.getMinimumToken().equals(filterRange.left);
    final boolean shouldSaveToCache = isPagingQuery(filter.dataRange);
    final boolean shouldRetrieveFromCache = shouldSaveToCache && !isFirstPage((DataRange.Paging) filter.dataRange);
    SearcherCallback<List<Row>> sc = new SearcherCallback<List<Row>>() {

        @Override
        public List<Row> doWithSearcher(org.apache.lucene.search.IndexSearcher searcher) throws Exception {
            Utils.SimpleTimer timer = Utils.getStartedTimer(logger);
            List<Row> results;
            if (search == null) {
                results = new ArrayList<>();
            } else {
                Utils.SimpleTimer timer2 = Utils.getStartedTimer(SearchSupport.logger);
                Function function = search.function();
                Query query = LuceneUtils.getQueryUpdatedWithPKCondition(search.query(options), getPartitionKeyString(filter));
                int resultsLimit = searcher.getIndexReader().maxDoc();
                if (resultsLimit == 0) {
                    resultsLimit = 1;
                }
                function.init(options);
                IndexEntryCollector collector = null;
                if (shouldRetrieveFromCache) {
                    collector = currentIndex.collectorMap.get(queryString);
                }
                if (collector == null) {
                    collector = new IndexEntryCollector(tableMapper, search, options, resultsLimit);
                    searcher.search(query, collector);
                    if (shouldSaveToCache) {
                        currentIndex.collectorMap.put(queryString, collector);
                    }
                    if (logger.isInfoEnabled()) {
                        logger.info("Adding collector to cache");
                    }
                } else if (logger.isInfoEnabled()) {
                    logger.info("Found collector in cache");
                }
                timer2.endLogTime("Lucene search for [" + collector.getTotalHits() + "] results ");
                if (SearchSupport.logger.isDebugEnabled()) {
                    SearchSupport.logger.debug(String.format("Search results [%s]", collector.getTotalHits()));
                }
                ResultMapper iter = new ResultMapper(tableMapper, searchSupport, filter, collector, function.shouldTryScoring() && search.isShowScore());
                Utils.SimpleTimer timer3 = Utils.getStartedTimer(SearchSupport.logger);
                results = function.process(iter, baseCfs, currentIndex);
                timer3.endLogTime("Aggregation [" + results.size() + "] results");
            }
            timer.endLogTime("Search with results [" + results.size() + "] ");
            return results;
        }

        @Override
        public Range<Token> filterRange() {
            return filterRange;
        }

        @Override
        public boolean isSingleToken() {
            return isSingleToken;
        }

        @Override
        public boolean isFullRange() {
            return isFullRange;
        }
    };
    return currentIndex.search(sc);
}
Also used : SecondaryIndexSearcher(org.apache.cassandra.db.index.SecondaryIndexSearcher) Query(org.apache.lucene.search.Query) Token(org.apache.cassandra.dht.Token) Range(org.apache.cassandra.dht.Range) IndexEntryCollector(com.tuplejump.stargate.lucene.IndexEntryCollector) Function(com.tuplejump.stargate.lucene.query.function.Function) StringEscapeUtils(org.apache.commons.lang3.StringEscapeUtils) Utils(com.tuplejump.stargate.Utils) LuceneUtils(com.tuplejump.stargate.lucene.LuceneUtils) SearcherCallback(com.tuplejump.stargate.lucene.SearcherCallback)

Aggregations

Utils (com.tuplejump.stargate.Utils)1 IndexEntryCollector (com.tuplejump.stargate.lucene.IndexEntryCollector)1 LuceneUtils (com.tuplejump.stargate.lucene.LuceneUtils)1 SearcherCallback (com.tuplejump.stargate.lucene.SearcherCallback)1 Function (com.tuplejump.stargate.lucene.query.function.Function)1 SecondaryIndexSearcher (org.apache.cassandra.db.index.SecondaryIndexSearcher)1 Range (org.apache.cassandra.dht.Range)1 Token (org.apache.cassandra.dht.Token)1 StringEscapeUtils (org.apache.commons.lang3.StringEscapeUtils)1 Query (org.apache.lucene.search.Query)1