Search in sources :

Example 1 with LuceneSearcher

use of org.alfresco.repo.search.impl.lucene.LuceneSearcher in project alfresco-repository by Alfresco.

the class LuceneQueryEngine method executeQuery.

public QueryEngineResults executeQuery(Query query, QueryOptions options, FunctionEvaluationContext functionContext) {
    Set<String> selectorGroup = null;
    if (query.getSource() != null) {
        List<Set<String>> selectorGroups = query.getSource().getSelectorGroups(functionContext);
        if (selectorGroups.size() == 0) {
            throw new UnsupportedOperationException("No selectors");
        }
        if (selectorGroups.size() > 1) {
            throw new UnsupportedOperationException("Advanced join is not supported");
        }
        selectorGroup = selectorGroups.get(0);
    }
    SearchParameters searchParameters = new SearchParameters();
    if (options.getLocales().size() > 0) {
        for (Locale locale : options.getLocales()) {
            searchParameters.addLocale(locale);
        }
    }
    searchParameters.excludeDataInTheCurrentTransaction(!options.isIncludeInTransactionData());
    searchParameters.setSkipCount(options.getSkipCount());
    searchParameters.setMaxPermissionChecks(options.getMaxPermissionChecks());
    searchParameters.setMaxPermissionCheckTimeMillis(options.getMaxPermissionCheckTimeMillis());
    searchParameters.setDefaultFieldName(options.getDefaultFieldName());
    searchParameters.setMlAnalaysisMode(options.getMlAnalaysisMode());
    if (options.getMaxItems() >= 0) {
        searchParameters.setLimitBy(LimitBy.FINAL_SIZE);
        searchParameters.setLimit(options.getMaxItems());
        searchParameters.setMaxItems(options.getMaxItems());
    } else {
        searchParameters.setLimitBy(LimitBy.UNLIMITED);
    }
    searchParameters.setUseInMemorySort(options.getUseInMemorySort());
    searchParameters.setMaxRawResultSetSizeForInMemorySort(options.getMaxRawResultSetSizeForInMemorySort());
    searchParameters.setBulkFetchEnabled(options.isBulkFetchEnabled());
    searchParameters.setQueryConsistency(options.getQueryConsistency());
    try {
        StoreRef storeRef = options.getStores().get(0);
        searchParameters.addStore(storeRef);
        if (query instanceof LuceneQueryBuilder) {
            SearchService searchService = indexAndSearcher.getSearcher(storeRef, options.isIncludeInTransactionData());
            if (searchService instanceof LuceneSearcher) {
                LuceneSearcher luceneSearcher = (LuceneSearcher) searchService;
                ClosingIndexSearcher searcher = luceneSearcher.getClosingIndexSearcher();
                LuceneQueryBuilderContext<org.apache.lucene.search.Query, Sort, ParseException> luceneContext = new LuceneQueryBuilderContextImpl(dictionaryService, namespaceService, tenantService, searchParameters, indexAndSearcher.getDefaultMLSearchAnalysisMode(), searcher.getIndexReader());
                @SuppressWarnings("unchecked") LuceneQueryBuilder<org.apache.lucene.search.Query, Sort, ParseException> builder = (LuceneQueryBuilder<org.apache.lucene.search.Query, Sort, ParseException>) query;
                org.apache.lucene.search.Query luceneQuery = builder.buildQuery(selectorGroup, luceneContext, functionContext);
                if (logger.isDebugEnabled()) {
                    logger.debug("Executing lucene query: " + luceneQuery);
                }
                Sort sort = builder.buildSort(selectorGroup, luceneContext, functionContext);
                Hits hits = searcher.search(luceneQuery);
                boolean postSort = false;
                ;
                if (sort != null) {
                    postSort = searchParameters.usePostSort(hits.length(), useInMemorySort, maxRawResultSetSizeForInMemorySort);
                    if (postSort == false) {
                        hits = searcher.search(luceneQuery, sort);
                    }
                }
                ResultSet answer;
                ResultSet result = new LuceneResultSet(hits, searcher, nodeService, tenantService, searchParameters, indexAndSearcher);
                if (postSort) {
                    if (sort != null) {
                        for (SortField sf : sort.getSort()) {
                            searchParameters.addSort(sf.getField(), !sf.getReverse());
                        }
                    }
                    ResultSet sorted = new SortedResultSet(result, nodeService, builder.buildSortDefinitions(selectorGroup, luceneContext, functionContext), namespaceService, dictionaryService, searchParameters.getSortLocale());
                    answer = sorted;
                } else {
                    answer = result;
                }
                ResultSet rs = new PagingLuceneResultSet(answer, searchParameters, nodeService);
                Map<Set<String>, ResultSet> map = new HashMap<Set<String>, ResultSet>(1);
                map.put(selectorGroup, rs);
                return new QueryEngineResults(map);
            } else {
                throw new UnsupportedOperationException();
            }
        } else {
            throw new UnsupportedOperationException();
        }
    } catch (ParseException e) {
        throw new SearcherException("Failed to parse query: " + e);
    } catch (IOException e) {
        throw new SearcherException("IO exception during search", e);
    }
}
Also used : Locale(java.util.Locale) QueryEngineResults(org.alfresco.repo.search.impl.querymodel.QueryEngineResults) Hits(org.apache.lucene.search.Hits) PagingLuceneResultSet(org.alfresco.repo.search.impl.lucene.PagingLuceneResultSet) ResultSet(org.alfresco.service.cmr.search.ResultSet) Set(java.util.Set) SortedResultSet(org.alfresco.repo.search.results.SortedResultSet) LuceneResultSet(org.alfresco.repo.search.impl.lucene.LuceneResultSet) Query(org.alfresco.repo.search.impl.querymodel.Query) HashMap(java.util.HashMap) SortField(org.apache.lucene.search.SortField) SearchParameters(org.alfresco.service.cmr.search.SearchParameters) SearcherException(org.alfresco.repo.search.SearcherException) SearchService(org.alfresco.service.cmr.search.SearchService) PagingLuceneResultSet(org.alfresco.repo.search.impl.lucene.PagingLuceneResultSet) ResultSet(org.alfresco.service.cmr.search.ResultSet) SortedResultSet(org.alfresco.repo.search.results.SortedResultSet) LuceneResultSet(org.alfresco.repo.search.impl.lucene.LuceneResultSet) Sort(org.apache.lucene.search.Sort) PagingLuceneResultSet(org.alfresco.repo.search.impl.lucene.PagingLuceneResultSet) PagingLuceneResultSet(org.alfresco.repo.search.impl.lucene.PagingLuceneResultSet) LuceneResultSet(org.alfresco.repo.search.impl.lucene.LuceneResultSet) StoreRef(org.alfresco.service.cmr.repository.StoreRef) SortedResultSet(org.alfresco.repo.search.results.SortedResultSet) IOException(java.io.IOException) LuceneSearcher(org.alfresco.repo.search.impl.lucene.LuceneSearcher) ClosingIndexSearcher(org.alfresco.repo.search.impl.lucene.ClosingIndexSearcher) ParseException(org.apache.lucene.queryParser.ParseException)

