Search in sources :

Example 6 with LindenInputParam

use of com.xiaomi.linden.thrift.common.LindenInputParam 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());
}
Also used : LindenInputParam(com.xiaomi.linden.thrift.common.LindenInputParam) LindenValue(com.xiaomi.linden.thrift.common.LindenValue) LindenSearchField(com.xiaomi.linden.thrift.common.LindenSearchField) LindenFlexibleQuery(com.xiaomi.linden.thrift.common.LindenFlexibleQuery) LindenSearchRequest(com.xiaomi.linden.thrift.common.LindenSearchRequest) LindenScoreModel(com.xiaomi.linden.thrift.common.LindenScoreModel) Test(org.junit.Test)

Example 7 with LindenInputParam

use of com.xiaomi.linden.thrift.common.LindenInputParam 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();
}
Also used : LindenInputParam(com.xiaomi.linden.thrift.common.LindenInputParam) LindenValue(com.xiaomi.linden.thrift.common.LindenValue) Map(java.util.Map)

Aggregations

LindenInputParam (com.xiaomi.linden.thrift.common.LindenInputParam)7 LindenValue (com.xiaomi.linden.thrift.common.LindenValue)5 LindenScoreModel (com.xiaomi.linden.thrift.common.LindenScoreModel)4 LindenFlexibleQuery (com.xiaomi.linden.thrift.common.LindenFlexibleQuery)2 LindenSearchField (com.xiaomi.linden.thrift.common.LindenSearchField)2 LindenSearchRequest (com.xiaomi.linden.thrift.common.LindenSearchRequest)2 Map (java.util.Map)2 Test (org.junit.Test)2 LindenQuery (com.xiaomi.linden.thrift.common.LindenQuery)1 LindenTerm (com.xiaomi.linden.thrift.common.LindenTerm)1 LindenType (com.xiaomi.linden.thrift.common.LindenType)1 AbstractMap (java.util.AbstractMap)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1