}
}
Query<?> query = null;
Set<ObjectType> types = getTypes();
ObjectType selectedType = getSelectedType();
Set<ObjectType> validTypes = findValidTypes();
Set<UUID> validTypeIds = null;
boolean isAllSearchable = true;
if (selectedType != null) {
isAllSearchable = Content.Static.isSearchableType(selectedType);
query = Query.fromType(selectedType);
} else {
for (ObjectType type : validTypes) {
if (!Content.Static.isSearchableType(type)) {
isAllSearchable = false;
}
}
if (types.size() == 1) {
for (ObjectType type : types) {
query = Query.fromType(type);
break;
}
} else {
query = isAllSearchable ? Query.fromGroup(Content.SEARCHABLE_GROUP) : Query.fromAll();
if (!validTypes.isEmpty()) {
validTypeIds = new HashSet<UUID>();
for (ObjectType t : validTypes) {
validTypeIds.add(t.getId());
}
}
}
}
String sort = getSort();
boolean metricSort = false;
if (RELEVANT_SORT_VALUE.equals(sort)) {
if (isAllSearchable) {
query.sortRelevant(100.0, "_label = ?", queryString);
query.sortRelevant(10.0, "_label matches ?", queryString);
}
} else if (sort != null) {
ObjectField sortField = selectedType != null ?
selectedType.getFieldGlobally(sort) :
Database.Static.getDefault().getEnvironment().getField(sort);
if (sortField != null) {
if (sortField.isMetric()) {
metricSort = true;
}
String sortName = selectedType != null ?
selectedType.getInternalName() + "/" + sort :
sort;
if (ObjectField.TEXT_TYPE.equals(sortField.getInternalType())) {
query.sortAscending(sortName);
} else {
query.sortDescending(sortName);
}
if (!isShowMissing()) {
query.and(sortName + " != missing");
}
}
}
if (metricSort) {
// Skip Solr-related operations if sorting by metrics.
} else if (ObjectUtils.isBlank(queryString)) {
if (isAllSearchable) {
query.and("* ~= *");
}
} else {
// Strip http: or https: from the query for search by path below.
if (queryString.length() > 8 &&
StringUtils.matches(queryString, "(?i)https?://.*")) {
int slashAt = queryString.indexOf("/", 8);
if (slashAt > -1) {
queryString = queryString.substring(slashAt);
}
}
// Search by path.
if (isAllSearchable && queryString.startsWith("/") && queryString.length() > 1) {
List<String> paths = new ArrayList<String>();
for (Directory directory : Query.
from(Directory.class).
where("path ^=[c] ?", queryString).
selectAll()) {
paths.add(directory.getRawPath());
}
int lastSlashAt = queryString.lastIndexOf("/");
if (lastSlashAt > 0) {
for (Directory directory : Query.
from(Directory.class).
where("path ^=[c] ?", queryString.substring(0, lastSlashAt)).
selectAll()) {
paths.add(directory.getRawPath() + queryString.substring(lastSlashAt + 1));
}
}
query.and(Directory.PATHS_FIELD + " ^= ?", paths);
// Full text search.
} else if (isAllSearchable) {
int lastSpaceAt = queryString.lastIndexOf(" ");
if (lastSpaceAt > -1) {
query.and("* ~= ?", Arrays.asList(queryString, queryString.substring(0, lastSpaceAt)));
} else {
query.and("* ~= ?", queryString);
}
} else if (selectedType != null) {
for (String field : selectedType.getLabelFields()) {
if (selectedType.getIndex(field) != null) {
query.and(selectedType.getInternalName() + "/" + field + " contains[c] ?", queryString);
}
break;
}
} else {
Predicate predicate = null;
for (ObjectType type : validTypes) {
String prefix = type.getInternalName() + "/";
for (String field : type.getLabelFields()) {
if (type.getIndex(field) != null) {
predicate = CompoundPredicate.combine(
PredicateParser.OR_OPERATOR,
predicate,
PredicateParser.Static.parse(prefix + field + " ^=[c] ?", queryString));
}
break;
}
}
query.and(predicate);
}
}
if (isOnlyPathed()) {
query.and(Directory.Static.hasPathPredicate());
}
if (isAllSearchable && selectedType == null) {
DatabaseEnvironment environment = Database.Static.getDefault().getEnvironment();
String q = getQueryString();
if (!ObjectUtils.isBlank(q)) {
q = q.replaceAll("\\s+", "").toLowerCase(Locale.ENGLISH);
for (ObjectType t : environment.getTypes()) {
String name = t.getDisplayName();
if (!ObjectUtils.isBlank(name) &&
q.contains(name.replaceAll("\\s+", "").toLowerCase(Locale.ENGLISH))) {
query.sortRelevant(20.0, "_type = ?", t.as(ToolUi.class).findDisplayTypes());
}
}
}
query.sortRelevant(10.0, "_type = ?", environment.getTypeByClass(Singleton.class).as(ToolUi.class).findDisplayTypes());
}
String additionalPredicate = getAdditionalPredicate();
if (!ObjectUtils.isBlank(additionalPredicate)) {
Object parent = Query.
from(Object.class).
where("_id = ?", getParentId()).
first();
if (parent == null && getParentTypeId() != null) {
ObjectType parentType = ObjectType.getInstance(getParentTypeId());
parent = parentType.createObject(null);
}
query.and(additionalPredicate, parent);
}
String advancedQuery = getAdvancedQuery();