Search in sources :

Example 11 with LuceneQueryParserException

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

the class SiteServiceImpl method findSites.

/*
     * (non-Javadoc)
     * @see org.alfresco.service.cmr.site.SiteService#findSites(java.lang.String, int)
     */
@Override
public List<SiteInfo> findSites(String filter, int size) {
    List<SiteInfo> result;
    NodeRef siteRoot = getSiteRoot();
    if (siteRoot == null) {
        result = Collections.emptyList();
    } else {
        // get the sites that match the specified names
        StringBuilder query = new StringBuilder(128);
        query.append("+TYPE:\"").append(SiteModel.TYPE_SITE).append('"');
        final boolean filterIsPresent = filter != null && filter.length() > 0;
        if (filterIsPresent) {
            query.append(" AND (");
            String escNameFilter = SearchLanguageConversion.escapeLuceneQuery(filter.replace('"', ' '));
            String[] tokenizedFilter = SearchLanguageConversion.tokenizeString(escNameFilter);
            // cm:name
            query.append(" cm:name:\" ");
            for (int i = 0; i < tokenizedFilter.length; i++) {
                if (// Not first element
                i != 0) {
                    query.append("?");
                }
                query.append(tokenizedFilter[i].toLowerCase());
            }
            query.append("*\"");
            // cm:title
            query.append(" OR ").append(" cm:title: (");
            for (int i = 0; i < tokenizedFilter.length; i++) {
                if (// Not first element
                i != 0) {
                    query.append(" AND ");
                }
                query.append("\"" + tokenizedFilter[i] + "*\" ");
            }
            query.append(")");
            query.append(" OR cm:description:\"" + escNameFilter + "\"");
            query.append(")");
        }
        SearchParameters sp = new SearchParameters();
        sp.addStore(siteRoot.getStoreRef());
        sp.setLanguage(SearchService.LANGUAGE_FTS_ALFRESCO);
        sp.setQuery(query.toString());
        if (size > 0) {
            sp.setLimit(size);
            sp.setLimitBy(LimitBy.FINAL_SIZE);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Search parameters are: " + sp);
        }
        ResultSet results = null;
        try {
            results = this.searchService.query(sp);
            result = new ArrayList<SiteInfo>(results.length());
            for (NodeRef site : results.getNodeRefs()) {
                result.add(createSiteInfo(site));
            }
        } catch (LuceneQueryParserException lqpe) {
            // Log the error but suppress is from the user
            logger.error("LuceneQueryParserException with findSites()", lqpe);
            result = Collections.emptyList();
        } finally {
            if (results != null)
                results.close();
        }
    }
    return result;
}
Also used : SiteInfo(org.alfresco.service.cmr.site.SiteInfo) NodeRef(org.alfresco.service.cmr.repository.NodeRef) SearchParameters(org.alfresco.service.cmr.search.SearchParameters) LuceneQueryParserException(org.alfresco.repo.search.impl.lucene.LuceneQueryParserException) ResultSet(org.alfresco.service.cmr.search.ResultSet) FilterTypeString(org.alfresco.repo.node.getchildren.FilterPropString.FilterTypeString) FilterPropString(org.alfresco.repo.node.getchildren.FilterPropString)

Example 12 with LuceneQueryParserException

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

the class SOLRAdminClient method executeCommand.

/* (non-Javadoc)
     * @see org.alfresco.repo.search.impl.solr.SolrAdminClient#executeCommand(java.lang.String, org.alfresco.repo.search.impl.solr.SolrAdminClient.HANDLER, org.alfresco.repo.search.impl.solr.SolrAdminClient.COMMAND, java.util.Map)
     */
