Example usage for org.apache.solr.common.params SolrParams getBool

List of usage examples for org.apache.solr.common.params SolrParams getBool

Introduction

In this page you can find the example usage for org.apache.solr.common.params SolrParams getBool.

Prototype

public boolean getBool(String param, boolean def) 

Source Link

Document

Returns the boolean value of the param, or def if not set

Usage

From source file:at.newmedialab.lmf.util.solr.SuggestionRequestHandler.java

License:Apache License

@Override
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {

    SolrParams params = req.getParams();

    if (params.getBool(SuggestionRequestParams.SUGGESTION, SUGGESTION)) {

        String q = params.get(CommonParams.Q);
        if (q == null) {
            rsp.add("error", error(400, "SuggestionRequest needs to have a 'q' parameter"));
            return;
        }/* w ww  .  j a  v  a  2 s  .c  om*/

        String[] fields = params.getParams(SuggestionRequestParams.SUGGESTION_FIELD) != null
                ? params.getParams(SuggestionRequestParams.SUGGESTION_FIELD)
                : FIELDS;
        if (fields == null) {
            rsp.add("error",
                    error(400, "SuggestionRequest needs to have at least one 'suggestion.field' parameter"));
            return;
        }

        String df = params.get(SuggestionRequestParams.SUGGESTION_DF, DF);
        if (df == null) {
            rsp.add("error", error(400, "SuggestionRequest needs to have a 'df' parameter"));
            return;
        }

        int limit = params.getInt(SuggestionRequestParams.SUGGESTION_LIMIT, LIMIT);
        if (limit < 1) {
            rsp.add("error", error(400, "SuggestionRequest needs to have a 'suggestion.limit' greater than 0"));
            return;
        }

        String[] fqs = params.getParams(CommonParams.FQ) != null ? params.getParams(CommonParams.FQ) : FQS;

        Boolean multivalue = params.getBool(SuggestionRequestParams.SUGGESTION_MULTIVALUE, MULTIVALUE);

        //TODO replace
        if (multivalue) {
            rsp.add("error", error(500, "Multivalue suggestions are not yet supported!"));
            return;
        }

        suggestionService.run(rsp, q, df, fields, fqs, limit, multivalue);

    } else {
        super.handleRequestBody(req, rsp);
    }
}

From source file:com.billiger.solr.handler.component.QLTBComponent.java

License:Apache License

/**
 * Check if this component is disabled for this particular request.
 * This component can be disabled on a per-request basis by either
 * adding qltb=false (substitute "qltb" with configured component name)
 * or adding opt_out=qltb (substitute...) to the SOLR request.
 *//*www .j  av  a 2 s  .c  om*/
private final boolean disabled(final ResponseBuilder responseBuilder) {
    SolrParams requestParams = responseBuilder.req.getParams();
    String componentName = initArgs.get(COMPONENT_NAME);
    String disable = requestParams.get(DISABLE_COMPONENT_PARAM);
    if (disable != null && Arrays.asList(disable.split(",")).contains(componentName)) {
        return true;
    }
    return requestParams.getBool(componentName, false);
}

From source file:com.cominvent.solr.RequestSanitizerComponent.java

License:Apache License

@Override
public void prepare(ResponseBuilder rb) throws IOException {
    SolrParams params = rb.req.getParams();
    if (!params.getBool(COMPONENT_NAME, true))
        return;//from www. j  a v a 2  s.  c  o m
    Map<String, Map<String, String>> mappings = parseMappings(Arrays.asList(params.getParams(SANITIZE_PARAM)));
    log.info("Initialized RequestSanitizerComponent with mappings " + mappings);

    Map<String, String> modified = getModifications(params, mappings);
    if (modified.size() > 0) {
        log.info("Request parameters that were modified by RequestSanitizerComponent: " + modified);
        rb.req.setParams(new DefaultSolrParams(new MapSolrParams(modified), params));
    }
}

From source file:com.cominvent.solr.update.processor.MappingUpdateProcessor.java

