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.wso2.carbon.apimgt.impl.APIProviderImpl.java

License:Open Source License

/**
 * To get the query to retrieve user role list query based on current role list.
 *
 * @return the query with user role list.
 * @throws APIManagementException API Management Exception.
 *//*  w ww . j a  v a 2s  .  c o m*/
private String getUserRoleListQuery() throws APIManagementException {
    StringBuilder rolesQuery = new StringBuilder();
    rolesQuery.append('(');
    rolesQuery.append(APIConstants.NULL_USER_ROLE_LIST);
    String[] userRoles = APIUtil.getListOfRoles(userNameWithoutChange);
    if (userRoles != null) {
        for (String userRole : userRoles) {
            rolesQuery.append(" OR ");
            rolesQuery.append(ClientUtils.escapeQueryChars(APIUtil.sanitizeUserRole(userRole.toLowerCase())));
        }
    }
    rolesQuery.append(")");
    if (log.isDebugEnabled()) {
        log.debug("User role list solr query " + APIConstants.PUBLISHER_ROLES + "=" + rolesQuery.toString());
    }
    return APIConstants.PUBLISHER_ROLES + "=" + rolesQuery.toString();
}

From source file:org.xwiki.search.solr.internal.job.IndexerJob.java

License:Open Source License

/**
 * Index document (versions) not yet indexed in the passed wiki.
 * /*from   ww w.  j  av a  2  s . co m*/
 * @param wiki the wiki where to search for documents to index
 * @param document the document found
 * @param solrInstance used to start indexing of a document
 * @throws SolrIndexerException when failing to index new documents
 * @throws IllegalArgumentException when failing to index new documents
 * @throws SolrServerException when failing to index new documents
 */
private void addMissing(String wiki, Object[] document, SolrInstance solrInstance)
        throws SolrIndexerException, IllegalArgumentException, SolrServerException {
    String name = (String) document[0];
    String space = (String) document[1];
    String localeString = (String) document[2];
    String version = (String) document[3];

    DocumentReference reference = createDocumentReference(wiki, space, name, localeString);

    SolrQuery solrQuery = new SolrQuery(
            FieldUtils.ID + ':' + ClientUtils.escapeQueryChars(this.solrResolver.getId(reference)));
    solrQuery.addFilterQuery(FieldUtils.VERSION + ':' + ClientUtils.escapeQueryChars(version));
    solrQuery.setFields(FieldUtils.ID);

    QueryResponse response = solrInstance.query(solrQuery);
    if (response.getResults().getNumFound() == 0) {
        this.indexer.index(reference, true);
    }
}

From source file:org.xwiki.search.solr.internal.reference.AbstractSolrReferenceResolver.java

License:Open Source License

@Override
public String getQuery(EntityReference reference) throws IllegalArgumentException, SolrIndexerException {
    return FieldUtils.ID + ':' + ClientUtils.escapeQueryChars(getId(reference));
}

From source file:org.xwiki.search.solr.internal.reference.DocumentSolrReferenceResolver.java

License:Open Source License

@Override
public String getQuery(EntityReference reference) throws SolrIndexerException {
    StringBuilder builder = new StringBuilder();

    EntityReference spaceReference = reference.extractReference(EntityType.SPACE);
    builder.append(spaceResolverProvider.get().getQuery(spaceReference));

    builder.append(QUERY_AND);/*from   w  ww  .  j ava2 s .  c om*/

    builder.append(FieldUtils.NAME_EXACT);
    builder.append(':');
    builder.append(ClientUtils.escapeQueryChars(reference.getName()));

    return builder.toString();
}

From source file:org.xwiki.search.solr.internal.reference.ObjectSolrReferenceResolver.java

License:Open Source License

@Override
public String getQuery(EntityReference reference) throws SolrIndexerException {
    BaseObjectReference objectReference = new BaseObjectReference(reference);

    StringBuilder builder = new StringBuilder();

    EntityReference spaceReference = reference.extractReference(EntityType.SPACE);
    builder.append(documentResolverProvider.get().getQuery(spaceReference));

    builder.append(QUERY_AND);/*from  w w  w.j  a  va2 s . co  m*/

    builder.append(FieldUtils.CLASS);
    builder.append(':');
    builder.append(
            ClientUtils.escapeQueryChars(this.localSerializer.serialize(objectReference.getXClassReference())));

    builder.append(QUERY_AND);

    builder.append(FieldUtils.NUMBER);
    builder.append(':');
    builder.append(ClientUtils.escapeQueryChars(String.valueOf(objectReference.getObjectNumber())));

    return builder.toString();
}

From source file:org.xwiki.search.solr.internal.reference.SpaceSolrReferenceResolver.java

License:Open Source License

