Search in sources :

Example 6 with ServiceNotAvailableException

use of org.olat.search.ServiceNotAvailableException in project openolat by klemens.

the class SearchCallable method call.

@Override
public SearchResults call() throws ParseException {
    IndexSearcher searcher = null;
    try {
        boolean debug = log.isDebug();
        if (!searchService.existIndex()) {
            log.warn("Index does not exist, can't search for queryString: " + queryString);
            throw new ServiceNotAvailableException("Index does not exist");
        }
        if (debug)
            log.debug("queryString=" + queryString);
        searcher = searchService.getIndexSearcher();
        BooleanQuery query = searchService.createQuery(queryString, condQueries);
        if (debug)
            log.debug("query=" + query);
        if (Thread.interrupted()) {
            throw new InterruptedException();
        }
        long startTime = System.currentTimeMillis();
        int n = SearchServiceFactory.getService().getSearchModuleConfig().getMaxHits();
        TopDocs docs = searcher.search(query, n);
        long queryTime = System.currentTimeMillis() - startTime;
        if (debug)
            log.debug("hits.length()=" + docs.totalHits);
        SearchResultsImpl searchResult = new SearchResultsImpl(searchService.getMainIndexer(), searcher, docs, query, searchService.getAnalyzer(), identity, roles, firstResult, maxResults, doHighlighting, false);
        searchResult.setQueryTime(queryTime);
        searchResult.setNumberOfIndexDocuments(docs.totalHits);
        if (debug)
            log.debug("found=" + docs.totalHits);
        return searchResult;
    } catch (ParseException pex) {
        throw pex;
    } catch (Exception naex) {
        log.error("", naex);
        return null;
    } finally {
        searchService.releaseIndexSearcher(searcher);
        DBFactory.getInstance().commitAndCloseSession();
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) ServiceNotAvailableException(org.olat.search.ServiceNotAvailableException) TopDocs(org.apache.lucene.search.TopDocs) BooleanQuery(org.apache.lucene.search.BooleanQuery) SearchResultsImpl(org.olat.search.service.searcher.SearchResultsImpl) ParseException(org.apache.lucene.queryparser.classic.ParseException) ParseException(org.apache.lucene.queryparser.classic.ParseException) ServiceNotAvailableException(org.olat.search.ServiceNotAvailableException)

Example 7 with ServiceNotAvailableException

use of org.olat.search.ServiceNotAvailableException in project openolat by klemens.

the class SearchOrderByCallable method call.

@Override
public List<Long> call() {
    IndexSearcher searcher = null;
    int found = -1;
    try {
        if (!searchService.existIndex()) {
            log.warn("Index does not exist, can't search for queryString: " + queryString);
            throw new ServiceNotAvailableException("Index does not exist");
        }
        searcher = searchService.getIndexSearcher();
        BooleanQuery query = searchService.createQuery(queryString, condQueries);
        // only search document with an primary key
        String idNotNull = AbstractOlatDocument.DB_ID_NAME + ":[* TO *]";
        QueryParser idQueryParser = new QueryParser(SearchService.OO_LUCENE_VERSION, idNotNull, searchService.getAnalyzer());
        Query idQuery = idQueryParser.parse(idNotNull);
        query.add(idQuery, Occur.MUST);
        int n = searchService.getSearchModuleConfig().getMaxHits();
        TopDocs docs;
        if (orderBy != null && orderBy.length > 0 && orderBy[0] != null) {
            SortField[] sortFields = new SortField[orderBy.length];
            for (int i = 0; i < orderBy.length; i++) {
                sortFields[i] = new SortField(orderBy[i].getKey(), SortField.Type.STRING_VAL, orderBy[i].isAsc());
            }
            Sort sort = new Sort(sortFields);
            docs = searcher.search(query, n, sort);
        } else {
            docs = searcher.search(query, n);
        }
        int numOfDocs = Math.min(n, docs.totalHits);
        Set<String> retrievedFields = new HashSet<String>();
        retrievedFields.add(AbstractOlatDocument.DB_ID_NAME);
        List<Long> res = new ArrayList<Long>();
        for (int i = firstResult; i < numOfDocs && res.size() < maxResults; i++) {
            Document doc = searcher.doc(docs.scoreDocs[i].doc, retrievedFields);
            String dbKeyStr = doc.get(AbstractOlatDocument.DB_ID_NAME);
            if (StringHelper.containsNonWhitespace(dbKeyStr)) {
                res.add(Long.parseLong(dbKeyStr));
            }
        }
        found = res.size();
        return res;
    } catch (Exception naex) {
        log.error("", naex);
        return null;
    } finally {
        searchService.releaseIndexSearcher(searcher);
        DBFactory.getInstance().commitAndCloseSession();
        log.info("queryString=" + queryString + " (" + found + " hits)");
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) ServiceNotAvailableException(org.olat.search.ServiceNotAvailableException) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) BooleanQuery(org.apache.lucene.search.BooleanQuery) ArrayList(java.util.ArrayList) SortField(org.apache.lucene.search.SortField) Document(org.apache.lucene.document.Document) AbstractOlatDocument(org.olat.search.model.AbstractOlatDocument) ServiceNotAvailableException(org.olat.search.ServiceNotAvailableException) TopDocs(org.apache.lucene.search.TopDocs) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) Sort(org.apache.lucene.search.Sort) HashSet(java.util.HashSet)