License:Apache License

/**
 * The constructor initializes the processor by reading configuration
 * and loading a HashMap from the specified file name.
 *//*from   ww w  .jav a  2 s. co m*/
public MappingUpdateProcessor(SolrCore core, SolrParams params, SolrQueryRequest req, SolrQueryResponse rsp,
        UpdateRequestProcessor next) {
    super(next);

    this.core = core;

    // TODO: Add support for caseSensitive and expand parameters, support full synonyms.txt format
    if (params != null) {
        setEnabled(params.getBool("enabled", true));

        inputField = params.get(INPUT_FIELD_PARAM, "").trim();
        outputField = params.get(OUTPUT_FIELD_PARAM, "").trim();
        docIdField = params.get(DOCID_PARAM, DOCID_FIELD_DEFAULT);
        fallbackValue = params.get(FALLBACK_VALUE_PARAM, null);
        mappingFile = params.get(MAPPING_FILE_PARAM, "").trim();
    }

    if (inputField.length() == 0) {
        log.error("Missing or faulty configuration of MappingUpdateProcessor. Input field must be specified");
        throw new SolrException(ErrorCode.NOT_FOUND,
                "Missing or faulty configuration of MappingUpdateProcessor. Input field must be specified");
    }

    try {
        log.info("Attempting to initialize mapping file " + mappingFile);
        InputStream is = core.getResourceLoader().openResource(mappingFile);
        BufferedReader br = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
        String line;
        while ((line = br.readLine()) != null) {
            if (line.startsWith("#"))
                continue;
            String[] kv = line.split("=>");
            if (kv.length < 2)
                continue;
            map.put(kv[0].trim(), kv[1].trim());
        }
        log.info("Map initialized from " + mappingFile + ": " + map);
    } catch (Exception e) {
        throw new SolrException(ErrorCode.NOT_FOUND, "Error when reading mapping file " + mappingFile + ".");
    }
}

