Example usage for org.apache.solr.common.params CommonParams DF

List of usage examples for org.apache.solr.common.params CommonParams DF

Introduction

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

Prototype

String DF

To view the source code for org.apache.solr.common.params CommonParams DF.

Click Source Link

Document

default query field

Usage

From source file:org.alfresco.solr.query.AbstractQParser.java

License:Open Source License

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"));
                }/*ww w .j a  v  a 2  s. c  om*/

                // 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);
}

From source file:org.apache.jena.query.text.TextIndexSolr.java

License:Apache License

private SolrDocumentList solrQuery(String qs, int limit) {
    SolrQuery sq = new SolrQuery(qs);
    sq.setIncludeScore(true);//from  w w  w.ja  v a2  s.c  o  m
    if (limit > 0)
        sq.setRows(limit);
    else
        sq.setRows(MAX_N); // The Solr default is 10.
    try {
        // Set default field.
        sq.add(CommonParams.DF, docDef.getPrimaryField());
        QueryResponse rsp = solrServer.query(sq);
        SolrDocumentList docs = rsp.getResults();
        return docs;
    } catch (SolrServerException e) {
        exception(e);
        return null;
    }
}

From source file:org.apache.lucene.benchmark.quality.mc.SolrSearcher.java

License:Apache License

private void search(String q, String QueryID) throws SolrServerException {

    SolrQuery query = new SolrQuery(q.replace("?", ""));
    query.setParam(CommonParams.DF, defaultField);
    query.setFields("id", "score");
    query.setStart(0);//from w w w. j  av  a2  s  . c  o m
    query.setRows(1000);
    query.setSort("score", SolrQuery.ORDER.desc);
    query.addSort("id", SolrQuery.ORDER.asc);

    int i = 0;

    for (SolrDocument document : server.query(query).getResults()) {

        String docno = (String) document.getFieldValue("id");
        Float score = (Float) document.getFieldValue("score");

        output.println(QueryID + "\tQ0\tMilliyet_0105_v00_" + docno.trim() + "\t" + i + "\t" + score + "\t"
                + getRunName());
        i++;
    }

}

From source file:org.apache.lucene.payload.dwarf.PayloadQParser.java

License:Apache License

@Override
public Query parse() throws SyntaxError {
    String qstr = getString();/*from  ww  w. j  a  va2 s  .com*/
    if (qstr == null || qstr.length() == 0)
        return null;

    String defaultField = getParam(CommonParams.DF);
    if (defaultField == null) {
        defaultField = getReq().getSchema().getDefaultSearchFieldName();
    }
    pqParser = new PayloadQueryParser(this, defaultField);

    pqParser.setDefaultOperator(
            QueryParsing.getQueryParserDefaultOperator(getReq().getSchema(), getParam(QueryParsing.OP)));

    return pqParser.parse(qstr);
}

From source file:org.codice.solr.xpath.XpathQParser.java

License:Open Source License

@Override
public Query parse() throws SyntaxError {
    String qstr = getString();/* w w w  .  j  av a2 s  . c om*/

    String defaultField = getParam(CommonParams.DF);
    if (defaultField == null) {
        defaultField = getReq().getSchema().getDefaultSearchFieldName();
    }

    XpathQueryParser queryParser = new XpathQueryParser(this, defaultField);

    queryParser.setDefaultOperator(
            QueryParsing.getQueryParserDefaultOperator(getReq().getSchema(), getParam(QueryParsing.OP)));

    return queryParser.parse(qstr);
}

From source file:org.dice.solrenhancements.morelikethis.DiceMoreLikeThisHandler.java

License:Apache License

private void configureSolrParameters(SolrQueryRequest req, ModifiableSolrParams params, String uniqueKeyField) {

    // default to the the edismax parser
    String defType = params.get(QueryParsing.DEFTYPE, EDISMAX);
    // allow useage of custom edismax implementations, such as our own
    if (defType.toLowerCase().contains(EDISMAX.toLowerCase())) {
        params.set(DisMaxParams.MM, 0);/* w w w .  j av a2  s. com*/
        // edismax blows up without df field, even if you specify the field to match on in the query
        params.set(CommonParams.DF, uniqueKeyField);
    }
    params.set(QueryParsing.DEFTYPE, defType);
    req.setParams(params);
}

From source file:org.eclipse.rdf4j.sail.solr.SolrIndex.java

License:Open Source License

