Java Code Examples for org.apache.solr.client.solrj.util.ClientUtils#escapeQueryChars()

The following examples show how to use org.apache.solr.client.solrj.util.ClientUtils#escapeQueryChars() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SolrUtil.java    From ranger with Apache License 2.0 6 votes vote down vote up
public String andList(String fieldName, Collection<?> valueList) {
	if (valueList == null || valueList.isEmpty()) {
		return null;
	}
	String expr = "";
	int count = -1;
	for (Object value : valueList) {
		count++;
		if (count > 0) {
			expr += " AND ";
		}
		expr += fieldName
				+ ":"
				+ ClientUtils.escapeQueryChars(value.toString()
						.toLowerCase());
	}
	if (valueList.isEmpty()) {
		return expr;
	} else {
		return "(" + expr + ")";
	}
}
 
Example 2
Source File: SolrUtil.java    From ranger with Apache License 2.0 6 votes vote down vote up
public String orList(String fieldName, Collection<?> valueList) {
	if (valueList == null || valueList.isEmpty()) {
		return null;
	}
	String expr = "";
	int count = -1;
	for (Object value : valueList) {
		count++;
		if (count > 0) {
			expr += " OR ";
		}
		expr += fieldName
				+ ":"
				+ ClientUtils.escapeQueryChars(value.toString()
						.toLowerCase());
	}
	if (valueList.isEmpty()) {
		return expr;
	} else {
		return "(" + expr + ")";
	}

}
 
Example 3
Source File: SolrExprUtil.java    From scipio-erp with Apache License 2.0 6 votes vote down vote up
/**
     * Escapes all special solr/query characters in the given query term
     * <em>not</em> enclosed in quotes (single term).
     * At current time, this includes at least:
     * <code>+ - && || ! ( ) { } [ ] ^ " ~ * ? : \ /</code> and whitespace.
     * NOTE: The result should NOT be enclosed in quotes; use {@link #escapeTermForQuote} for that.
     * FIXME?: whitespace escaping appears to not always be honored by solr parser?...
     * @see #escapeTermForQuote
     */
    public static String escapeTermPlain(String term) {
        return ClientUtils.escapeQueryChars(term);
        // Reference implementation:
//        StringBuilder sb = new StringBuilder();
//        for (int i = 0; i < s.length(); i++) {
//          char c = s.charAt(i);
//          // These characters are part of the query syntax and must be escaped
//          if (c == '\\' || c == '+' || c == '-' || c == '!'  || c == '(' || c == ')' || c == ':'
//            || c == '^' || c == '[' || c == ']' || c == '\"' || c == '{' || c == '}' || c == '~'
//            || c == '*' || c == '?' || c == '|' || c == '&'  || c == ';' || c == '/'
//            || Character.isWhitespace(c)) {
//            sb.append('\\');
//          }
//          sb.append(c);
//        }
//        return sb.toString();
    }
 
Example 4
Source File: ChildDocTransformerFactory.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
protected static String processPathHierarchyQueryString(String queryString) {
  // if the filter includes a path string, build a lucene query string to match those specific child documents.
  // e.g. /toppings/ingredients/name_s:cocoa -> +_nest_path_:/toppings/ingredients +(name_s:cocoa)
  // ingredients/name_s:cocoa -> +_nest_path_:*/ingredients +(name_s:cocoa)
  int indexOfFirstColon = queryString.indexOf(':');
  if (indexOfFirstColon <= 0) {
    return queryString;// give up
  }
  int indexOfLastPathSepChar = queryString.lastIndexOf(PATH_SEP_CHAR, indexOfFirstColon);
  if (indexOfLastPathSepChar < 0) {
    // regular filter, not hierarchy based.
    return ClientUtils.escapeQueryChars(queryString.substring(0, indexOfFirstColon))
        + ":" + ClientUtils.escapeQueryChars(queryString.substring(indexOfFirstColon + 1));
  }
  final boolean isAbsolutePath = queryString.charAt(0) == PATH_SEP_CHAR;
  String path = ClientUtils.escapeQueryChars(queryString.substring(0, indexOfLastPathSepChar));
  String remaining = queryString.substring(indexOfLastPathSepChar + 1); // last part of path hierarchy

  return
      "+" + NEST_PATH_FIELD_NAME + (isAbsolutePath? ":": ":*\\/") + path
      + " +(" + remaining + ")";
}
 
