use of com.xiaomi.linden.thrift.common.LindenValue in project linden by XiaoMi.
the class TestBQL method testScoreModel.
@Test
public void testScoreModel() {
String bql = "select * from linden where title = 'qq' " + "using score model test (Float link = 1)" + " begin" + " return score();" + " end";
LindenSearchRequest lindenRequest = compiler.compile(bql).getSearchRequest();
Assert.assertEquals(new LindenTerm("title", "qq"), lindenRequest.getFilter().getTermFilter().getTerm());
LindenScoreModel lindenScoreModel = new LindenScoreModel().setName("test").setFunc("return score();");
lindenScoreModel.addToParams(new LindenInputParam("link").setValue(new LindenValue().setDoubleValue(1)));
Assert.assertTrue(lindenRequest.getQuery().isSetMatchAllQuery());
Assert.assertEquals(lindenScoreModel, lindenRequest.getQuery().getScoreModel());
bql = "select * from linden where title = 'qq' " + "using score model override test (Float link = 1)" + " begin" + " return score();" + " end";
lindenRequest = compiler.compile(bql).getSearchRequest();
Assert.assertEquals(new LindenTerm("title", "qq"), lindenRequest.getFilter().getTermFilter().getTerm());
lindenScoreModel = new LindenScoreModel().setName("test").setFunc("return score();").setOverride(true);
lindenScoreModel.addToParams(new LindenInputParam("link").setValue(new LindenValue().setDoubleValue(1)));
Assert.assertTrue(lindenRequest.getQuery().isSetMatchAllQuery());
Assert.assertEquals(lindenScoreModel, lindenRequest.getQuery().getScoreModel());
// list input params.
bql = "select * from linden where title = 'qq' " + "using score model test (Float link = [1, 2], Integer pos = [3, 4], Map<String,Double> kv = {\"a\":1, \"b\":2})" + " begin" + " return score();" + " end";
lindenRequest = compiler.compile(bql).getSearchRequest();
lindenScoreModel = new LindenScoreModel().setName("test").setFunc("return score();");
LindenInputParam inputParam = new LindenInputParam("link").setValue(new LindenValue());
inputParam.getValue().addToDoubleValues(1);
inputParam.getValue().addToDoubleValues(2);
lindenScoreModel.addToParams(inputParam);
LindenInputParam inputParam2 = new LindenInputParam("pos").setValue(new LindenValue());
inputParam2.getValue().addToLongValues(3);
inputParam2.getValue().addToLongValues(4);
lindenScoreModel.addToParams(inputParam2);
LindenInputParam inputParam3 = new LindenInputParam("kv").setValue(new LindenValue());
inputParam3.getValue().putToMapValue(new LindenValue().setStringValue("a"), new LindenValue().setDoubleValue(1));
inputParam3.getValue().putToMapValue(new LindenValue().setStringValue("b"), new LindenValue().setDoubleValue(2));
lindenScoreModel.addToParams(inputParam3);
Assert.assertEquals(lindenScoreModel, lindenRequest.getQuery().getScoreModel());
}
use of com.xiaomi.linden.thrift.common.LindenValue in project linden by XiaoMi.
the class TestScoreModelStrategy method init.
@Override
public void init() throws IOException {
customObjs = registerCustomCacheWrapper(new TestCustomCacheWrapper());
List<LindenInputParam> params = getParams();
LindenInputParam firstParam = params.get(0);
Assert.assertEquals(new LindenInputParam("param1"), firstParam);
LindenInputParam secondParam = params.get(1);
Map<LindenValue, LindenValue> dicts = secondParam.getValue().getMapValue();
Assert.assertEquals(2.0, dicts.get(new LindenValue().setStringValue("b")).getDoubleValue(), 0.001);
}
use of com.xiaomi.linden.thrift.common.LindenValue in project linden by XiaoMi.
the class BQLCompilerAnalyzer method exitFormal_parameter_decl.
@Override
public void exitFormal_parameter_decl(BQLParser.Formal_parameter_declContext ctx) {
if (ctx.python_style_value() != null) {
String name = ctx.variable_declarator_id().getText();
LindenInputParam inputParam = new LindenInputParam(name);
BQLParser.TypeContext obj = ctx.type();
LindenType type = (LindenType) valProperty.get(obj);
if (type == null && ctx.python_style_value().python_style_dict() == null) {
return;
}
if (ctx.python_style_value().value() != null) {
if (ctx.python_style_value().value().PLACEHOLDER() == null) {
String value = String.valueOf(valProperty.get(ctx.python_style_value().value()));
switch(type) {
case STRING:
case FACET:
inputParam.setValue(new LindenValue().setStringValue(value));
break;
case LONG:
case INTEGER:
inputParam.setValue(new LindenValue().setLongValue(Long.valueOf(value)));
break;
case FLOAT:
case DOUBLE:
inputParam.setValue(new LindenValue().setDoubleValue(Double.valueOf(value)));
break;
default:
}
}
} else if (ctx.python_style_value().python_style_list() != null) {
List<String> values = (List<String>) valProperty.get(ctx.python_style_value().python_style_list());
switch(type) {
case STRING:
inputParam.setValue(new LindenValue().setStringValues(values));
break;
case INTEGER:
case LONG:
if (values.size() > 0) {
inputParam.setValue(new LindenValue());
for (String value : values) {
inputParam.getValue().addToLongValues(Long.valueOf(value));
}
}
break;
case FLOAT:
case DOUBLE:
if (values.size() > 0) {
inputParam.setValue(new LindenValue());
for (String value : values) {
inputParam.getValue().addToDoubleValues(Double.valueOf(value));
}
}
break;
default:
}
} else {
if (ctx.python_style_value().python_style_dict() != null) {
LindenType leftType = (LindenType) valProperty.get(ctx.type().map_type().left);
LindenType rightType = (LindenType) valProperty.get(ctx.type().map_type().rigth);
Map<String, String> kvMap = (Map<String, String>) valProperty.get(ctx.python_style_value().python_style_dict());
Map<LindenValue, LindenValue> lindenValueMap = new HashMap<>();
for (Map.Entry<String, String> entry : kvMap.entrySet()) {
LindenValue left = getLindenValue(leftType, entry.getKey());
LindenValue right = getLindenValue(rightType, entry.getValue());
lindenValueMap.put(left, right);
}
inputParam.setValue(new LindenValue().setMapValue(lindenValueMap));
}
}
valProperty.put(ctx, inputParam);
}
}
use of com.xiaomi.linden.thrift.common.LindenValue in project linden by XiaoMi.
the class TestBQL method testFlexibleQuery.
@Test
public void testFlexibleQuery() {
String bql = "select * from linden by flexible_query is 'test' match 1 in (title, name^0.9)\n" + "using model test (Float a = 1, Long b = 2)\n" + "begin\n" + " return 1f;\n" + "end\n" + "where id = 231\n";
LindenSearchRequest lindenRequest = compiler.compile(bql).getSearchRequest();
Assert.assertTrue(lindenRequest.getQuery().isSetFlexQuery());
LindenFlexibleQuery lindenFlexibleQuery = new LindenFlexibleQuery().setQuery("test").setFields(Arrays.asList(new LindenSearchField("title"), new LindenSearchField("name").setBoost(0.9))).setModel(new LindenScoreModel().setName("test").setFunc("return 1f;").setParams(Arrays.asList(new LindenInputParam("a").setValue(new LindenValue().setDoubleValue(1)), new LindenInputParam("b").setValue(new LindenValue().setLongValue(2))))).setMatchRatio(1);
Assert.assertEquals(lindenFlexibleQuery, lindenRequest.getQuery().getFlexQuery());
// test full match
bql = "select * from linden by flexible_query is 'test' full_match in (title, name^0.9)\n" + "using model test (Float a = 1, Long b = 2)\n" + "begin\n" + " return 1f;\n" + "end\n" + "where id = 231\n";
lindenRequest = compiler.compile(bql).getSearchRequest();
Assert.assertTrue(lindenRequest.getQuery().isSetFlexQuery());
lindenFlexibleQuery = new LindenFlexibleQuery().setQuery("test").setFields(Arrays.asList(new LindenSearchField("title"), new LindenSearchField("name").setBoost(0.9))).setFullMatch(true).setModel(new LindenScoreModel().setName("test").setFunc("return 1f;").setParams(Arrays.asList(new LindenInputParam("a").setValue(new LindenValue().setDoubleValue(1)), new LindenInputParam("b").setValue(new LindenValue().setLongValue(2)))));
Assert.assertEquals(lindenFlexibleQuery, lindenRequest.getQuery().getFlexQuery());
}
use of com.xiaomi.linden.thrift.common.LindenValue in project linden by XiaoMi.
the class LindenScoreModelStrategyBuilder method buildInputParamCode.
private static String buildInputParamCode(List<LindenInputParam> params) throws Exception {
StringBuilder sb = new StringBuilder();
if (params != null && !params.isEmpty()) {
sb.append("if (getParams() == null || getParams().isEmpty()) {\n");
sb.append(" return 0f;\n }\n");
for (int i = 0; i < params.size(); ++i) {
LindenInputParam param = params.get(i);
if (!param.isSetValue()) {
continue;
}
if (param.getValue().isSetStringValue()) {
sb.append(String.format("String %s = getParams().get(%d).getValue().getStringValue();\n", param.getName(), i));
} else if (param.getValue().isSetLongValue()) {
sb.append(String.format("Long %s = getParams().get(%d).getValue().getLongValue();\n", param.getName(), i));
} else if (param.getValue().isSetDoubleValue()) {
sb.append(String.format("Double %s = getParams().get(%d).getValue().getDoubleValue();\n", param.getName(), i));
} else if (param.getValue().isSetLongValues()) {
sb.append(String.format("List<Long> %s = getParams().get(%d).getValue().getLongValues();\n", param.getName(), i));
} else if (param.getValue().isSetDoubleValues()) {
sb.append(String.format("List<Double> %s = getParams().get(%d).getValue().getDoubleValues();\n", param.getName(), i));
} else if (param.getValue().isSetStringValues()) {
sb.append(String.format("List<String> %s = getParams().get(%d).getValue().getStringValues();\n", param.getName(), i));
} else if (param.getValue().isSetMapValue()) {
LindenValueTypeMethodPair keyPair = null, valuePair = null;
for (Map.Entry<LindenValue, LindenValue> entry : param.getValue().getMapValue().entrySet()) {
keyPair = parseLindenValueTypeMethodPair(entry.getKey());
valuePair = parseLindenValueTypeMethodPair(entry.getValue());
break;
}
if (keyPair != null && valuePair != null) {
sb.append(String.format("Map<%s, %s> %s = new HashMap<>();\n", keyPair.type, valuePair.type, param.getName()));
sb.append(String.format("Map<LindenValue, LindenValue> %sMapsLocal = getParams().get(%d).getValue().getMapValue();\n", param.getName(), i));
sb.append(String.format("for(Map.Entry<LindenValue, LindenValue> entry : %sMapsLocal.entrySet()) {\n", param.getName()));
sb.append(String.format("%s.put(entry.getKey().%s, entry.getValue().%s);\n", param.getName(), keyPair.method, valuePair.method));
sb.append(String.format("}\n"));
} else {
sb.append(String.format("Map<Object, Object> %s = new HashMap<>();\n", param.getName()));
}
}
}
}
return sb.toString();
}
Aggregations