Search in sources :

Example 1 with LindenScoreModel

use of com.xiaomi.linden.thrift.common.LindenScoreModel 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 LindenScoreModel

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

the class TestLindenCore method lindenScoreModelTest.

@Test
public void lindenScoreModelTest() throws IOException {
    String func = " return rank() * 10;";
    LindenScoreModel model = new LindenScoreModel().setName("test").setFunc(func);
    LindenSearchRequest request = new LindenSearchRequest().setQuery(LindenQueryBuilder.buildTermQuery("title", "lucene").setScoreModel(model));
    LindenResult result = lindenCore.search(request);
    Assert.assertEquals(4, result.getTotalHits());
    Assert.assertEquals(103f, result.getHits().get(0).getScore(), 0.1f);
    // bad score model function
    func = " return rank() * 10 + a;";
    model = new LindenScoreModel().setName("test").setFunc(func);
    request = new LindenSearchRequest().setQuery(LindenQueryBuilder.buildTermQuery("title", "lucene").setScoreModel(model));
    try {
        lindenCore.search(request);
    } catch (Exception e) {
    // do nothing
    }
    try {
        lindenCore.search(request);
    } catch (Exception e) {
        Assert.assertTrue(e.getMessage().contains("score model test compile failed, please check score model code"));
    }
}
Also used : LindenResult(com.xiaomi.linden.thrift.common.LindenResult) LindenSearchRequest(com.xiaomi.linden.thrift.common.LindenSearchRequest) IOException(java.io.IOException) LindenScoreModel(com.xiaomi.linden.thrift.common.LindenScoreModel) Test(org.junit.Test)

Example 3 with LindenScoreModel

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

the class BQLCompilerAnalyzer method exitSelect_stmt.