@Override
public String getQuery(EntityReference reference) throws SolrIndexerException {
    StringBuilder builder = new StringBuilder();

    EntityReference wikiReference = reference.extractReference(EntityType.WIKI);
    builder.append(wikiResolverProvider.get().getQuery(wikiReference));

    builder.append(QUERY_AND);/*ww w  .j  a va2 s . c  o m*/

    builder.append(FieldUtils.SPACE_EXACT);
    builder.append(':');
    builder.append(ClientUtils.escapeQueryChars(reference.getName()));

    return builder.toString();
}

From source file:org.xwiki.search.solr.internal.reference.WikiSolrReferenceResolver.java

License:Open Source License

@Override
public String getQuery(EntityReference reference) {
    return FieldUtils.WIKI + ':' + ClientUtils.escapeQueryChars(reference.getName());
}

From source file:podd.search.impl.IndexManager.java

License:Open Source License

/**
 * Delete a podd object from the index if it exists.
 *
 * @param poddObject/*from w w  w  .  jav a  2 s.c  o m*/
 */
public void deleteIndexedPoddObject(Object poddObject) {
    if (poddObject instanceof PoddObject) {
        PoddObject poddObj = (PoddObject) poddObject;
        LOGGER.debug("Deleting: " + poddObj.getPid());

        // Invoke delete on Solr
        if (poddObj.getId() != null) {
            try {
                // Before we delete, we must flush the cache
                synchronized (this) {
                    flush();
                }
                solrServer.deleteByQuery("id:" + ClientUtils.escapeQueryChars(poddObj.getPid()));
                solrServer.commit();
            } catch (SolrServerException e) {
                LOGGER.error("Found exception", e);
            } catch (IOException e) {
                LOGGER.error("Found exception", e);
            }
        }
    }
}

From source file:podd.search.impl.SearchCriteriaImpl.java

License:Open Source License

/**
  * Helper class to put together AND/OR conditions from an array of queries
  */*from   www  .ja va  2 s . com*/
  * @param indexField
  * @param array
  * @param condition
  * @param conditionAfterFirst if the condition is  NOT you'll want this to be false
  * @return
  */
private String buildFromQueryArray(IndexFields indexField, String[] array, String condition,
        boolean conditionAfterFirst) {
    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < array.length; i++) {
        String word = array[i];
        if (StringUtils.hasLength(word)) {
            if (!conditionAfterFirst || sb.length() > 0) {
                sb.append(condition);
            }
            if (indexField != null) {
                sb.append(indexField).append(":");
            }
            sb.append(ClientUtils.escapeQueryChars(word));
        }
    }

    return sb.toString();
}

From source file:podd.search.impl.SearchCriteriaImpl.java

License:Open Source License

@Override
public String getQuery() {
    // Convert the queries into a single query for Solr

    StringBuilder sb = new StringBuilder();

    // All these words
    if (StringUtils.hasText(this.getQueryAllTheseWords())) {
        String[] allTheseWords = this.getQueryAllTheseWords().split(WHITE_SPACE);

        sb.append(buildFromQueryArray(null, allTheseWords, AND, true));
    }//w  ww  . j a v  a2  s. c o m

    // Any these words
    if (StringUtils.hasText(this.getQueryAnyTheseWords())) {
        if (sb.length() > 0) {
            sb.append(AND);
        }

        String[] anyTheseWords = this.getQueryAnyTheseWords().split(WHITE_SPACE);
        sb.append(" ");
        sb.append("(");
        sb.append(buildFromQueryArray(null, anyTheseWords, OR, true));
        sb.append(")");
    }

    // Phrase
    if (StringUtils.hasText(this.getQueryPhrase())) {
        if (sb.length() > 0) {
            sb.append(AND);
        }
        // Remove all existing double quotes
        String phrase = this.getQueryPhrase().replaceAll("\"", "");
        // Now append quotes around the phrase
        sb.append(" ");
        sb.append("(");
        sb.append("\"").append(ClientUtils.escapeQueryChars(phrase)).append("\"");
        sb.append(")");
    }

    // Exclude
    if (StringUtils.hasText(this.getQueryExclude())) {
        String[] excludes = this.getQueryExclude().split(WHITE_SPACE);
        sb.append(buildFromQueryArray(null, excludes, NOT, false));
    }

    // PODD object id
    if (StringUtils.hasText(this.poddObjectId)) {
        if (sb.length() > 0) {
            sb.append(AND);
        }
        sb.append(" ").append(IndexFields.ID.toString()).append(":")
                .append(ClientUtils.escapeQueryChars(this.getPoddObjectId()));
    }

    String q = sb.toString();

    // If there are no query terms, then search on everything
    if (!StringUtils.hasText(q.trim())) {
        q = "*:*";
    }

    LOGGER.info("solr query=" + q);

    return q;
}