Example 8 with ServiceNotAvailableException

use of org.olat.search.ServiceNotAvailableException in project openolat by klemens.

the class SearchClientProxy method doSearch.

/**
 * Uses Request/reply mechanism for synchronous operation.
 * @see org.olat.search.service.searcher.OLATSearcher#doSearch(java.lang.String, org.olat.core.id.Identity, org.olat.core.id.Roles, boolean)
 */
@Override
public SearchResults doSearch(String queryString, List<String> condQueries, Identity identity, Roles roles, int firstResult, int maxResults, boolean doHighlighting) throws ServiceNotAvailableException, ParseException, QueryException {
    boolean isDebug = log.isDebug();
    if (isDebug) {
        log.debug("STARTqueryString=" + queryString);
    }
    SearchRequest searchRequest = new SearchRequest(queryString, condQueries, identity.getKey(), roles, firstResult, maxResults, doHighlighting);
    Session session = null;
    try {
        session = acquireSession();
        if (isDebug) {
            log.debug("doSearch session=" + session);
        }
        Message requestMessage = session.createObjectMessage(searchRequest);
        Message returnedMessage = doSearchRequest(session, requestMessage);
        queryCount_++;
        if (returnedMessage != null) {
            String responseStatus = returnedMessage.getStringProperty(JMS_RESPONSE_STATUS_PROPERTY_NAME);
            if (responseStatus.equalsIgnoreCase(JMS_RESPONSE_STATUS_OK)) {
                SearchResults searchResult = (SearchResults) ((ObjectMessage) returnedMessage).getObject();
                if (isDebug) {
                    log.debug("ENDqueryString=" + queryString);
                }
                return searchResult;
            } else if (responseStatus.equalsIgnoreCase(JMS_RESPONSE_STATUS_PARSE_EXCEPTION)) {
                throw new ParseException("can not parse query=" + queryString);
            } else if (responseStatus.equalsIgnoreCase(JMS_RESPONSE_STATUS_QUERY_EXCEPTION)) {
                throw new QueryException("invalid query=" + queryString);
            } else if (responseStatus.equalsIgnoreCase(JMS_RESPONSE_STATUS_SERVICE_NOT_AVAILABLE_EXCEPTION)) {
                throw new ServiceNotAvailableException("Remote search service not available" + queryString);
            } else {
                log.warn("doSearch: receive unkown responseStatus=" + responseStatus);
                return null;
            }
        } else {
            // null returnedMessage
            throw new ServiceNotAvailableException("communication error with JMS - cannot receive messages!!!");
        }
    } catch (JMSException e) {
        log.error("Search failure I", e);
        throw new ServiceNotAvailableException("communication error with JMS - cannot send messages!!!");
    } finally {
        releaseSession(session);
    }
}
Also used : ServiceNotAvailableException(org.olat.search.ServiceNotAvailableException) QueryException(org.olat.search.QueryException) ObjectMessage(javax.jms.ObjectMessage) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) JMSException(javax.jms.JMSException) ParseException(org.apache.lucene.queryparser.classic.ParseException) SearchResults(org.olat.search.SearchResults) Session(javax.jms.Session)

Example 9 with ServiceNotAvailableException

use of org.olat.search.ServiceNotAvailableException in project openolat by klemens.

the class SearchInputController method doSearch.