From source file:com.doculibre.constellio.solr.handler.component.ConstellioAuthorizationComponent.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*w w w . jav  a 2s.  c om*/
public void prepare(ResponseBuilder rb) throws IOException {
    SolrQueryRequest req = rb.req;
    SolrIndexSearcher searcher = req.getSearcher();
    //IndexReader reader = req.getSearcher().getReader();
    SolrParams params = req.getParams();

    // A runtime param can skip
    if (!params.getBool(ENABLE, true)) {
        return;
    }

    Query query = rb.getQuery();
    String qstr = rb.getQueryString();
    if (query == null || qstr == null) {
        return;
    }

    ConstellioUser user;
    String userIdStr = params.get(ConstellioSolrQueryParams.USER_ID);
    if (userIdStr != null) {
        UserServices userServices = ConstellioSpringUtils.getUserServices();
        try {
            user = userServices.get(new Long(userIdStr));
        } catch (NumberFormatException e) {
            user = null;
        }
    } else {
        user = null;
    }

    String collectionName = params.get(ConstellioSolrQueryParams.COLLECTION_NAME);
    RecordCollectionServices collectionServices = ConstellioSpringUtils.getRecordCollectionServices();
    FederationServices federationServices = ConstellioSpringUtils.getFederationServices();
    RecordCollection collection = collectionServices.get(collectionName);

    List<TermQuery> restrictedCollectionQueries = new ArrayList<TermQuery>();
    if (collection.isFederationOwner()) {
        List<RecordCollection> includedCollections = federationServices.listIncludedCollections(collection);
        for (RecordCollection includedCollection : includedCollections) {
            if (includedCollection.hasSearchPermission()
                    && (user == null || !user.hasSearchPermission(includedCollection))) {
                restrictedCollectionQueries.add(new TermQuery(
                        new Term(IndexField.COLLECTION_ID_FIELD, "" + includedCollection.getId())));
            }
        }
    }

    // User must be logged in to see private records
    if (user != null) {
        String luceneQueryStr = params.get(ConstellioSolrQueryParams.LUCENE_QUERY);
        if (StringUtils.isBlank(luceneQueryStr)) {
            return;
        }

        IndexSchema schema = req.getSchema();
        SolrQueryParser queryParser = new SolrQueryParser(rb.getQparser(), IndexField.DEFAULT_SEARCH_FIELD);
        Query luceneQuery;
        try {
            luceneQuery = queryParser.parse(luceneQueryStr);
        } catch (SyntaxError e) {
            log.error("Error parsing lucene query " + luceneQueryStr, e);
            return;
        }
        // Create a new query which will only include private records
        BooleanQuery privateRecordQuery = new BooleanQuery(true);
        privateRecordQuery.add(luceneQuery, BooleanClause.Occur.MUST);
        for (TermQuery restrictionCollectionQuery : restrictedCollectionQueries) {
            privateRecordQuery.add(restrictionCollectionQuery, BooleanClause.Occur.MUST_NOT);
        }

        TermQuery privateRecordTQ = new TermQuery(new Term(IndexField.PUBLIC_RECORD_FIELD, "F"));
        privateRecordQuery.add(privateRecordTQ, BooleanClause.Occur.MUST);

        DocSet privateRecordIdDocSet = searcher.getDocSet(privateRecordQuery);

        if (privateRecordIdDocSet.size() > 0) {
            RecordServices recordServices = ConstellioSpringUtils.getRecordServices();
            ACLServices aclServices = ConstellioSpringUtils.getACLServices();
            ConnectorManagerServices connectorManagerServices = ConstellioSpringUtils
                    .getConnectorManagerServices();

            List<Record> privateRecords = new ArrayList<Record>();
            DocIterator docIt = privateRecordIdDocSet.iterator();
            while (docIt.hasNext()) {
                int docId = docIt.nextDoc();
                Document luceneDoc = searcher.doc(docId);
                Long recordId = new Long(luceneDoc.get(IndexField.RECORD_ID_FIELD));
                Record record = recordServices.get(recordId, collection);
                privateRecords.add(record);
            }
            // First pass : Remove ACL authorized records
            List<Record> unevaluatedPrivateRecords = aclServices.removeAuthorizedRecords(privateRecords, user);
            if (!unevaluatedPrivateRecords.isEmpty()) {
                Set<UserCredentials> userCredentials = user.getUserCredentials();
                // Second pass : Ask the connector manager
                ConnectorManager connectorManager = connectorManagerServices.getDefaultConnectorManager();
                List<Record> authorizedRecords = connectorManagerServices
                        .authorizeByConnector(unevaluatedPrivateRecords, userCredentials, connectorManager);
                List<Record> unauthorizedRecords = ListUtils.removeAll(unevaluatedPrivateRecords,
                        authorizedRecords);

                if (!unauthorizedRecords.isEmpty()) {
                    // Create a new query which will exclude unauthorized records
                    BooleanQuery authorizedRecordQuery = new BooleanQuery(true);
                    authorizedRecordQuery.add(query, BooleanClause.Occur.MUST);
                    for (Record unauthorizedRecord : unauthorizedRecords) {
                        TermQuery unauthorizedRecordTQ = new TermQuery(
                                new Term(IndexField.RECORD_ID_FIELD, "" + unauthorizedRecord.getId()));
                        authorizedRecordQuery.add(unauthorizedRecordTQ, BooleanClause.Occur.MUST_NOT);
                    }
                    rb.setQuery(authorizedRecordQuery);
                }
            }
        }
    } else {
        BooleanQuery publicRecordQuery = new BooleanQuery(true);
        publicRecordQuery.add(query, BooleanClause.Occur.MUST);
        TermQuery publicRecordTQ = new TermQuery(new Term(IndexField.PUBLIC_RECORD_FIELD, "T"));
        publicRecordQuery.add(publicRecordTQ, BooleanClause.Occur.MUST);
        for (TermQuery restrictionCollectionQuery : restrictedCollectionQueries) {
            publicRecordQuery.add(restrictionCollectionQuery, BooleanClause.Occur.MUST_NOT);
        }
        rb.setQuery(publicRecordQuery);
    }
}

