Search in sources :

Example 1 with LindenInputParam

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

Example 2 with LindenInputParam

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

Example 3 with LindenInputParam

use of com.xiaomi.linden.thrift.common.LindenInputParam in project linden by XiaoMi.

the class BQLCompilerAnalyzer method exitFlexible_query_predicate.

@Override
public void exitFlexible_query_predicate(BQLParser.Flexible_query_predicateContext ctx) {
    String orig = unescapeStringLiteral(ctx.STRING_LITERAL());
    LindenScoreModel lindenScoreModel = new LindenScoreModel().setName(ctx.IDENT().getText()).setFunc((String) valProperty.get(ctx.score_model())).setParams((List<LindenInputParam>) valProperty.get(ctx.formal_parameters()));
    if (ctx.OVERRIDE() != null) {
        lindenScoreModel.setOverride(true);
    }
    if (ctx.PLUGIN() != null) {
        lindenScoreModel.setPlugin(true);
    }
    LindenFlexibleQuery lindenFlexibleQuery = new LindenFlexibleQuery().setQuery(orig).setFields((List<LindenSearchField>) valProperty.get(ctx.flexible_fields()));
    lindenFlexibleQuery.setModel(lindenScoreModel);
    if (ctx.fm != null) {
        lindenFlexibleQuery.setFullMatch(true);
    } else if (ctx.mrt != null && ctx.mrt.PLACEHOLDER() == null) {
        double ratio = Double.valueOf(ctx.mrt.getText());
        lindenFlexibleQuery.setMatchRatio(ratio);
    }
    LindenQuery lindenQuery = new LindenQuery();
    if (inQueryWhere) {
        if (ctx.gi != null) {
            lindenFlexibleQuery.setGlobalIDF(true);
            if (ctx.gfd != null) {
                lindenFlexibleQuery.setGlobalFields((List<LindenSearchField>) valProperty.get(ctx.global_fields()));
            }
        }
        queryProperty.put(ctx, lindenQuery.setFlexQuery(lindenFlexibleQuery));
    } else {
        filterProperty.put(ctx, LindenQueryFilterBuilder.buildQueryFilter(lindenQuery.setFlexQuery(lindenFlexibleQuery)));
    }
}
Also used : LindenInputParam(com.xiaomi.linden.thrift.common.LindenInputParam) LindenQuery(com.xiaomi.linden.thrift.common.LindenQuery) LindenSearchField(com.xiaomi.linden.thrift.common.LindenSearchField) LindenFlexibleQuery(com.xiaomi.linden.thrift.common.LindenFlexibleQuery) LindenScoreModel(com.xiaomi.linden.thrift.common.LindenScoreModel)

Example 4 with LindenInputParam

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

Example 5 with LindenInputParam

use of com.xiaomi.linden.thrift.common.LindenInputParam in project linden by XiaoMi.

the class BQLCompilerAnalyzer method exitScore_model_clause.

@Override
public void exitScore_model_clause(BQLParser.Score_model_clauseContext ctx) {
    LindenScoreModel lindenScoreModel = new LindenScoreModel().setName(ctx.IDENT().getText()).setFunc((String) valProperty.get(ctx.score_model())).setParams((List<LindenInputParam>) valProperty.get(ctx.formal_parameters()));
    if (spatialParam != null) {
        lindenScoreModel.setCoordinate(spatialParam.getCoordinate());
    }
    if (ctx.OVERRIDE() != null) {
        lindenScoreModel.setOverride(true);
    }
    if (ctx.PLUGIN() != null) {
        lindenScoreModel.setPlugin(true);
    }
    valProperty.put(ctx, lindenScoreModel);
}
Also used : LindenInputParam(com.xiaomi.linden.thrift.common.LindenInputParam) LindenScoreModel(com.xiaomi.linden.thrift.common.LindenScoreModel)

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