protected SearchResults doSearch(UserRequest ureq, String searchString, List<String> condSearchStrings, String parentCtxt, String docType, String rsrcUrl, int firstResult, int maxReturns, boolean doSpellCheck) {
    String query = null;
    List<String> condQueries = null;
    try {
        if (doSpellCheck) {
            // remove first old "did you mean words"
            hideDidYouMeanWords();
        }
        query = getQueryString(searchString, false);
        condQueries = getCondQueryStrings(condSearchStrings, parentCtxt, docType, rsrcUrl);
        SearchResults searchResults = searchClient.doSearch(query, condQueries, ureq.getIdentity(), ureq.getUserSession().getRoles(), firstResult, maxReturns, true);
        if (firstResult == 0 && searchResults.size() == 0 && StringHelper.containsNonWhitespace(query) && !query.endsWith(FUZZY_SEARCH)) {
            // result-list was empty => first try to find word via spell-checker
            if (doSpellCheck) {
                Set<String> didYouMeansWords = searchClient.spellCheck(searchString);
                if (didYouMeansWords != null && !didYouMeansWords.isEmpty()) {
                    setDidYouMeanWords(didYouMeansWords);
                } else {
                    searchResults = doFuzzySearch(ureq, searchString, null, parentCtxt, docType, rsrcUrl, firstResult, maxReturns);
                }
            } else {
                searchResults = doFuzzySearch(ureq, searchString, null, parentCtxt, docType, rsrcUrl, firstResult, maxReturns);
            }
        }
        if (firstResult == 0 && searchResults.getList().isEmpty()) {
            showInfo("found.no.result.try.fuzzy.search");
        }
        return searchResults;
    } catch (ParseException e) {
        if (log.isDebug())
            log.debug("Query cannot be parsed: " + query);
        getWindowControl().setWarning(translate("invalid.search.query"));
    } catch (QueryException e) {
        getWindowControl().setWarning(translate("invalid.search.query.with.wildcard"));
    } catch (ServiceNotAvailableException e) {
        getWindowControl().setWarning(translate("search.service.not.available"));
    } catch (Exception e) {
        log.error("Unexpected exception while searching", e);
        getWindowControl().setWarning(translate("search.service.unexpected.error"));
    }
    return SearchResults.EMPTY_SEARCH_RESULTS;
}
Also used : ServiceNotAvailableException(org.olat.search.ServiceNotAvailableException) QueryException(org.olat.search.QueryException) ParseException(org.apache.lucene.queryparser.classic.ParseException) SearchResults(org.olat.search.SearchResults) ParseException(org.apache.lucene.queryparser.classic.ParseException) QueryException(org.olat.search.QueryException) ServiceNotAvailableException(org.olat.search.ServiceNotAvailableException)

Example 10 with ServiceNotAvailableException

use of org.olat.search.ServiceNotAvailableException in project OpenOLAT by OpenOLAT.

the class SearchCallable method call.

@Override
public SearchResults call() throws ParseException {
    IndexSearcher searcher = null;
    try {
        boolean debug = log.isDebug();
        if (!searchService.existIndex()) {
            log.warn("Index does not exist, can't search for queryString: " + queryString);
            throw new ServiceNotAvailableException("Index does not exist");
        }
        if (debug)
            log.debug("queryString=" + queryString);
        searcher = searchService.getIndexSearcher();
        BooleanQuery query = searchService.createQuery(queryString, condQueries);
        if (debug)
            log.debug("query=" + query);
        if (Thread.interrupted()) {
            throw new InterruptedException();
        }
        long startTime = System.currentTimeMillis();
        int n = SearchServiceFactory.getService().getSearchModuleConfig().getMaxHits();
        TopDocs docs = searcher.search(query, n);
        long queryTime = System.currentTimeMillis() - startTime;
        if (debug)
            log.debug("hits.length()=" + docs.totalHits);
        SearchResultsImpl searchResult = new SearchResultsImpl(searchService.getMainIndexer(), searcher, docs, query, searchService.getAnalyzer(), identity, roles, firstResult, maxResults, doHighlighting, false);
        searchResult.setQueryTime(queryTime);
        searchResult.setNumberOfIndexDocuments(docs.totalHits);
        if (debug)
            log.debug("found=" + docs.totalHits);
        return searchResult;
    } catch (ParseException pex) {
        throw pex;
    } catch (Exception naex) {
        log.error("", naex);
        return null;
    } finally {
        searchService.releaseIndexSearcher(searcher);
        DBFactory.getInstance().commitAndCloseSession();
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) ServiceNotAvailableException(org.olat.search.ServiceNotAvailableException) TopDocs(org.apache.lucene.search.TopDocs) BooleanQuery(org.apache.lucene.search.BooleanQuery) SearchResultsImpl(org.olat.search.service.searcher.SearchResultsImpl) ParseException(org.apache.lucene.queryparser.classic.ParseException) ParseException(org.apache.lucene.queryparser.classic.ParseException) ServiceNotAvailableException(org.olat.search.ServiceNotAvailableException)

Aggregations

ServiceNotAvailableException (org.olat.search.ServiceNotAvailableException)16 ParseException (org.apache.lucene.queryparser.classic.ParseException)12 QueryException (org.olat.search.QueryException)8 SearchResults (org.olat.search.SearchResults)8 JMSException (javax.jms.JMSException)6 Message (javax.jms.Message)6 ObjectMessage (javax.jms.ObjectMessage)6 Session (javax.jms.Session)6 TextMessage (javax.jms.TextMessage)6 HashSet (java.util.HashSet)4 Document (org.apache.lucene.document.Document)4 BooleanQuery (org.apache.lucene.search.BooleanQuery)4 IndexSearcher (org.apache.lucene.search.IndexSearcher)4 TopDocs (org.apache.lucene.search.TopDocs)4 AbstractOlatDocument (org.olat.search.model.AbstractOlatDocument)4 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 ExecutionException (java.util.concurrent.ExecutionException)2