From source file:com.doculibre.constellio.solr.handler.component.ManifoldCFAuthorizationComponent.java

License:Open Source License

@Override
public void prepare(ResponseBuilder rb) throws IOException {
    SolrQueryRequest req = rb.req;//from w  ww . j  a v a2  s .  c  o m
    SolrParams params = req.getParams();

    // A runtime param can skip
    if (!params.getBool(ENABLE, true)) {
        return;
    }

    boolean hasManifoldConnector = false;

    String collectioName = params.get(ConstellioSolrQueryParams.COLLECTION_NAME);
    RecordCollectionServices recordCollectionServices = ConstellioSpringUtils.getRecordCollectionServices();
    RecordCollection recordCollection = recordCollectionServices.get(collectioName);
    if (recordCollection != null) {
        for (ConnectorInstance connector : recordCollection.getConnectorInstances()) {
            if (connector.getConnectorType().getName().equals(ManifoldCFConnectorType.CONNECTOR_TYPE_NAME)) {
                hasManifoldConnector = true;
                break;
            } else if (connector.getConnectorType().getName()
                    .equals(IntelliGIDConnectorType.CONNECTOR_TYPE_NAME)) {
                hasManifoldConnector = true;
                break;
            }
        }
    }

    //skip calling the component if we don't use the service (the manifoldcf server could not be up)
    if (hasManifoldConnector) {
        ConstellioUser user;
        String userIdStr = params.get(ConstellioSolrQueryParams.USER_ID);
        if (userIdStr != null) {
            UserServices userServices = ConstellioSpringUtils.getUserServices();
            try {
                user = userServices.get(new Long(userIdStr));
            } catch (NumberFormatException e) {
                user = null;
            }
        } else {
            user = null;
        }

        if (user != null) {
            ModifiableSolrParams newParams = new ModifiableSolrParams(params);
            newParams.add(AUTHENTICATED_USER_NAME, user.getUsername() + "@" + user.getDomain());
            req.setParams(newParams);
        } else {
            ModifiableSolrParams newParams = new ModifiableSolrParams(params);
            newParams.add(AUTHENTICATED_USER_NAME, "guest@guest");
            req.setParams(newParams);
        }
        super.prepare(rb);
    }
}

From source file:com.francelabs.datafari.updateprocessor.DatafariUpdateProcessor.java

License:Apache License

public DatafariUpdateProcessor(final SolrParams params, final UpdateRequestProcessor next) {
    super(next);//from   ww  w.  ja  v a2 s  . c o m
    if (params != null) {
        extensionFromName = params.getBool(EXTENSION_PARAM, false);
    }
}

From source file:com.github.healthonnet.search.SynonymExpandingExtendedDismaxQParserPlugin.java

License:Apache License

