use of com.yahoo.search.query.Sorting.AttributeSorter in project vespa by vespa-engine.
the class YqlParserTestCase method testAnnotatedOrdering.
@Test
public void testAnnotatedOrdering() {
assertParse("select foo from bar where title contains \"madonna\"" + " order by [{\"function\": \"uca\", \"locale\": \"en_US\", \"strength\": \"IDENTICAL\"}]other desc" + " limit 600" + " timeout 3;", "title:madonna");
final FieldOrder fieldOrder = parser.getSorting().fieldOrders().get(0);
assertEquals("other", fieldOrder.getFieldName());
assertEquals(Order.DESCENDING, fieldOrder.getSortOrder());
final AttributeSorter sorter = fieldOrder.getSorter();
assertEquals(UcaSorter.class, sorter.getClass());
final UcaSorter uca = (UcaSorter) sorter;
assertEquals("en_US", uca.getLocale());
assertEquals(UcaSorter.Strength.IDENTICAL, uca.getStrength());
}
use of com.yahoo.search.query.Sorting.AttributeSorter in project vespa by vespa-engine.
the class YqlParser method fetchSorting.
@NonNull
private OperatorNode<?> fetchSorting(OperatorNode<?> ast) {
if (ast.getOperator() != SequenceOperator.SORT)
return ast;
List<FieldOrder> sortingInit = new ArrayList<>();
List<OperatorNode<?>> sortArguments = ast.getArgument(1);
for (OperatorNode<?> op : sortArguments) {
OperatorNode<ExpressionOperator> fieldNode = op.<OperatorNode<ExpressionOperator>>getArgument(0);
String field = fetchFieldRead(fieldNode);
String locale = getAnnotation(fieldNode, SORTING_LOCALE, String.class, null, "locale used by sorting function");
String function = getAnnotation(fieldNode, SORTING_FUNCTION, String.class, null, "sorting function for the specified attribute");
String strength = getAnnotation(fieldNode, SORTING_STRENGTH, String.class, null, "strength for sorting function");
AttributeSorter sorter;
if (function == null) {
sorter = new AttributeSorter(field);
} else if (Sorting.LOWERCASE.equals(function)) {
sorter = new LowerCaseSorter(field);
} else if (Sorting.RAW.equals(function)) {
sorter = new RawSorter(field);
} else if (Sorting.UCA.equals(function)) {
if (locale != null) {
UcaSorter.Strength ucaStrength = UcaSorter.Strength.UNDEFINED;
if (strength != null) {
if (Sorting.STRENGTH_PRIMARY.equalsIgnoreCase(strength)) {
ucaStrength = UcaSorter.Strength.PRIMARY;
} else if (Sorting.STRENGTH_SECONDARY.equalsIgnoreCase(strength)) {
ucaStrength = UcaSorter.Strength.SECONDARY;
} else if (Sorting.STRENGTH_TERTIARY.equalsIgnoreCase(strength)) {
ucaStrength = UcaSorter.Strength.TERTIARY;
} else if (Sorting.STRENGTH_QUATERNARY.equalsIgnoreCase(strength)) {
ucaStrength = UcaSorter.Strength.QUATERNARY;
} else if (Sorting.STRENGTH_IDENTICAL.equalsIgnoreCase(strength)) {
ucaStrength = UcaSorter.Strength.IDENTICAL;
} else {
throw newUnexpectedArgumentException(function, Sorting.STRENGTH_PRIMARY, Sorting.STRENGTH_SECONDARY, Sorting.STRENGTH_TERTIARY, Sorting.STRENGTH_QUATERNARY, Sorting.STRENGTH_IDENTICAL);
}
sorter = new UcaSorter(field, locale, ucaStrength);
} else {
sorter = new UcaSorter(field, locale, ucaStrength);
}
} else {
sorter = new UcaSorter(field);
}
} else {
throw newUnexpectedArgumentException(function, "lowercase", "raw", "uca");
}
switch((SortOperator) op.getOperator()) {
case ASC:
sortingInit.add(new FieldOrder(sorter, Order.ASCENDING));
break;
case DESC:
sortingInit.add(new FieldOrder(sorter, Order.DESCENDING));
break;
default:
throw newUnexpectedArgumentException(op.getOperator(), SortOperator.ASC, SortOperator.DESC);
}
}
sorting = new Sorting(sortingInit);
return ast.getArgument(0);
}
use of com.yahoo.search.query.Sorting.AttributeSorter in project vespa by vespa-engine.
the class MinimalQueryInserterTestCase method testOrdering.
@Test
public void testOrdering() {
{
String yql = "select%20ignoredfield%20from%20ignoredsource%20where%20title%20contains%20%22madonna%22%20order%20by%20something%2C%20shoesize%20desc%20limit%20300%20timeout%203%3B";
Query query = new Query("search/?yql=" + yql);
execution.search(query);
assertEquals(2, query.getRanking().getSorting().fieldOrders().size());
assertEquals("something", query.getRanking().getSorting().fieldOrders().get(0).getFieldName());
assertEquals(Order.ASCENDING, query.getRanking().getSorting().fieldOrders().get(0).getSortOrder());
assertEquals("shoesize", query.getRanking().getSorting().fieldOrders().get(1).getFieldName());
assertEquals(Order.DESCENDING, query.getRanking().getSorting().fieldOrders().get(1).getSortOrder());
assertEquals("select ignoredfield from ignoredsource where title contains \"madonna\" order by something, shoesize desc limit 300 timeout 3;", query.yqlRepresentation());
}
{
String yql = "select%20ignoredfield%20from%20ignoredsource%20where%20title%20contains%20%22madonna%22%20order%20by%20other%20limit%20300%20timeout%203%3B";
Query query = new Query("search/?yql=" + yql);
execution.search(query);
assertEquals("other", query.getRanking().getSorting().fieldOrders().get(0).getFieldName());
assertEquals(Order.ASCENDING, query.getRanking().getSorting().fieldOrders().get(0).getSortOrder());
assertEquals("select ignoredfield from ignoredsource where title contains \"madonna\" order by other limit 300 timeout 3;", query.yqlRepresentation());
}
{
String yql = "select%20foo%20from%20bar%20where%20title%20contains%20%22madonna%22%20order%20by%20%5B%7B%22function%22%3A%20%22uca%22%2C%20%22locale%22%3A%20%22en_US%22%2C%20%22strength%22%3A%20%22IDENTICAL%22%7D%5Dother%20desc%2C%20%5B%7B%22function%22%3A%20%22lowercase%22%7D%5Dsomething%20limit%20300%20timeout%203%3B";
Query query = new Query("search/?yql=" + yql);
execution.search(query);
{
FieldOrder fieldOrder = query.getRanking().getSorting().fieldOrders().get(0);
assertEquals("other", fieldOrder.getFieldName());
assertEquals(Order.DESCENDING, fieldOrder.getSortOrder());
AttributeSorter sorter = fieldOrder.getSorter();
assertEquals(UcaSorter.class, sorter.getClass());
UcaSorter uca = (UcaSorter) sorter;
assertEquals("en_US", uca.getLocale());
assertEquals(UcaSorter.Strength.IDENTICAL, uca.getStrength());
}
{
FieldOrder fieldOrder = query.getRanking().getSorting().fieldOrders().get(1);
assertEquals("something", fieldOrder.getFieldName());
assertEquals(Order.ASCENDING, fieldOrder.getSortOrder());
AttributeSorter sorter = fieldOrder.getSorter();
assertEquals(LowerCaseSorter.class, sorter.getClass());
}
assertEquals("select foo from bar where title contains \"madonna\" order by [{\"function\": \"uca\", \"locale\": \"en_US\", \"strength\": \"IDENTICAL\"}]other desc, [{\"function\": \"lowercase\"}]something limit 300 timeout 3;", query.yqlRepresentation());
}
}
Aggregations