Aggregations

IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Locale (java.util.Locale)1 Set (java.util.Set)1 SearcherException (org.alfresco.repo.search.SearcherException)1 ClosingIndexSearcher (org.alfresco.repo.search.impl.lucene.ClosingIndexSearcher)1 LuceneResultSet (org.alfresco.repo.search.impl.lucene.LuceneResultSet)1 LuceneSearcher (org.alfresco.repo.search.impl.lucene.LuceneSearcher)1 PagingLuceneResultSet (org.alfresco.repo.search.impl.lucene.PagingLuceneResultSet)1 Query (org.alfresco.repo.search.impl.querymodel.Query)1 QueryEngineResults (org.alfresco.repo.search.impl.querymodel.QueryEngineResults)1 SortedResultSet (org.alfresco.repo.search.results.SortedResultSet)1 StoreRef (org.alfresco.service.cmr.repository.StoreRef)1 ResultSet (org.alfresco.service.cmr.search.ResultSet)1 SearchParameters (org.alfresco.service.cmr.search.SearchParameters)1 SearchService (org.alfresco.service.cmr.search.SearchService)1 ParseException (org.apache.lucene.queryParser.ParseException)1 Hits (org.apache.lucene.search.Hits)1 Sort (org.apache.lucene.search.Sort)1 SortField (org.apache.lucene.search.SortField)1