use of com.yahoo.collections.Pair in project vespa by vespa-engine.
the class SummaryNamesFieldCollisions method process.
@Override
public void process(boolean validate) {
if (!validate)
return;
Map<String, Pair<String, String>> fieldToClassAndSource = new HashMap<>();
for (DocumentSummary summary : search.getSummaries().values()) {
if ("default".equals(summary.getName()))
continue;
for (SummaryField summaryField : summary.getSummaryFields()) {
if (summaryField.isImplicit())
continue;
Pair<String, String> prevClassAndSource = fieldToClassAndSource.get(summaryField.getName());
for (Source source : summaryField.getSources()) {
if (prevClassAndSource != null) {
String prevClass = prevClassAndSource.getFirst();
String prevSource = prevClassAndSource.getSecond();
if (!prevClass.equals(summary.getName())) {
if (!prevSource.equals(source.getName())) {
throw new IllegalArgumentException("For search '" + search.getName() + "', summary class '" + summary.getName() + "'," + " summary field '" + summaryField.getName() + "':" + " Can not use source '" + source.getName() + "' for this summary field, an equally named field in summary class '" + prevClass + "' uses a different source: '" + prevSource + "'.");
}
}
} else {
fieldToClassAndSource.put(summaryField.getName(), new Pair<>(summary.getName(), source.getName()));
}
}
}
}
}
use of com.yahoo.collections.Pair in project vespa by vespa-engine.
the class DocumentProcessorModelBuilder method parseFieldNameSchemaMap.
/**
* Parses a schemamapping element and generates a map of field mappings
*
* @param e a schemamapping element
* @return doctype,in-document → in-processor
*/
public static Map<Pair<String, String>, String> parseFieldNameSchemaMap(Element e) {
Map<Pair<String, String>, String> ret = new HashMap<>();
for (Element sm : XML.getChildren(e, "map")) {
for (Element fm : XML.getChildren(sm, "field")) {
String from = fm.getAttribute("in-document");
String to = fm.getAttribute("in-processor");
String doctype = fm.getAttribute("doctype");
if ("".equals(doctype))
doctype = null;
ret.put(new Pair<>(doctype, from), to);
}
}
return ret;
}
use of com.yahoo.collections.Pair in project vespa by vespa-engine.
the class RankingExpressionInliningTestCase method testConstants.
@Test
public void testConstants() throws ParseException {
RankProfileRegistry rankProfileRegistry = new RankProfileRegistry();
SearchBuilder builder = new SearchBuilder(rankProfileRegistry);
builder.importString("search test {\n" + " document test { \n" + " field a type string { \n" + " indexing: index \n" + " }\n" + " }\n" + " \n" + " rank-profile parent {\n" + " constants {\n" + " p1: 7 \n" + " p2: 0 \n" + " }\n" + " first-phase {\n" + " expression: p1 + foo\n" + " }\n" + " second-phase {\n" + " expression: p2 * foo\n" + " }\n" + " macro inline foo() {\n" + " expression: 3 + p1 + p2\n" + " }\n" + " }\n" + " rank-profile child inherits parent {\n" + " first-phase {\n" + " expression: p1 + foo + baz + bar + arg(4.0)\n" + " }\n" + " constants {\n" + " p2: 2.0 \n" + " }\n" + " macro bar() {\n" + " expression: p2*p1\n" + " }\n" + " macro inline baz() {\n" + " expression: p2+p1+boz\n" + " }\n" + " macro inline boz() {\n" + " expression: 3.0\n" + " }\n" + " macro inline arg(a1) {\n" + " expression: a1*2\n" + " }\n" + " }\n" + "\n" + "}\n");
builder.build();
Search s = builder.getSearch();
RankProfile parent = rankProfileRegistry.getRankProfile(s, "parent").compile(new QueryProfileRegistry());
assertEquals("17.0", parent.getFirstPhaseRanking().getRoot().toString());
assertEquals("0.0", parent.getSecondPhaseRanking().getRoot().toString());
List<Pair<String, String>> parentRankProperties = new RawRankProfile(parent, new QueryProfileRegistry(), new AttributeFields(s)).configProperties();
assertEquals("(rankingExpression(foo).rankingScript,10.0)", parentRankProperties.get(0).toString());
assertEquals("(rankingExpression(firstphase).rankingScript,17.0)", parentRankProperties.get(2).toString());
assertEquals("(rankingExpression(secondphase).rankingScript,0.0)", parentRankProperties.get(4).toString());
RankProfile child = rankProfileRegistry.getRankProfile(s, "child").compile(new QueryProfileRegistry());
assertEquals("31.0 + bar + arg(4.0)", child.getFirstPhaseRanking().getRoot().toString());
assertEquals("24.0", child.getSecondPhaseRanking().getRoot().toString());
List<Pair<String, String>> childRankProperties = new RawRankProfile(child, new QueryProfileRegistry(), new AttributeFields(s)).configProperties();
assertEquals("(rankingExpression(foo).rankingScript,12.0)", childRankProperties.get(0).toString());
assertEquals("(rankingExpression(bar).rankingScript,14.0)", childRankProperties.get(1).toString());
assertEquals("(rankingExpression(boz).rankingScript,3.0)", childRankProperties.get(2).toString());
assertEquals("(rankingExpression(baz).rankingScript,9.0 + rankingExpression(boz))", childRankProperties.get(3).toString());
assertEquals("(rankingExpression(arg).rankingScript,a1 * 2)", childRankProperties.get(4).toString());
assertEquals("(rankingExpression(firstphase).rankingScript,31.0 + rankingExpression(bar) + rankingExpression(arg@))", censorBindingHash(childRankProperties.get(7).toString()));
assertEquals("(rankingExpression(secondphase).rankingScript,24.0)", childRankProperties.get(9).toString());
}
use of com.yahoo.collections.Pair in project vespa by vespa-engine.
the class RankingExpressionsTestCase method testMacros.
@Test
public void testMacros() throws IOException, ParseException {
RankProfileRegistry rankProfileRegistry = new RankProfileRegistry();
Search search = SearchBuilder.createFromDirectory("src/test/examples/rankingexpressionfunction", rankProfileRegistry, new QueryProfileRegistry()).getSearch();
final RankProfile macrosRankProfile = rankProfileRegistry.getRankProfile(search, "macros");
macrosRankProfile.parseExpressions();
final Map<String, RankProfile.Macro> macros = macrosRankProfile.getMacros();
assertEquals(2, macros.get("titlematch$").getFormalParams().size());
assertEquals("var1", macros.get("titlematch$").getFormalParams().get(0));
assertEquals("var2", macros.get("titlematch$").getFormalParams().get(1));
assertEquals("var1 * var2 + 890", macros.get("titlematch$").getTextualExpression().trim());
assertEquals("var1 * var2 + 890", macros.get("titlematch$").getRankingExpression().getRoot().toString());
assertEquals("0.8+0.2*titlematch$(4,5)+0.8*titlematch$(7,8)*closeness(distance)", macrosRankProfile.getFirstPhaseRankingString().trim());
assertEquals("78 + closeness(distance)", macros.get("artistmatch").getTextualExpression().trim());
assertEquals(0, macros.get("artistmatch").getFormalParams().size());
List<Pair<String, String>> rankProperties = new RawRankProfile(macrosRankProfile, new QueryProfileRegistry(), new AttributeFields(search)).configProperties();
assertEquals(6, rankProperties.size());
assertEquals("rankingExpression(titlematch$).rankingScript", rankProperties.get(0).getFirst());
assertEquals("var1 * var2 + 890", rankProperties.get(0).getSecond());
assertEquals("rankingExpression(artistmatch).rankingScript", rankProperties.get(1).getFirst());
assertEquals("78 + closeness(distance)", rankProperties.get(1).getSecond());
assertEquals("rankingExpression(firstphase).rankingScript", rankProperties.get(5).getFirst());
assertEquals("0.8 + 0.2 * rankingExpression(titlematch$@126063073eb2deb.ab95cd69909927c) + 0.8 * rankingExpression(titlematch$@c7e4c2d0e6d9f2a1.1d4ed08e56cce2e6) * closeness(distance)", rankProperties.get(5).getSecond());
assertEquals("rankingExpression(titlematch$@c7e4c2d0e6d9f2a1.1d4ed08e56cce2e6).rankingScript", rankProperties.get(3).getFirst());
assertEquals("7 * 8 + 890", rankProperties.get(3).getSecond());
assertEquals("rankingExpression(titlematch$@126063073eb2deb.ab95cd69909927c).rankingScript", rankProperties.get(2).getFirst());
assertEquals("4 * 5 + 890", rankProperties.get(2).getSecond());
}
use of com.yahoo.collections.Pair in project vespa by vespa-engine.
the class RankingExpressionShadowingTestCase method testMacroShadowingArguments.
@Test
public void testMacroShadowingArguments() throws ParseException {
RankProfileRegistry rankProfileRegistry = new RankProfileRegistry();
SearchBuilder builder = new SearchBuilder(rankProfileRegistry);
builder.importString("search test {\n" + " document test { \n" + " field a type string { \n" + " indexing: index \n" + " }\n" + " }\n" + " \n" + " rank-profile test {\n" + " macro sin(x) {\n" + " expression: x * x\n" + " }\n" + " first-phase {\n" + " expression: cos(sin(2*2)) + sin(cos(1+4))\n" + " }\n" + " }\n" + "\n" + "}\n");
builder.build();
Search s = builder.getSearch();
RankProfile test = rankProfileRegistry.getRankProfile(s, "test").compile(new QueryProfileRegistry());
List<Pair<String, String>> testRankProperties = new RawRankProfile(test, new QueryProfileRegistry(), new AttributeFields(s)).configProperties();
assertEquals("(rankingExpression(sin).rankingScript,x * x)", testRankProperties.get(0).toString());
assertEquals("(rankingExpression(sin@).rankingScript,4.0 * 4.0)", censorBindingHash(testRankProperties.get(1).toString()));
assertEquals("(rankingExpression(sin@).rankingScript,cos(5.0) * cos(5.0))", censorBindingHash(testRankProperties.get(2).toString()));
assertEquals("(vespa.rank.firstphase,rankingExpression(firstphase))", censorBindingHash(testRankProperties.get(3).toString()));
assertEquals("(rankingExpression(firstphase).rankingScript,cos(rankingExpression(sin@)) + rankingExpression(sin@))", censorBindingHash(testRankProperties.get(4).toString()));
}
Aggregations