Search in sources :

Example 1 with LuceneQueryException

use of org.apache.geode.cache.lucene.LuceneQueryException in project geode by apache.

the class StringQueryProvider method getQuery.

@Override
public synchronized Query getQuery(LuceneIndex index) throws LuceneQueryException {
    if (luceneQuery == null) {
        String[] fields = index.getFieldNames();
        LuceneIndexImpl indexImpl = (LuceneIndexImpl) index;
        StandardQueryParser parser = new StandardQueryParser(indexImpl.getAnalyzer());
        parser.setAllowLeadingWildcard(true);
        try {
            luceneQuery = parser.parse(query, defaultField);
            if (logger.isDebugEnabled()) {
                logger.debug("User query " + query + " is parsed to be: " + luceneQuery);
            }
        } catch (QueryNodeException e) {
            logger.warn("Caught the following exception attempting parse query '" + query + "': ", e);
            throw new LuceneQueryException(LocalizedStrings.StringQueryProvider_PARSING_QUERY_0_FAILED_DUE_TO_1.toLocalizedString("'" + query + "'", e.getMessage()));
        }
    }
    return luceneQuery;
}
Also used : QueryNodeException(org.apache.lucene.queryparser.flexible.core.QueryNodeException) LuceneQueryException(org.apache.geode.cache.lucene.LuceneQueryException) StandardQueryParser(org.apache.lucene.queryparser.flexible.standard.StandardQueryParser)

Example 2 with LuceneQueryException

use of org.apache.geode.cache.lucene.LuceneQueryException in project geode by apache.

the class LuceneQueryImpl method findTopEntries.

private TopEntries<K> findTopEntries() throws LuceneQueryException {
    TopEntriesCollectorManager manager = new TopEntriesCollectorManager(null, limit);
    LuceneFunctionContext<TopEntriesCollector> context = new LuceneFunctionContext<>(query, indexName, manager, limit);
    // TODO provide a timeout to the user?
    TopEntries<K> entries = null;
    try {
        TopEntriesFunctionCollector collector = new TopEntriesFunctionCollector(context);
        ResultCollector<TopEntriesCollector, TopEntries<K>> rc = onRegion().setArguments(context).withCollector(collector).execute(LuceneQueryFunction.ID);
        entries = rc.getResult();
    } catch (FunctionException e) {
        if (e.getCause() instanceof LuceneQueryException) {
            throw new LuceneQueryException(e);
        } else if (e.getCause() instanceof TransactionException) {
            // When run from client with single hop disabled
            throw new LuceneQueryException(LUCENE_QUERY_CANNOT_BE_EXECUTED_WITHIN_A_TRANSACTION);
        } else if (e.getCause() instanceof RuntimeException) {
            throw (RuntimeException) e.getCause();
        }
        throw e;
    } catch (TransactionException e) {
        // When function execution is run from server
        throw new LuceneQueryException(LUCENE_QUERY_CANNOT_BE_EXECUTED_WITHIN_A_TRANSACTION);
    }
    return entries;
}
Also used : LuceneFunctionContext(org.apache.geode.cache.lucene.internal.distributed.LuceneFunctionContext) TopEntriesCollector(org.apache.geode.cache.lucene.internal.distributed.TopEntriesCollector) LuceneQueryException(org.apache.geode.cache.lucene.LuceneQueryException) TopEntriesFunctionCollector(org.apache.geode.cache.lucene.internal.distributed.TopEntriesFunctionCollector) FunctionException(org.apache.geode.cache.execute.FunctionException) TransactionException(org.apache.geode.cache.TransactionException) TopEntriesCollectorManager(org.apache.geode.cache.lucene.internal.distributed.TopEntriesCollectorManager) TopEntries(org.apache.geode.cache.lucene.internal.distributed.TopEntries)

Example 3 with LuceneQueryException

use of org.apache.geode.cache.lucene.LuceneQueryException in project geode by apache.

the class LuceneSearchIndexFunction method execute.

