Search in sources :

Example 46 with ContentStream

use of org.apache.solr.common.util.ContentStream in project SearchServices by Alfresco.

the class AbstractQParser method getSearchParameters.

protected Pair<SearchParameters, Boolean> getSearchParameters() {
    SearchParameters searchParameters = new SearchParameters();
    Boolean isFilter = Boolean.FALSE;
    Iterable<ContentStream> streams = req.getContentStreams();
    JSONObject json = (JSONObject) req.getContext().get(ALFRESCO_JSON);
    if (json == null) {
        if (streams != null) {
            try {
                Reader reader = null;
                for (ContentStream stream : streams) {
                    reader = new BufferedReader(new InputStreamReader(stream.getStream(), "UTF-8"));
                }
                // TODO - replace with streaming-based solution e.g. SimpleJSON ContentHandler
                if (reader != null) {
                    json = new JSONObject(new JSONTokener(reader));
                    req.getContext().put(ALFRESCO_JSON, json);
                }
            } catch (JSONException e) {
            // This is expected when there is no json element to the request
            } catch (IOException e) {
                throw new AlfrescoRuntimeException("IO Error parsing query parameters", e);
            }
        }
    }
    if (json != null) {
        try {
            if (getString() != null) {
                if (getString().equals(AUTHORITY_FILTER_FROM_JSON)) {
                    isFilter = Boolean.TRUE;
                    ArrayList<String> tenantList = new ArrayList<String>(1);
                    JSONArray tenants = json.getJSONArray("tenants");
                    for (int i = 0; i < tenants.length(); i++) {
                        String tenantString = tenants.getString(i);
                        tenantList.add(tenantString);
                    }
                    ArrayList<String> authorityList = new ArrayList<String>(1);
                    JSONArray authorities = json.getJSONArray("authorities");
                    for (int i = 0; i < authorities.length(); i++) {
                        String authorityString = authorities.getString(i);
                        authorityList.add(authorityString);
                    }
                    char separator = getSeparator(authorityList);
                    StringBuilder authQuery = new StringBuilder();
                    StringBuilder denyQuery = new StringBuilder();
                    for (String tenant : tenantList) {
                        for (String authority : authorityList) {
                            if (separator == 0) {
                                if (authQuery.length() > 0) {
                                    authQuery.append(" ");
                                    denyQuery.append(" ");
                                }
                                switch(AuthorityType.getAuthorityType(authority)) {
                                    case USER:
                                        authQuery.append("|AUTHORITY:\"").append(authority).append("\"");
                                        denyQuery.append("|DENIED:\"").append(authority).append("\"");
                                        break;
                                    case GROUP:
                                    case EVERYONE:
                                    case GUEST:
                                        if (tenant.length() == 0) {
                                            // Default tenant matches 4.0
                                            authQuery.append("|AUTHORITY:\"").append(authority).append("\"");
                                            denyQuery.append("|DENIED:\"").append(authority).append("\"");
                                        } else {
                                            authQuery.append("|AUTHORITY:\"").append(authority).append("@").append(tenant).append("\"");
                                            denyQuery.append("|DENIED:\"").append(authority).append("@").append(tenant).append("\"");
                                        }
                                        break;
                                    default:
                                        authQuery.append("|AUTHORITY:\"").append(authority).append("\"");
                                        denyQuery.append("|DENIED:\"").append(authority).append("\"");
                                        break;
                                }
                            } else {
                                if (authQuery.length() == 0) {
                                    authset = true;
                                    authQuery.append("|AUTHSET:\"");
                                    denyQuery.append("|DENYSET:\"");
                                }
                                switch(AuthorityType.getAuthorityType(authority)) {
                                    case USER:
                                        authQuery.append(separator).append(authority);
                                        denyQuery.append(separator).append(authority);
                                        break;
                                    case GROUP:
                                    case EVERYONE:
                                    case GUEST:
                                        if (tenant.length() == 0) {
                                            // Default tenant matches 4.0
                                            authQuery.append(separator).append(authority);
                                            denyQuery.append(separator).append(authority);
                                        } else {
                                            authQuery.append(separator).append(authority).append("@").append(tenant);
                                            denyQuery.append(separator).append(authority).append("@").append(tenant);
                                        }
                                        break;
                                    default:
                                        authQuery.append(separator).append(authority);
                                        denyQuery.append(separator).append(authority);
                                        break;
                                }
                            }
                        }
                    }
                    if (separator != 0) {
                        authQuery.append("\"");
                        denyQuery.append("\"");
                    }
                    if (authQuery.length() > 0) {
                        // Default to true for safety reasons.
                        final boolean anyDenyDenies = json.optBoolean("anyDenyDenies", true);
                        if (anyDenyDenies) {
                            authQuery.insert(0, "(").append(") AND NOT (").append(denyQuery).append(")");
                            // Record that the clause has been added.
                            // We only ever set this to true for solr4+
                            req.getContext().put("processedDenies", Boolean.TRUE);
                        }
                        searchParameters.setQuery(authQuery.toString());
                    }
                } else if (getString().equals(TENANT_FILTER_FROM_JSON)) {
                    isFilter = Boolean.TRUE;
                    ArrayList<String> tenantList = new ArrayList<String>(1);
                    JSONArray tenants = json.getJSONArray("tenants");
                    for (int i = 0; i < tenants.length(); i++) {
                        String tenantString = tenants.getString(i);
                        tenantList.add(tenantString);
                    }
                    StringBuilder tenantQuery = new StringBuilder();
                    for (String tenant : tenantList) {
                        if (tenantQuery.length() > 0) {
                            tenantQuery.append(" ");
                        }
                        if (tenant.length() > 0) {
                            tenantQuery.append("|TENANT:\"").append(tenant).append("\"");
                        } else {
                            // TODO: Need to check for the default tenant or no tenant (4.0) or we force a reindex
                            // requirement later ...
                            // Better to add default tenant to the 4.0 index
                            tenantQuery.append("|TENANT:\"").append("_DEFAULT_").append("\"");
                        // tenantQuery.append(" |(+ISNODE:T -TENANT:*)");
                        }
                    }
                    searchParameters.setQuery(tenantQuery.toString());
                } else if (getString().equals(RERANK_QUERY_FROM_CONTEXT)) {
                    String searchTerm = getParam("spellcheck.q");
                    searchParameters.setQuery(searchTerm);
                }
            } else {
                String query = json.getString("query");
                if (query != null) {
                    searchParameters.setQuery(query);
                }
            }
            JSONArray locales = json.getJSONArray("locales");
            for (int i = 0; i < locales.length(); i++) {
                String localeString = locales.getString(i);
                Locale locale = DefaultTypeConverter.INSTANCE.convert(Locale.class, localeString);
                searchParameters.addLocale(locale);
            }
            JSONArray templates = json.getJSONArray("templates");
            for (int i = 0; i < templates.length(); i++) {
                JSONObject template = templates.getJSONObject(i);
                String name = template.getString("name");
                String queryTemplate = template.getString("template");
                searchParameters.addQueryTemplate(name, queryTemplate);
            }
            JSONArray allAttributes = json.getJSONArray("allAttributes");
            for (int i = 0; i < allAttributes.length(); i++) {
                String allAttribute = allAttributes.getString(i);
                searchParameters.addAllAttribute(allAttribute);
            }
            searchParameters.setDefaultFTSOperator(Operator.valueOf(json.getString("defaultFTSOperator")));
            searchParameters.setDefaultFTSFieldConnective(Operator.valueOf(json.getString("defaultFTSFieldOperator")));
            if (json.has("mlAnalaysisMode")) {
                searchParameters.setMlAnalaysisMode(MLAnalysisMode.valueOf(json.getString("mlAnalaysisMode")));
            }
            searchParameters.setNamespace(json.getString("defaultNamespace"));
            JSONArray textAttributes = json.getJSONArray("textAttributes");
            for (int i = 0; i < textAttributes.length(); i++) {
                String textAttribute = textAttributes.getString(i);
                searchParameters.addTextAttribute(textAttribute);
            }
            searchParameters.setQueryConsistency(QueryConsistency.valueOf(json.getString("queryConsistency")));
        } catch (JSONException e) {
        // This is expected when there is no json element to the request
        }
    }
    if (json != null) {
        if (log.isDebugEnabled()) {
            log.debug(json.toString());
        }
    }
    if (searchParameters.getQuery() == null) {
        searchParameters.setQuery(getString());
    }
    if (searchParameters.getLocales().size() == 0) {
        searchParameters.addLocale(I18NUtil.getLocale());
    }
    String defaultField = getParam(CommonParams.DF);
    if (defaultField != null) {
        searchParameters.setDefaultFieldName(defaultField);
    }
    if (autoDetectQueryLocale) {
        String searchTerm = getParam("spellcheck.q");
        if (searchTerm != null) {
            searchParameters.setSearchTerm(searchTerm);
            List<DetectedLanguage> detetcted = detectLanguage(searchTerm);
            if ((detetcted != null) && (detetcted.size() > 0)) {
                Locale detectedLocale = Locale.forLanguageTag(detetcted.get(0).getLangCode());
                if (localeIsNotIncluded(searchParameters, detectedLocale)) {
                    searchParameters.addLocale(Locale.forLanguageTag(detectedLocale.getLanguage()));
                }
            }
        }
    }
    if (fixedQueryLocales.size() > 0) {
        for (String locale : fixedQueryLocales) {
            searchParameters.addLocale(Locale.forLanguageTag(locale));
        }
    }
    // searchParameters.setMlAnalaysisMode(getMLAnalysisMode());
    searchParameters.setNamespace(NamespaceService.CONTENT_MODEL_1_0_URI);
    return new Pair<SearchParameters, Boolean>(searchParameters, isFilter);
}
Also used : Locale(java.util.Locale) InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) JSONException(org.json.JSONException) IOException(java.io.IOException) JSONTokener(org.json.JSONTokener) SearchParameters(org.alfresco.service.cmr.search.SearchParameters) ContentStream(org.apache.solr.common.util.ContentStream) JSONObject(org.json.JSONObject) BufferedReader(java.io.BufferedReader) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) Pair(org.alfresco.util.Pair)