@Override
protected Iterable<? extends DocumentResult> geoRelationQuery(String relation, URI geoProperty, Shape shape,
        Var contextVar) throws MalformedQueryException, IOException {
    String spatialOp = toSpatialOp(relation);
    if (spatialOp == null) {
        return null;
    }//from   w  w  w.j a  v  a  2s  .  c  o  m
    String wkt = toWkt(shape);
    String qstr = "\"" + spatialOp + "(" + wkt + ")\"";
    if (contextVar != null) {
        Resource ctx = (Resource) contextVar.getValue();
        String tq = termQuery(SearchFields.CONTEXT_FIELD_NAME, SearchFields.getContextID(ctx));
        if (ctx != null) {
            qstr = tq + " AND " + qstr;
        } else {
            qstr = "-" + tq + " AND " + qstr;
        }
    }
    SolrQuery q = new SolrQuery(qstr);
    q.set(CommonParams.DF, SearchFields.getPropertyField(geoProperty));
    q.addField(SearchFields.URI_FIELD_NAME);
    // ':' is part of the fl parameter syntax so we can't use the full
    // property field name
    // instead we use wildcard + local part of the property URI
    q.addField("*" + geoProperty.getLocalName());
    boolean requireContext = (contextVar != null && !contextVar.hasValue());
    if (requireContext) {
        q.addField(SearchFields.CONTEXT_FIELD_NAME);
    }

    QueryResponse response;
    try {
        response = search(q);
    } catch (SolrServerException e) {
        throw new IOException(e);
    }

    SolrDocumentList results = response.getResults();
    return Iterables.transform(results, new Function<SolrDocument, DocumentResult>() {

        @Override
        public DocumentResult apply(SolrDocument document) {
            SolrSearchDocument doc = new SolrSearchDocument(document);
            return new SolrDocumentResult(doc);
        }
    });
}

From source file:org.eclipse.rdf4j.sail.solr.SolrIndex.java

License:Open Source License

private SolrQuery prepareQuery(URI propertyURI, SolrQuery query) {
    // check out which query parser to use, based on the given property URI
    if (propertyURI == null)
        // if we have no property given, we create a default query parser which
        // has the TEXT_FIELD_NAME as the default field
        query.set(CommonParams.DF, SearchFields.TEXT_FIELD_NAME);
    else//from  w w  w . j a v  a2 s . c o m
        // otherwise we create a query parser that has the given property as
        // the default field
        query.set(CommonParams.DF, SearchFields.getPropertyField(propertyURI));
    return query;
}

From source file:org.sindice.siren.solr.qparser.SirenQParser.java

License:Apache License

/**
 * Uses {@link SolrPluginUtils#parseFieldBoosts(String)} with the 'qf'
 * parameter. Falls back to the 'df' parameter or
 * {@link org.apache.solr.schema.IndexSchema#getDefaultSearchFieldName()}.
 *//*  www.j a  va 2  s .  c o m*/
public static Map<String, Float> parseQueryFields(final IndexSchema indexSchema, final SolrParams solrParams)
        throws ParseException {
    final Map<String, Float> queryFields = SolrPluginUtils
            .parseFieldBoosts(solrParams.getParams(SirenParams.QF));
    if (queryFields.isEmpty()) {
        final String df = QueryParsing.getDefaultField(indexSchema, solrParams.get(CommonParams.DF));
        if (df == null) {
            throw new ParseException("Neither " + SirenParams.QF + ", " + CommonParams.DF
                    + ", nor the default search field are present.");
        }
        queryFields.put(df, 1.0f);
    }
    checkFieldTypes(indexSchema, queryFields);
    return queryFields;
}

From source file:org.tallison.solr.search.concordance.KeywordCooccurRankHandler.java

License:Apache License

public static String getField(SolrParams params) {
    String fieldName = params.get(CommonParams.FIELD);
    if (fieldName == null || fieldName.equalsIgnoreCase("null")) {

        if (fieldName == null || fieldName.equalsIgnoreCase("null"))
            fieldName = params.get(CommonParams.DF);

        if (fieldName == null || fieldName.equalsIgnoreCase("null")) {
            //check field list if not in field
            fieldName = params.get(CommonParams.FL);

            //TODO: change when/if request allows for multiple terms
            if (fieldName != null)
                fieldName = fieldName.split(",")[0].trim();
        }/* w w w  .j  a  va 2  s .co m*/
    }
    return fieldName;
}