Example usage for org.apache.solr.client.solrj.util ClientUtils escapeQueryChars

List of usage examples for org.apache.solr.client.solrj.util ClientUtils escapeQueryChars

Introduction

In this page you can find the example usage for org.apache.solr.client.solrj.util ClientUtils escapeQueryChars.

Prototype


public static String escapeQueryChars(String s) 

Source Link

Document

See: <a href="https://www.google.com/?gws_rd=ssl#q=lucene+query+parser+syntax">Lucene query parser syntax</a> for more information on Escaping Special Characters

Usage

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

License:Apache License

@Override
public Map<String, Node> get(String uri) {
    String escaped = ClientUtils.escapeQueryChars(uri);
    String qs = docDef.getEntityField() + ":" + escaped;
    SolrDocumentList solrResults = solrQuery(qs, 1);

    List<Map<String, Node>> records = process(solrResults);
    if (records.size() == 0)
        return null;
    if (records.size() > 1)
        log.warn("Multiple docs for one URI: " + uri);
    return records.get(0);
}

From source file:org.apache.metron.solr.dao.SolrMetaAlertSearchDao.java

License:Apache License

@Override
public GroupResponse group(GroupRequest groupRequest) throws InvalidSearchException {
    // Make sure to escape any problematic characters here
    String sourceType = ClientUtils.escapeQueryChars(config.getSourceTypeField());
    String baseQuery = groupRequest.getQuery();
    String adjustedQuery = baseQuery + " -" + MetaAlertConstants.METAALERT_FIELD + ":[* TO *]" + " -"
            + sourceType + ":" + MetaAlertConstants.METAALERT_TYPE;
    LOG.debug("MetaAlert group adjusted query: {}", adjustedQuery);
    groupRequest.setQuery(adjustedQuery);
    return solrSearchDao.group(groupRequest);
}

From source file:org.apache.oodt.cas.filemgr.catalog.solr.SolrClient.java

License:Apache License

/**
 * Method to query the Solr index for a product with the specified name.
 * @param name/*from ww w.  j  a  v  a 2 s.  c  om*/
 * @param mimeType
 * @return
 */
public String queryProductByName(String name, String mimeType) throws CatalogException {

    ConcurrentHashMap<String, String[]> params = new ConcurrentHashMap<String, String[]>();
    params.put("q", new String[] { Parameters.PRODUCT_NAME + ":" + ClientUtils.escapeQueryChars(name) });
    return query(params, mimeType);

}

From source file:org.apache.ranger.solr.SolrUtil.java

License:Apache License

private String setFieldForPartialSearch(String fieldName, Object value) {
    if (value == null || value.toString().trim().length() == 0) {
        return null;
    }/*from  www.  j av a  2  s .c  o m*/
    return fieldName + ":*" + ClientUtils.escapeQueryChars(value.toString().trim().toLowerCase()) + "*";
}

From source file:org.apache.ranger.solr.SolrUtil.java

License:Apache License

public String setField(String fieldName, Object value) {
    if (value == null || value.toString().trim().length() == 0) {
        return null;
    }// w  ww  .  j ava2s. com
    return fieldName + ":" + ClientUtils.escapeQueryChars(value.toString().trim().toLowerCase());
}

From source file:org.apache.ranger.solr.SolrUtil.java

License:Apache License

public String orList(String fieldName, Collection<?> valueList) {
    if (valueList == null || valueList.size() == 0) {
        return null;
    }//from   ww w . ja  v a 2  s  . c om
    String expr = "";
    int count = -1;
    for (Object value : valueList) {
        count++;
        if (count > 0) {
            expr += " OR ";
        }
        expr += fieldName + ":" + ClientUtils.escapeQueryChars(value.toString().toLowerCase());
    }
    if (valueList.size() == 0) {
        return expr;
    } else {
        return "(" + expr + ")";
    }

}

From source file:org.apache.ranger.solr.SolrUtil.java

License:Apache License

public String andList(String fieldName, Collection<?> valueList) {
    if (valueList == null || valueList.size() == 0) {
        return null;
    }/*from ww  w .  j a  va 2 s  .c o  m*/
    String expr = "";
    int count = -1;
    for (Object value : valueList) {
        count++;
        if (count > 0) {
            expr += " AND ";
        }
        expr += fieldName + ":" + ClientUtils.escapeQueryChars(value.toString().toLowerCase());
    }
    if (valueList.size() == 0) {
        return expr;
    } else {
        return "(" + expr + ")";
    }
}

