Search in sources :

Example 1 with ServiceNotAvailableException

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

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 2 with ServiceNotAvailableException

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

the class SearchServiceImpl method doSearch.

/**
 * Do search a certain query. The results will be filtered for the identity and roles.
 * @param queryString   Search query-string.
 * @param identity      Filter results for this identity (user).
 * @param roles         Filter results for this roles (role of user).
 * @return              SearchResults object for this query
 */
@Override
public SearchResults doSearch(String queryString, List<String> condQueries, Identity identity, Roles roles, int firstResult, int maxResults, boolean doHighlighting) throws ServiceNotAvailableException, ParseException {
    Future<SearchResults> futureResults = null;
    try {
        SearchCallable run = new SearchCallable(queryString, condQueries, identity, roles, firstResult, maxResults, doHighlighting, this);
        futureResults = searchExecutor.submit(run);
        SearchResults results = futureResults.get(searchModuleConfig.getSearchTimeout(), TimeUnit.SECONDS);
        queryCount++;
        return results;
    } catch (InterruptedException e) {
        log.error("", e);
        return null;
    } catch (TimeoutException e) {
        cancelSearch(futureResults);
        log.error("", e);
        return null;
    } catch (ExecutionException e) {
        Throwable e1 = e.getCause();
        if (e1 instanceof ParseException) {
            throw (ParseException) e1;
        } else if (e1 instanceof ServiceNotAvailableException) {
            throw (ServiceNotAvailableException) e1;
        }
        log.error("", e);
        return null;
    }
}
Also used : ServiceNotAvailableException(org.olat.search.ServiceNotAvailableException) ParseException(org.apache.lucene.queryparser.classic.ParseException) SearchResults(org.olat.search.SearchResults) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 3 with ServiceNotAvailableException

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

the class FileDocumentFactory method getDocumentFromCurrentIndex.

private Document getDocumentFromCurrentIndex(SearchResourceContext leafResourceContext, VFSLeaf leaf) {
    try {
        String resourceUrl = leafResourceContext.getResourceUrl();
        SearchService searchService = CoreSpringFactory.getImpl(SearchServiceImpl.class);
        Document indexedDoc = searchService.doSearch(resourceUrl);
        if (indexedDoc != null) {
            String timestamp = indexedDoc.get(AbstractOlatDocument.TIME_STAMP_NAME);
            if (timestamp != null) {
                Date indexLastModification = DateTools.stringToDate(timestamp);
                Date docLastModificationDate = new Date(leaf.getLastModified());
                if (leaf instanceof MetaTagged) {
                    MetaInfo metaInfo = ((MetaTagged) leaf).getMetaInfo();
                    Date metaDate = metaInfo.getMetaLastModified();
                    if (metaDate != null && metaDate.after(docLastModificationDate)) {
                        docLastModificationDate = metaDate;
                    }
                }
                if (docLastModificationDate.compareTo(indexLastModification) < 0) {
                    OlatDocument olatDoc = new OlatDocument(indexedDoc);
                    return olatDoc.getLuceneDocument();
                }
            }
        }
    } catch (ServiceNotAvailableException | ParseException | QueryException | java.text.ParseException e) {
        log.error("", e);
    }
    return null;
}
Also used : ServiceNotAvailableException(org.olat.search.ServiceNotAvailableException) AbstractOlatDocument(org.olat.search.model.AbstractOlatDocument) OlatDocument(org.olat.search.model.OlatDocument) QueryException(org.olat.search.QueryException) SearchService(org.olat.search.SearchService) MetaTagged(org.olat.core.commons.modules.bc.meta.tagged.MetaTagged) MetaInfo(org.olat.core.commons.modules.bc.meta.MetaInfo) ParseException(org.apache.lucene.queryparser.classic.ParseException) Document(org.apache.lucene.document.Document) AbstractOlatDocument(org.olat.search.model.AbstractOlatDocument) OlatDocument(org.olat.search.model.OlatDocument) Date(java.util.Date)

Example 4 with ServiceNotAvailableException

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

the class SearchClientProxy method spellCheck.

/**
 * Uses Request/reply mechanism for synchronous operation.
 * @see org.olat.search.service.searcher.OLATSearcher#spellCheck(java.lang.String)
 */
public Set<String> spellCheck(String query) throws ServiceNotAvailableException {
    Session session = null;
    try {
        session = acquireSession();
        TextMessage requestMessage = session.createTextMessage(query);
        Message returnedMessage = doSearchRequest(session, requestMessage);
        if (returnedMessage != null) {
            @SuppressWarnings("unchecked") List<String> spellStringList = (List<String>) ((ObjectMessage) returnedMessage).getObject();
            return new HashSet<String>(spellStringList);
        } else {
            // null returnedMessage
            throw new ServiceNotAvailableException("spellCheck, communication error with JMS - cannot receive messages!!!");
        }
    } catch (JMSException e) {
        throw new ServiceNotAvailableException("spellCheck, communication error with JMS - cannot send messages!!!");
    } catch (ServiceNotAvailableException e) {
        throw e;
    } catch (Throwable th) {
        throw new OLATRuntimeException("ClusteredSearchRequester.spellCheck() error!!!", th);
    } finally {
        releaseSession(session);
    }
}
Also used : ServiceNotAvailableException(org.olat.search.ServiceNotAvailableException) ObjectMessage(javax.jms.ObjectMessage) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) LinkedList(java.util.LinkedList) List(java.util.List) JMSException(javax.jms.JMSException) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) HashSet(java.util.HashSet)

Example 5 with ServiceNotAvailableException

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

the class SearchServiceImpl method doSearch.

/**
 * Do search a certain query. The results will be filtered for the identity and roles.
 * @param queryString   Search query-string.
 * @param identity      Filter results for this identity (user).
 * @param roles         Filter results for this roles (role of user).
 * @return              SearchResults object for this query
 */
@Override
public SearchResults doSearch(String queryString, List<String> condQueries, Identity identity, Roles roles, int firstResult, int maxResults, boolean doHighlighting) throws ServiceNotAvailableException, ParseException {
    Future<SearchResults> futureResults = null;
    try {
        SearchCallable run = new SearchCallable(queryString, condQueries, identity, roles, firstResult, maxResults, doHighlighting, this);
        futureResults = searchExecutor.submit(run);
        SearchResults results = futureResults.get(searchModuleConfig.getSearchTimeout(), TimeUnit.SECONDS);
        queryCount++;
        return results;
    } catch (InterruptedException e) {
        log.error("", e);
        return null;
    } catch (TimeoutException e) {
        cancelSearch(futureResults);
        log.error("", e);
        return null;
    } catch (ExecutionException e) {
        Throwable e1 = e.getCause();
        if (e1 instanceof ParseException) {
            throw (ParseException) e1;
        } else if (e1 instanceof ServiceNotAvailableException) {
            throw (ServiceNotAvailableException) e1;
        }
        log.error("", e);
        return null;
    }
}
Also used : ServiceNotAvailableException(org.olat.search.ServiceNotAvailableException) ParseException(org.apache.lucene.queryparser.classic.ParseException) SearchResults(org.olat.search.SearchResults) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

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