@Override
public void exitSelect_stmt(BQLParser.Select_stmtContext ctx) {
    if (ctx.order_by_clause().size() > 1) {
        throw new ParseCancellationException(new SemanticException(ctx.order_by_clause(1), "ORDER BY clause can only appear once."));
    }
    if (ctx.limit_clause().size() > 1) {
        throw new ParseCancellationException(new SemanticException(ctx.limit_clause(1), "LIMIT clause can only appear once."));
    }
    if (ctx.group_by_clause().size() > 1) {
        throw new ParseCancellationException(new SemanticException(ctx.group_by_clause(1), "GROUP BY clause can only appear once."));
    }
    if (ctx.browse_by_clause().size() > 1) {
        throw new ParseCancellationException(new SemanticException(ctx.browse_by_clause(1), "BROWSE BY clause can only appear once."));
    }
    if (ctx.drill_clause().size() > 1) {
        throw new ParseCancellationException(new SemanticException(ctx.drill_clause(1), "DRILL clause can only appear once."));
    }
    if (ctx.source_clause().size() > 1) {
        throw new ParseCancellationException(new SemanticException(ctx.source_clause(1), "SOURCE clause can only appear once."));
    }
    if (ctx.route_by_clause().size() > 1) {
        throw new ParseCancellationException(new SemanticException(ctx.route_by_clause(1), "ROUTE BY clause can only appear once."));
    }
    if (ctx.score_model_clause().size() > 1) {
        throw new ParseCancellationException(new SemanticException(ctx.score_model_clause(1), "USING SCORE MODEL clause can only appear once."));
    }
    LindenSearchRequest lindenRequest = new LindenSearchRequest();
    if (ctx.cols != null) {
        lindenRequest.setSourceFields((List<String>) valProperty.get(ctx.cols));
    }
    if (ctx.tables != null) {
        lindenRequest.setIndexNames((List<String>) valProperty.get(ctx.tables));
    }
    if (ctx.group_by != null) {
        GroupParam groupParam = (GroupParam) valProperty.get(ctx.group_by);
        if (groupParam != null) {
            lindenRequest.setGroupParam(groupParam);
        }
    }
    if (ctx.limit != null) {
        lindenRequest.setOffset(offsetProperty.get(ctx.limit));
        lindenRequest.setLength(countProperty.get(ctx.limit));
    }
    if (ctx.source != null && (Boolean) valProperty.get(ctx.source)) {
        lindenRequest.setSource(true);
    }
    if (ctx.explain != null && (Boolean) valProperty.get(ctx.explain)) {
        lindenRequest.setExplain(true);
    }
    LindenQuery query = null;
    if (ctx.q != null) {
        query = queryProperty.get(ctx.q);
    }
    if (query == null) {
        query = LindenQueryBuilder.buildMatchAllQuery();
    }
    lindenRequest.setQuery(query);
    if (ctx.w != null) {
        LindenFilter filter = filterProperty.get(ctx.w);
        lindenRequest.setFilter(filter);
    }
    if (ctx.scoring_model != null) {
        LindenScoreModel scoreModel = (LindenScoreModel) valProperty.get(ctx.scoring_model);
        lindenRequest.getQuery().setScoreModel(scoreModel);
    }
    if (ctx.boost_by != null && ctx.boost_by.numeric_value().PLACEHOLDER() == null) {
        lindenRequest.getQuery().setBoost(Double.valueOf(ctx.boost_by.numeric_value().getText()));
    }
    if (spatialParam != null) {
        lindenRequest.setSpatialParam(spatialParam);
    }
    if (ctx.order_by != null) {
        lindenRequest.setSort((LindenSort) valProperty.get(ctx.order_by));
    }
    if (ctx.snippet != null) {
        lindenRequest.setSnippetParam((SnippetParam) valProperty.get(ctx.snippet));
    }
    if (ctx.in_top != null) {
        lindenRequest.setEarlyParam((EarlyParam) valProperty.get(ctx.in_top));
    }
    if (ctx.route_param != null) {
        lindenRequest.setRouteParam((SearchRouteParam) valProperty.get(ctx.route_param));
    }
    if (facetRequest.isSetFacetParams() || facetRequest.isSetDrillDownDimAndPaths() || facetRequest.isSetAggregations()) {
        lindenRequest.setFacet(facetRequest);
    }
    lindenSearchRequestProperty.put(ctx, lindenRequest);
}
Also used : LindenFilter(com.xiaomi.linden.thrift.common.LindenFilter) LindenQuery(com.xiaomi.linden.thrift.common.LindenQuery) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) GroupParam(com.xiaomi.linden.thrift.common.GroupParam) LindenSearchRequest(com.xiaomi.linden.thrift.common.LindenSearchRequest) LindenScoreModel(com.xiaomi.linden.thrift.common.LindenScoreModel)

Example 4 with LindenScoreModel

use of com.xiaomi.linden.thrift.common.LindenScoreModel 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 5 with LindenScoreModel

use of com.xiaomi.linden.thrift.common.LindenScoreModel 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

LindenScoreModel (com.xiaomi.linden.thrift.common.LindenScoreModel)6 LindenInputParam (com.xiaomi.linden.thrift.common.LindenInputParam)4 LindenSearchRequest (com.xiaomi.linden.thrift.common.LindenSearchRequest)4 Test (org.junit.Test)3 LindenFlexibleQuery (com.xiaomi.linden.thrift.common.LindenFlexibleQuery)2 LindenQuery (com.xiaomi.linden.thrift.common.LindenQuery)2 LindenSearchField (com.xiaomi.linden.thrift.common.LindenSearchField)2 LindenValue (com.xiaomi.linden.thrift.common.LindenValue)2 GroupParam (com.xiaomi.linden.thrift.common.GroupParam)1 LindenFilter (com.xiaomi.linden.thrift.common.LindenFilter)1 LindenResult (com.xiaomi.linden.thrift.common.LindenResult)1 LindenTerm (com.xiaomi.linden.thrift.common.LindenTerm)1 IOException (java.io.IOException)1 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)1