@Override
public Query parse() throws SyntaxError {

    SolrParams localParams = getLocalParams();
    SolrParams params = getParams();/*from w ww  .  j a  va 2s. c  o m*/
    SolrParams solrParams = localParams == null ? params : SolrParams.wrapDefaults(localParams, params);

    String defmainAnalyser = solrParams.get(Params.MAIN_DEFAULT_ANALYZER, null);
    String defSynonymsAnalyser = solrParams.get(Params.SYNONYMS_DEFAULT_ANALYZER, null);

    Boolean preanalyzis = solrParams.getBool(Params.MAIN_PREANALYZIS, false);

    // check to make sure the analyzer exists
    if (preanalyzis) {
        String preAnalyzerName = solrParams.get(Params.MAIN_ANALYZER, null);
        if (preAnalyzerName == null) { // no query analyzer specified
            if (defmainAnalyser != null && defmainAnalyser.length() > 0) {
                preAnalyzerName = defmainAnalyser;
            } else {
                if (mainAnalyzers.size() >= 1) {
                    // only one analyzer defined; just use that one
                    preAnalyzerName = mainAnalyzers.keySet().iterator().next();
                }
            }
        }

        if (preAnalyzerName != null) {

            mainAnalyzer = mainAnalyzers.get(preAnalyzerName);

            analyzeMainQuery(mainAnalyzer);
        } else
            mainAnalyzer = null;
    } else {
        mainAnalyzer = null;
    }

    Query query = mainQueryParser.parse();

    // disable/enable synonym handling altogether
    if (!solrParams.getBool(Params.SYNONYMS, false)) {
        reasonForNotExpandingSynonyms = ReasonForNotExpandingSynonyms.PluginDisabled;
        return query;
    }

    // check to make sure the analyzer exists
    String analyzerName = solrParams.get(Params.SYNONYMS_ANALYZER, null);
    if (analyzerName == null) { // no synonym analyzer specified
        if (defSynonymsAnalyser != null && defSynonymsAnalyser.length() > 0) {
            analyzerName = defSynonymsAnalyser;
        } else {
            if (synonymAnalyzers.size() >= 1) {
                // only one analyzer defined; just use that one
                analyzerName = synonymAnalyzers.keySet().iterator().next();
            } else {
                reasonForNotExpandingSynonyms = ReasonForNotExpandingSynonyms.NoAnalyzerSpecified;
                return query;
            }
        }
    }

    Analyzer synonymAnalyzer = synonymAnalyzers.get(analyzerName);

    if (synonymAnalyzer == null) { // couldn't find analyzer
        reasonForNotExpandingSynonyms = ReasonForNotExpandingSynonyms.AnalyzerNotFound;
        return query;
    }

    if (solrParams.getBool(Params.SYNONYMS_DISABLE_PHRASE_QUERIES, false)
            && getQueryStringFromParser().indexOf('"') != -1) {
        // disable if a phrase query is detected, i.e. there's a '"'
        reasonForNotExpandingSynonyms = ReasonForNotExpandingSynonyms.IgnoringPhrases;
        return query;
    }

    try {
        query = attemptToApplySynonymsToQuery(query, solrParams, synonymAnalyzer);
    } catch (IOException e) {
        // TODO: better error handling - for now just bail out
        reasonForNotExpandingSynonyms = ReasonForNotExpandingSynonyms.UnhandledException;
        e.printStackTrace(System.err);
    }

    return query;
}

From source file:com.github.healthonnet.search.SynonymExpandingExtendedDismaxQParserPlugin.java

License:Apache License

private Query attemptToApplySynonymsToQuery(Query query, SolrParams solrParams, Analyzer synonymAnalyzer)
        throws IOException {

    List<Query> synonymQueries = generateSynonymQueries(synonymAnalyzer, solrParams);

    boolean ignoreQueryOperators = solrParams.getBool(Params.SYNONYMS_IGNORE_QUERY_OPERATORS, false);
    boolean hasComplexQueryOperators = ignoreQueryOperators ? false
            : Const.COMPLEX_QUERY_OPERATORS_PATTERN.matcher(getQueryStringFromParser()).find();

    if (hasComplexQueryOperators) { // TODO: support complex operators
        reasonForNotExpandingSynonyms = ReasonForNotExpandingSynonyms.HasComplexQueryOperators;
        return query;
    } else if (synonymQueries.isEmpty()) { // didn't find more than 0 synonyms, i.e. it's just the original phrase
        reasonForNotExpandingSynonyms = ReasonForNotExpandingSynonyms.DidntFindAnySynonyms;
        return query;
    }//from w w w.ja v  a  2 s. c om

    float originalBoost = solrParams.getFloat(Params.SYNONYMS_ORIGINAL_BOOST, 1.0F);
    float synonymBoost = solrParams.getFloat(Params.SYNONYMS_SYNONYM_BOOST, 1.0F);

    query = applySynonymQueries(query, synonymQueries, originalBoost, synonymBoost);
    return query;
}