@Override
public JSONAPIResult executeCommand(String core, JSONAPIResultFactory.HANDLER handler, JSONAPIResultFactory.COMMAND command, Map<String, String> parameters) {
    StoreRef store = StoreRef.STORE_REF_WORKSPACE_SPACESSTORE;
    SolrStoreMappingWrapper mapping = SolrClientUtil.extractMapping(store, mappingLookup, shardRegistry, useDynamicShardRegistration, beanFactory);
    HttpClient httpClient = mapping.getHttpClientAndBaseUrl().getFirst();
    StringBuilder url = new StringBuilder();
    url.append(baseUrl);
    if (!url.toString().endsWith("/")) {
        url.append("/");
    }
    url.append(core + "/" + handler.toString().toLowerCase());
    URLCodec encoder = new URLCodec();
    url.append("?command=" + command.toString().toLowerCase());
    parameters.forEach((key, value) -> {
        try {
            url.append("&" + key + "=" + encoder.encode(value));
        } catch (EncoderException e) {
            throw new RuntimeException(e);
        }
    });
    url.append("&alfresco.shards=");
    if (mapping.isSharded()) {
        url.append(mapping.getShards());
    } else {
        String solrurl = httpClient.getHostConfiguration().getHostURL() + mapping.getHttpClientAndBaseUrl().getSecond();
        url.append(solrurl);
    }
    try {
        JSONAPIResult response = new SolrCommandBackupResult(getOperation(httpClient, url.toString()));
        if (response.getStatus() != 0) {
            solrTracker.setSolrActive(false);
        }
        return response;
    } catch (IOException e) {
        throw new LuceneQueryParserException("action", e);
    }
}
Also used : URLCodec(org.apache.commons.codec.net.URLCodec) StoreRef(org.alfresco.service.cmr.repository.StoreRef) EncoderException(org.apache.commons.codec.EncoderException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) SolrCommandBackupResult(org.alfresco.repo.search.impl.lucene.SolrCommandBackupResult) ExplicitSolrStoreMappingWrapper(org.alfresco.repo.search.impl.solr.ExplicitSolrStoreMappingWrapper) SolrStoreMappingWrapper(org.alfresco.repo.search.impl.solr.SolrStoreMappingWrapper) LuceneQueryParserException(org.alfresco.repo.search.impl.lucene.LuceneQueryParserException) HttpClient(org.apache.commons.httpclient.HttpClient) IOException(java.io.IOException) JSONAPIResult(org.alfresco.repo.search.impl.lucene.JSONAPIResult)

Example 13 with LuceneQueryParserException

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

the class ExplicitSolrStoreMappingWrapper method getShards2.