From source file:org.codelibs.fess.helper.IndexingHelper.java

License:Apache License

private void deleteOldDocuments(final SolrGroup solrGroup, final List<SolrInputDocument> docList) {
    final FieldHelper fieldHelper = ComponentUtil.getFieldHelper();

    final List<String> docIdList = new ArrayList<String>();
    final StringBuilder q = new StringBuilder(1000);
    final StringBuilder fq = new StringBuilder(100);
    for (final SolrInputDocument inputDoc : docList) {
        final Object idValue = inputDoc.getFieldValue(fieldHelper.idField);
        if (idValue == null) {
            continue;
        }/*from   w  w w.j a v  a  2 s. co m*/

        final Object configIdValue = inputDoc.getFieldValue(fieldHelper.configIdField);
        if (configIdValue == null) {
            continue;
        }

        q.setLength(0);
        q.append(fieldHelper.urlField).append(":\"");
        q.append(ClientUtils.escapeQueryChars((String) inputDoc.getFieldValue(fieldHelper.urlField)));
        q.append('"');

        fq.setLength(0);
        fq.append(fieldHelper.configIdField).append(':');
        fq.append(configIdValue.toString());

        final SolrDocumentList docs = getSolrDocumentList(solrGroup, fq.toString(), q.toString(),
                new String[] { fieldHelper.idField, fieldHelper.docIdField });
        for (final SolrDocument doc : docs) {
            final Object oldIdValue = doc.getFieldValue(fieldHelper.idField);
            if (!idValue.equals(oldIdValue) && oldIdValue != null) {
                final Object oldDocIdValue = doc.getFieldValue(fieldHelper.docIdField);
                if (oldDocIdValue != null) {
                    docIdList.add(oldDocIdValue.toString());
                }
            }
        }
        if (logger.isDebugEnabled()) {
            logger.debug(q + " in " + fq + " => " + docs);
        }
    }
    if (!docIdList.isEmpty()) {
        deleteDocumentsByDocId(solrGroup, docIdList);
    }
}

From source file:org.dspace.app.cris.integration.CRISAuthority.java