Example 47 with ContentStream

use of org.apache.solr.common.util.ContentStream in project storm by apache.

the class SolrJsonMapper method createtSolrRequest.

private SolrRequest createtSolrRequest(String json) {
    final ContentStreamUpdateRequest request = new ContentStreamUpdateRequest(jsonUpdateUrl);
    final ContentStream cs = new ContentStreamBase.StringStream(json, CONTENT_TYPE);
    request.addContentStream(cs);
    if (logger.isDebugEnabled()) {
        logger.debug("Request generated with JSON: " + json);
    }
    return request;
}
Also used : ContentStream(org.apache.solr.common.util.ContentStream) ContentStreamUpdateRequest(org.apache.solr.client.solrj.request.ContentStreamUpdateRequest)

Aggregations

ContentStream (org.apache.solr.common.util.ContentStream)47 ArrayList (java.util.ArrayList)22 Test (org.junit.Test)15 SolrException (org.apache.solr.common.SolrException)14 ContentStreamBase (org.apache.solr.common.util.ContentStreamBase)12 SolrParams (org.apache.solr.common.params.SolrParams)11 MultiMapSolrParams (org.apache.solr.common.params.MultiMapSolrParams)10 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)10 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)8 HashMap (java.util.HashMap)7 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)7 SolrCore (org.apache.solr.core.SolrCore)7 SolrQueryRequestBase (org.apache.solr.request.SolrQueryRequestBase)7 IOException (java.io.IOException)6 Reader (java.io.Reader)6 HttpServletRequest (javax.servlet.http.HttpServletRequest)6 SolrInputDocument (org.apache.solr.common.SolrInputDocument)6 NamedList (org.apache.solr.common.util.NamedList)6 LocalSolrQueryRequest (org.apache.solr.request.LocalSolrQueryRequest)5 FormDataRequestParser (org.apache.solr.servlet.SolrRequestParsers.FormDataRequestParser)5