return getQuery(filter).toString();
}
private SolrQuery getQuery(Filter filter) {
SolrQuery solrQuery = new SolrQuery();
setDefaults(solrQuery);
StringBuilder queryBuilder = new StringBuilder();
for (String pt : filter.getPrimaryTypes()) {
queryBuilder.append("jcr\\:primaryType").append(':').append(partialEscape(pt));
}
Filter.PathRestriction pathRestriction = filter.getPathRestriction();
if (pathRestriction != null) {
String path = purgePath(filter);
String fieldName = configuration.getFieldForPathRestriction(pathRestriction);
if (fieldName != null) {
queryBuilder.append(fieldName);
queryBuilder.append(':');
queryBuilder.append(path);
if (!path.equals("\\/")) {
queryBuilder.append("\\/");
}
// TODO: Also handle other path restriction types
if (pathRestriction.equals(Filter.PathRestriction.ALL_CHILDREN)) {
queryBuilder.append("*");
}
queryBuilder.append(" ");
}
}
Collection<Filter.PropertyRestriction> propertyRestrictions = filter.getPropertyRestrictions();
if (propertyRestrictions != null && !propertyRestrictions.isEmpty()) {
for (Filter.PropertyRestriction pr : propertyRestrictions) {
if (pr.propertyName.contains("/")) {
// cannot handle child-level property restrictions
continue;
}
String first = null;
if (pr.first != null) {
first = partialEscape(String.valueOf(pr.first.getValue(pr.first.getType()))).toString();
}
String last = null;
if (pr.last != null) {
last = partialEscape(String.valueOf(pr.last.getValue(pr.last.getType()))).toString();
}
String prField = configuration.getFieldForPropertyRestriction(pr);
CharSequence fieldName = partialEscape(prField != null ?
prField : pr.propertyName);
if ("jcr\\:path".equals(fieldName.toString())) {
queryBuilder.append(configuration.getPathField());
queryBuilder.append(':');
queryBuilder.append(first);
if (first!= null && !"\\/".equals(first)) {
queryBuilder.append("\\/");
}
} else {
queryBuilder.append(fieldName).append(':');
if (pr.first != null && pr.last != null && pr.first.equals(pr.last)) {
queryBuilder.append(first);
} else if (pr.first == null && pr.last == null) {
queryBuilder.append('*');
} else if ((pr.first != null && pr.last == null) || (pr.last != null && pr.first == null) || (!pr.first.equals(pr.last))) {
// TODO : need to check if this works for all field types (most likely not!)
queryBuilder.append(createRangeQuery(first, last, pr.firstIncluding, pr.lastIncluding));
} else if (pr.isLike) {
// TODO : the current parameter substitution is not expected to work well
queryBuilder.append(partialEscape(String.valueOf(pr.first.getValue(pr.first.getType())).replace('%', '*').replace('_', '?')));
} else {
throw new RuntimeException("[unexpected!] not handled case");
}
}
queryBuilder.append(" ");
}
}
Collection<String> fulltextConditions = filter.getFulltextConditions();
for (String fulltextCondition : fulltextConditions) {
queryBuilder.append(fulltextCondition).append(" ");
}
if(queryBuilder.length() == 0) {
queryBuilder.append("*:*");
}
String escapedQuery = queryBuilder.toString();
solrQuery.setQuery(escapedQuery);
if (log.isDebugEnabled()) {
log.debug("JCR query {} has been converted to Solr query {}",
filter.getQueryStatement(), solrQuery.toString());
}
return solrQuery;
}