use of org.apache.lucene.search.SortField.Type in project lucene-solr by apache.
the class TestSort method testRandomFieldNameSorts.
public void testRandomFieldNameSorts() throws Exception {
SolrQueryRequest req = lrf.makeRequest("q", "*:*");
final int iters = atLeast(5000);
// infinite loop abort when trying to generate a non-blank sort "name"
final int nonBlankAttempts = 37;
for (int i = 0; i < iters; i++) {
final StringBuilder input = new StringBuilder();
final String[] names = new String[TestUtil.nextInt(r, 1, 10)];
final boolean[] reverse = new boolean[names.length];
for (int j = 0; j < names.length; j++) {
names[j] = null;
for (int k = 0; k < nonBlankAttempts && null == names[j]; k++) {
names[j] = TestUtil.randomRealisticUnicodeString(r, 1, 100);
// munge anything that might make this a function
names[j] = names[j].replaceFirst("\\{", "\\{\\{");
names[j] = names[j].replaceFirst("\\(", "\\(\\(");
names[j] = names[j].replaceFirst("(\\\"|\\')", "$1$1z");
names[j] = names[j].replaceFirst("(\\d)", "$1x");
// eliminate pesky problem chars
names[j] = names[j].replaceAll("\\p{Cntrl}|\\p{javaWhitespace}", "");
if (0 == names[j].length()) {
names[j] = null;
}
}
// with luck this bad, never go to vegas
// alternatively: if (null == names[j]) names[j] = "never_go_to_vegas";
assertNotNull("Unable to generate a (non-blank) names[" + j + "] after " + nonBlankAttempts + " attempts", names[j]);
reverse[j] = r.nextBoolean();
input.append(r.nextBoolean() ? " " : "");
input.append(names[j]);
input.append(" ");
input.append(reverse[j] ? "desc," : "asc,");
}
input.deleteCharAt(input.length() - 1);
SortField[] sorts = null;
List<SchemaField> fields = null;
try {
SortSpec spec = SortSpecParsing.parseSortSpec(input.toString(), req);
sorts = spec.getSort().getSort();
fields = spec.getSchemaFields();
} catch (RuntimeException e) {
throw new RuntimeException("Failed to parse sort: " + input, e);
}
assertEquals("parsed sorts had unexpected size", names.length, sorts.length);
assertEquals("parsed sort schema fields had unexpected size", names.length, fields.size());
for (int j = 0; j < names.length; j++) {
assertEquals("sorts[" + j + "] had unexpected reverse: " + input, reverse[j], sorts[j].getReverse());
final Type type = sorts[j].getType();
if (Type.SCORE.equals(type)) {
assertEquals("sorts[" + j + "] is (unexpectedly) type score : " + input, "score", names[j]);
} else if (Type.DOC.equals(type)) {
assertEquals("sorts[" + j + "] is (unexpectedly) type doc : " + input, "_docid_", names[j]);
} else if (Type.CUSTOM.equals(type) || Type.REWRITEABLE.equals(type)) {
fail("sorts[" + j + "] resulted in a '" + type.toString() + "', either sort parsing code is broken, or func/query " + "semantics have gotten broader and munging in this test " + "needs improved: " + input);
} else {
assertEquals("sorts[" + j + "] (" + type.toString() + ") had unexpected field in: " + input, names[j], sorts[j].getField());
assertEquals("fields[" + j + "] (" + type.toString() + ") had unexpected name in: " + input, names[j], fields.get(j).getName());
}
}
}
}
Aggregations