private String getShards2() {
    try {
        URLCodec encoder = new URLCodec();
        StringBuilder builder = new StringBuilder();
        for (int shard = 0; shard < wrapped.getNumShards(); shard++) {
            int position = random.nextInt(wrapped.getReplicationFactor());
            List<Integer> nodeInstances = policy.getNodeInstancesForShardId(shard);
            Integer nodeId = nodeInstances.get(position);
            if (builder.length() > 0) {
                builder.append(',');
            }
            HttpClientAndBaseUrl httpClientAndBaseUrl = httpClientsAndBaseURLs.toArray(new HttpClientAndBaseUrl[0])[nodeId - 1];
            builder.append(encoder.encode(httpClientAndBaseUrl.getProtocol() + "://", "UTF-8"));
            builder.append(encoder.encode(httpClientAndBaseUrl.getHost(), "UTF-8"));
            builder.append(':');
            builder.append(encoder.encode("" + httpClientAndBaseUrl.getPort(), "UTF-8"));
            if (httpClientAndBaseUrl.getBaseUrl().startsWith("/")) {
                builder.append(encoder.encode(httpClientAndBaseUrl.getBaseUrl(), "UTF-8"));
            } else {
                builder.append(encoder.encode("/" + httpClientAndBaseUrl.getBaseUrl(), "UTF-8"));
            }
            if (isSharded())
                builder.append('-').append(shard);
        }
        return builder.toString();
    } catch (UnsupportedEncodingException e) {
        throw new LuceneQueryParserException("", e);
    }
}
Also used : URLCodec(org.apache.commons.codec.net.URLCodec) LuceneQueryParserException(org.alfresco.repo.search.impl.lucene.LuceneQueryParserException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 14 with LuceneQueryParserException

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

the class SolrClientUtil method extractMapping.

public static SolrStoreMappingWrapper extractMapping(StoreRef store, HashMap<StoreRef, SolrStoreMappingWrapper> mappingLookup, ShardRegistry shardRegistry, boolean useDynamicShardRegistration, BeanFactory beanFactory) {
    if ((shardRegistry != null) && useDynamicShardRegistration) {
        SearchParameters sp = new SearchParameters();
        sp.addStore(store);
        List<ShardInstance> slice = shardRegistry.getIndexSlice(sp);
        if ((slice == null) || (slice.size() == 0)) {
            logger.error("No available shards for solr query of store " + store + " - trying non-dynamic configuration");
            SolrStoreMappingWrapper mappings = mappingLookup.get(store);
            if (mappings == null) {
                throw new LuceneQueryParserException("No solr query support for store " + store);
            }
            return mappings;
        }
        return DynamicSolrStoreMappingWrapperFactory.wrap(slice, beanFactory);
    } else {
        SolrStoreMappingWrapper mappings = mappingLookup.get(store);
        if (mappings == null) {
            throw new LuceneQueryParserException("No solr query support for store " + store);
        }
        return mappings;
    }
}
Also used : BasicSearchParameters(org.alfresco.service.cmr.search.BasicSearchParameters) SearchParameters(org.alfresco.service.cmr.search.SearchParameters) LuceneQueryParserException(org.alfresco.repo.search.impl.lucene.LuceneQueryParserException) ShardInstance(org.alfresco.repo.index.shard.ShardInstance)

Example 15 with LuceneQueryParserException

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

the class AbstractSolrAdminHTTPClient method getOperation.

/**
 * Executes an action or a command in SOLR using REST API
 *
 * @param httpClient HTTP Client to be used for the invocation
 * @param url Complete URL of SOLR REST API Endpoint
 * @return A JSON Object including SOLR response
 * @throws UnsupportedEncodingException
 */
protected JSONObject getOperation(HttpClient httpClient, String url) throws UnsupportedEncodingException {
    GetMethod get = new GetMethod(url);
    try {
        httpClient.executeMethod(get);
        if (get.getStatusCode() == HttpStatus.SC_MOVED_PERMANENTLY || get.getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY) {
            Header locationHeader = get.getResponseHeader("location");
            if (locationHeader != null) {
                String redirectLocation = locationHeader.getValue();
                get.setURI(new URI(redirectLocation, true));
                httpClient.executeMethod(get);
            }
        }
        if (get.getStatusCode() != HttpServletResponse.SC_OK) {
            throw new LuceneQueryParserException("Request failed " + get.getStatusCode() + " " + url.toString());
        }
        Reader reader = new BufferedReader(new InputStreamReader(get.getResponseBodyAsStream(), get.getResponseCharSet()));
        JSONObject json = new JSONObject(new JSONTokener(reader));
        return json;
    } catch (IOException | JSONException e) {
        throw new AlfrescoRuntimeException(e.getMessage(), e);
    } finally {
        get.releaseConnection();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) JSONException(org.json.JSONException) IOException(java.io.IOException) URI(org.apache.commons.httpclient.URI) JSONTokener(org.json.JSONTokener) Header(org.apache.commons.httpclient.Header) JSONObject(org.json.JSONObject) LuceneQueryParserException(org.alfresco.repo.search.impl.lucene.LuceneQueryParserException) GetMethod(org.apache.commons.httpclient.methods.GetMethod) BufferedReader(java.io.BufferedReader) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException)

Aggregations

LuceneQueryParserException (org.alfresco.repo.search.impl.lucene.LuceneQueryParserException)15 IOException (java.io.IOException)9 JSONObject (org.json.JSONObject)9 URLCodec (org.apache.commons.codec.net.URLCodec)8 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)7 HttpClient (org.apache.commons.httpclient.HttpClient)7 JSONException (org.json.JSONException)7 BufferedReader (java.io.BufferedReader)6 InputStreamReader (java.io.InputStreamReader)6 Reader (java.io.Reader)6 SearchParameters (org.alfresco.service.cmr.search.SearchParameters)6 Header (org.apache.commons.httpclient.Header)6 URI (org.apache.commons.httpclient.URI)6 JSONTokener (org.json.JSONTokener)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 StoreRef (org.alfresco.service.cmr.repository.StoreRef)5 GetMethod (org.apache.commons.httpclient.methods.GetMethod)5 HttpException (org.apache.commons.httpclient.HttpException)4 HashMap (java.util.HashMap)3 Locale (java.util.Locale)3