/**
 * Return a list of choices performing a lucene query on the RPs names index
 * appending the wildchar to every word in the query (i.e. if the query
 * string is Chan Tse the search will be perfomed with Chan* Tse*). For any
 * matching RP will be returned choices for every variants form.
 * /*from w w w.ja  v a  2 s .c  o  m*/
 * {@link ChoiceAuthority#getMatches(String, int, int, int, String)}
 * 
 * @param query
 *            the lookup string
 * @param collection
 *            (not used by this Authority)
 * @param locale
 *            (not used by this Authority)
 * @param start
 *            (not used by this Authority)
 * @param limit
 *            (not used by this Authority)
 * @param locale
 *            (not used by this Authority)
 * 
 * @return a Choices of RPs where a name form match the query string
 */
public Choices getMatches(String field, String query, int collection, int start, int limit, String locale) {
    try {
        init();
        if (query != null && query.length() > 2) {
            String luceneQuery = ClientUtils.escapeQueryChars(query.toLowerCase()) + "*";
            luceneQuery = luceneQuery.replaceAll("\\\\ ", " ");
            DiscoverQuery discoverQuery = new DiscoverQuery();
            discoverQuery.setDSpaceObjectFilter(getCRISTargetTypeID());
            String filter = configurationService.getProperty("cris." + getPluginName()
                    + ((field != null && !field.isEmpty()) ? "." + field : "") + ".filter");
            if (filter != null) {
                discoverQuery.addFilterQueries(filter);
            }

            discoverQuery.setQuery("{!lucene q.op=AND df=crisauthoritylookup}(" + luceneQuery + ") OR (\""
                    + luceneQuery.substring(0, luceneQuery.length() - 1) + "\")");

            discoverQuery.setMaxResults(50);
            DiscoverResult result = searchService.search(null, discoverQuery, true);

            List<Choice> choiceList = new ArrayList<Choice>();

            for (DSpaceObject dso : result.getDspaceObjects()) {
                ACrisObject cris = (ACrisObject) dso;
                choiceList.add(new Choice(ResearcherPageUtils.getPersistentIdentifier(cris), cris.getName(),
                        cris.getName()));
            }

            Choice[] results = new Choice[choiceList.size()];
            results = choiceList.toArray(results);
            return new Choices(results, 0, results.length, Choices.CF_AMBIGUOUS, false, 0);
        }
        return new Choices(false);
    } catch (Exception e) {
        log.error("Error quering the RPAuthority - " + e.getMessage(), e);
        return new Choices(true);
    }
}

From source file:org.dspace.app.cris.integration.RPAuthority.java

/**
 * Return a list of choices performing a lucene query on the RPs names index
 * appending the wildchar to every word in the query (i.e. if the query
 * string is Chan Tse the search will be perfomed with Chan* Tse*). For any
 * matching RP will be returned choices for every variants form.
 * /*from  ww  w  .  j a  v  a 2 s  . c  om*/
 * {@link ChoiceAuthority#getMatches(String, int, int, int, String)}
 * 
 * @param query
 *            the lookup string
 * @param collection
 *            (not used by this Authority)
 * @param locale
 *            (not used by this Authority)
 * @param start
 *            (not used by this Authority)
 * @param limit
 *            (not used by this Authority)
 * @param locale
 *            (not used by this Authority)
 * 
 * @return a Choices of RPs where a name form match the query string
 */
@Override
public Choices getMatches(String field, String query, int collection, int start, int limit, String locale) {
    try {
        init();
        if (query != null && query.length() > 2) {
            DCPersonName tmpPersonName = new DCPersonName(query.toLowerCase());

            String luceneQuery = "";
            if (StringUtils.isNotBlank(tmpPersonName.getLastName())) {
                luceneQuery += ClientUtils.escapeQueryChars(tmpPersonName.getLastName().trim())
                        + (StringUtils.isNotBlank(tmpPersonName.getFirstNames()) ? "" : "*");
            }

            if (StringUtils.isNotBlank(tmpPersonName.getFirstNames())) {
                luceneQuery += (luceneQuery.length() > 0 ? " " : "")
                        + ClientUtils.escapeQueryChars(tmpPersonName.getFirstNames().trim()) + "*";
            }
            luceneQuery = luceneQuery.replaceAll("\\\\ ", " ");
            DiscoverQuery discoverQuery = new DiscoverQuery();
            discoverQuery.setDSpaceObjectFilter(CrisConstants.RP_TYPE_ID);
            String filter = configurationService.getProperty("cris." + RP_AUTHORITY_NAME
                    + ((field != null && !field.isEmpty()) ? "." + field : "") + ".filter");
            if (filter != null) {
                discoverQuery.addFilterQueries(filter);
            }

            discoverQuery.setQuery("{!lucene q.op=AND df=crisauthoritylookup}(" + luceneQuery + ") OR (\""
                    + luceneQuery.substring(0, luceneQuery.length() - 1) + "\")");
            discoverQuery.setMaxResults(50);
            DiscoverResult result = searchService.search(null, discoverQuery, true);

            List<Choice> choiceList = new ArrayList<Choice>();

            for (DSpaceObject dso : result.getDspaceObjects()) {
                ResearcherPage rp = (ResearcherPage) dso;
                choiceList.add(new Choice(ResearcherPageUtils.getPersistentIdentifier(rp), rp.getFullName(),
                        ResearcherPageUtils.getLabel(rp.getFullName(), rp)));

                if (rp.getTranslatedName() != null
                        && rp.getTranslatedName().getVisibility() == VisibilityConstants.PUBLIC
                        && rp.getTranslatedName().getValue() != null) {
                    choiceList.add(new Choice(ResearcherPageUtils.getPersistentIdentifier(rp),
                            rp.getTranslatedName().getValue(),
                            ResearcherPageUtils.getLabel(rp.getTranslatedName().getValue(), rp)));
                }

                for (RestrictedField variant : rp.getVariants()) {
                    if (variant.getValue() != null && variant.getVisibility() == VisibilityConstants.PUBLIC) {
                        choiceList.add(new Choice(ResearcherPageUtils.getPersistentIdentifier(rp),
                                variant.getValue(), ResearcherPageUtils.getLabel(variant.getValue(), rp)));
                    }
                }
            }

            Choice[] results = new Choice[choiceList.size()];
            results = choiceList.toArray(results);
            return new Choices(results, 0, results.length, Choices.CF_AMBIGUOUS, false, 0);
        }
        return new Choices(false);
    } catch (Exception e) {
        log.error("Error quering the RPAuthority - " + e.getMessage(), e);
        return new Choices(true);
    }
}