public void execute(final FunctionContext context) {
    Set<LuceneSearchResults> result = new HashSet<>();
    final Cache cache = getCache();
    final LuceneQueryInfo queryInfo = (LuceneQueryInfo) context.getArguments();
    LuceneService luceneService = LuceneServiceProvider.get(getCache());
    try {
        if (luceneService.getIndex(queryInfo.getIndexName(), queryInfo.getRegionPath()) == null) {
            throw new Exception("Index " + queryInfo.getIndexName() + " not found on region " + queryInfo.getRegionPath());
        }
        final LuceneQuery<K, V> query = luceneService.createLuceneQueryFactory().setLimit(queryInfo.getLimit()).create(queryInfo.getIndexName(), queryInfo.getRegionPath(), queryInfo.getQueryString(), queryInfo.getDefaultField());
        if (queryInfo.getKeysOnly()) {
            query.findKeys().forEach(key -> result.add(new LuceneSearchResults(key.toString())));
        } else {
            PageableLuceneQueryResults pageableLuceneQueryResults = query.findPages();
            while (pageableLuceneQueryResults.hasNext()) {
                List<LuceneResultStruct> page = pageableLuceneQueryResults.next();
                page.stream().forEach(searchResult -> result.add(new LuceneSearchResults<K, V>(searchResult.getKey().toString(), searchResult.getValue().toString(), searchResult.getScore())));
            }
        }
        if (result != null) {
            context.getResultSender().lastResult(result);
        }
    } catch (LuceneQueryException e) {
        result.add(new LuceneSearchResults(true, e.getRootCause().getMessage()));
        context.getResultSender().lastResult(result);
    } catch (Exception e) {
        result.add(new LuceneSearchResults(true, e.getMessage()));
        context.getResultSender().lastResult(result);
    }
}
Also used : LuceneSearchResults(org.apache.geode.cache.lucene.internal.cli.LuceneSearchResults) LuceneQueryException(org.apache.geode.cache.lucene.LuceneQueryException) LuceneQueryException(org.apache.geode.cache.lucene.LuceneQueryException) LuceneQueryInfo(org.apache.geode.cache.lucene.internal.cli.LuceneQueryInfo) LuceneService(org.apache.geode.cache.lucene.LuceneService) LuceneResultStruct(org.apache.geode.cache.lucene.LuceneResultStruct) PageableLuceneQueryResults(org.apache.geode.cache.lucene.PageableLuceneQueryResults) HashSet(java.util.HashSet) Cache(org.apache.geode.cache.Cache)

Aggregations

LuceneQueryException (org.apache.geode.cache.lucene.LuceneQueryException)3 HashSet (java.util.HashSet)1 Cache (org.apache.geode.cache.Cache)1 TransactionException (org.apache.geode.cache.TransactionException)1 FunctionException (org.apache.geode.cache.execute.FunctionException)1 LuceneResultStruct (org.apache.geode.cache.lucene.LuceneResultStruct)1 LuceneService (org.apache.geode.cache.lucene.LuceneService)1 PageableLuceneQueryResults (org.apache.geode.cache.lucene.PageableLuceneQueryResults)1 LuceneQueryInfo (org.apache.geode.cache.lucene.internal.cli.LuceneQueryInfo)1 LuceneSearchResults (org.apache.geode.cache.lucene.internal.cli.LuceneSearchResults)1 LuceneFunctionContext (org.apache.geode.cache.lucene.internal.distributed.LuceneFunctionContext)1 TopEntries (org.apache.geode.cache.lucene.internal.distributed.TopEntries)1 TopEntriesCollector (org.apache.geode.cache.lucene.internal.distributed.TopEntriesCollector)1 TopEntriesCollectorManager (org.apache.geode.cache.lucene.internal.distributed.TopEntriesCollectorManager)1 TopEntriesFunctionCollector (org.apache.geode.cache.lucene.internal.distributed.TopEntriesFunctionCollector)1 QueryNodeException (org.apache.lucene.queryparser.flexible.core.QueryNodeException)1 StandardQueryParser (org.apache.lucene.queryparser.flexible.standard.StandardQueryParser)1