Example 5
Source File: DateTimeConverters.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Override
public String convert(ReadableInstant source) {
	if (source == null) {
		return null;
	}
	return (ClientUtils.escapeQueryChars(FORMATTER.print(source.getMillis())));
}
 
Example 6
Source File: TextIndexSolr5.java    From BioSolr with Apache License 2.0 5 votes vote down vote up
@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) ;
}
 
Example 7
Source File: SolrUtil.java    From ranger with Apache License 2.0 5 votes vote down vote up
public String setField(String fieldName, Object value) {
	if (value == null || value.toString().trim().length() == 0) {
		return null;
	}
	return fieldName
			+ ":"
			+ ClientUtils.escapeQueryChars(value.toString().trim()
					.toLowerCase());
}
 
Example 8
Source File: SolrMetaAlertSearchDao.java    From metron with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 9
Source File: SolrQueryEscapingEvaluator.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public String evaluate(String expression, Context context) {
  List<Object> l = parseParams(expression, context.getVariableResolver());
  if (l.size() != 1) {
    throw new DataImportHandlerException(SEVERE, "'escapeQueryChars' must have at least one parameter ");
  }
  String s = l.get(0).toString();
  return ClientUtils.escapeQueryChars(s);
}
 
Example 10
Source File: FacetField.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public String getAsFilterQuery() {
  if (_ff.getName().equals("facet_queries")) {
    return _name;
  }
  return 
     ClientUtils.escapeQueryChars( _ff._name ) + ":" + 
     ClientUtils.escapeQueryChars( _name );
}
 
Example 11
Source File: DateTimeConverters.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Override
public String convert(Date source) {
	if (source == null) {
		return null;
	}

	return ClientUtils.escapeQueryChars(FORMATTER.print(source.getTime()));
}
 
Example 12
Source File: DateTimeConverters.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Override
public String convert(LocalDateTime source) {
	if (source == null) {
		return null;
	}
	return ClientUtils.escapeQueryChars(FORMATTER.print(source.toDateTime(DateTimeZone.UTC).getMillis()));
}
 
Example 13
Source File: SolrIndex.java    From titan1withtp3.1 with Apache License 2.0 4 votes vote down vote up
private static String escapeValue(Object value) {
    return ClientUtils.escapeQueryChars(value.toString());
}
 
Example 14
Source File: Solr5Index.java    From incubator-atlas with Apache License 2.0 4 votes vote down vote up
private static String escapeValue(Object value) {
    return ClientUtils.escapeQueryChars(value.toString());
}
 
Example 15
Source File: ElasticSearchUtil.java    From ranger with Apache License 2.0 4 votes vote down vote up
private String filterText(Object value) {
    return ClientUtils.escapeQueryChars(value.toString().trim().toLowerCase());
}
 
Example 16
Source File: SolrUtil.java    From ranger with Apache License 2.0 4 votes vote down vote up
private String setFieldForPartialSearch(String fieldName, Object value) {
	if (value == null || value.toString().trim().length() == 0) {
		return null;
	}
	return fieldName + ":*" + ClientUtils.escapeQueryChars(value.toString().trim().toLowerCase()) + "*";
}
 
Example 17
Source File: AbstractAlfrescoSolrIT.java    From SearchServices with GNU Lesser General Public License v3.0 4 votes vote down vote up
protected String escape(String value)
{
    return ClientUtils.escapeQueryChars(value);
}
 
Example 18
Source File: Solr6Index.java    From atlas with Apache License 2.0 4 votes vote down vote up
private static String escapeValue(Object value) {
    return ClientUtils.escapeQueryChars(value.toString());
}