From source file:com.github.healthonnet.search.SynonymExpandingExtendedDismaxQParserPlugin.java

License:Apache License

/**
 * Given the synonymAnalyzer, returns a list of all alternate queries expanded from the original user query.
 * /*from  www  .  j a v  a 2  s. c  o m*/
 * @param synonymAnalyzer
 * @param solrParams
 * @return
 */
private List<Query> generateSynonymQueries(Analyzer synonymAnalyzer, SolrParams solrParams) {

    String origQuery = getQueryStringFromParser();
    int queryLen = origQuery.length();

    // TODO: make the token stream reusable?
    TokenStream tokenStream = synonymAnalyzer.tokenStream(Const.IMPOSSIBLE_FIELD_NAME,
            new StringReader(origQuery));

    SortedSetMultimap<Integer, TextInQuery> startPosToTextsInQuery = TreeMultimap.create();

    boolean constructPhraseQueries = solrParams.getBool(Params.SYNONYMS_CONSTRUCT_PHRASES, false);

    boolean bag = solrParams.getBool(Params.SYNONYMS_BAG, false);
    List<String> synonymBag = new ArrayList<>();

    try {
        tokenStream.reset();
        while (tokenStream.incrementToken()) {
            CharTermAttribute term = tokenStream.getAttribute(CharTermAttribute.class);
            OffsetAttribute offsetAttribute = tokenStream.getAttribute(OffsetAttribute.class);
            TypeAttribute typeAttribute = tokenStream.getAttribute(TypeAttribute.class);

            if (!typeAttribute.type().equals("shingle")) {
                // ignore shingles; we only care about synonyms and the original text
                // TODO: filter other types as well

                String termToAdd = term.toString();

                if (typeAttribute.type().equals("SYNONYM")) {
                    synonymBag.add(termToAdd);
                }

                // Don't quote sibgle term term synonyms
                if (constructPhraseQueries && typeAttribute.type().equals("SYNONYM")
                        && termToAdd.contains(" ")) {
                    // Don't Quote when original is already surrounded by quotes
                    if (offsetAttribute.startOffset() == 0 || offsetAttribute.endOffset() == queryLen
                            || origQuery.charAt(offsetAttribute.startOffset() - 1) != '"'
                            || origQuery.charAt(offsetAttribute.endOffset()) != '"') {
                        // make a phrase out of the synonym
                        termToAdd = new StringBuilder(termToAdd).insert(0, '"').append('"').toString();
                    }
                }
                if (!bag) {
                    // create a graph of all possible synonym combinations,
                    // e.g. dog bite, hound bite, dog nibble, hound nibble, etc.
                    TextInQuery textInQuery = new TextInQuery(termToAdd, offsetAttribute.startOffset(),
                            offsetAttribute.endOffset());

                    startPosToTextsInQuery.put(offsetAttribute.startOffset(), textInQuery);
                }
            }
        }
        tokenStream.end();
    } catch (IOException e) {
        throw new RuntimeException("uncaught exception in synonym processing", e);
    } finally {
        try {
            tokenStream.close();
        } catch (IOException e) {
            throw new RuntimeException("uncaught exception in synonym processing", e);
        }
    }

    List<String> alternateQueries = synonymBag;

    if (!bag) {
        // use a graph rather than a bag
        List<List<TextInQuery>> sortedTextsInQuery = new ArrayList<>(startPosToTextsInQuery.values().size());
        sortedTextsInQuery.addAll(startPosToTextsInQuery.asMap().values().stream().map(ArrayList::new)
                .collect(Collectors.toList()));

        // have to use the start positions and end positions to figure out all possible combinations
        alternateQueries = buildUpAlternateQueries(solrParams, sortedTextsInQuery);
    }

    // save for debugging purposes
    expandedSynonyms = alternateQueries;

    return createSynonymQueries(solrParams, alternateQueries);
}