Search in sources :

Example 1 with LindenFlexibleQuery

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

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

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

the class FlexibleQueryConstructor method construct.

@Override
protected Query construct(LindenQuery lindenQuery, LindenConfig config) throws IOException {
    if (lindenQuery.isSetFlexQuery()) {
        LindenFlexibleQuery lindenFlexibleQuery = lindenQuery.getFlexQuery();
        List<FlexibleQuery.FlexibleField> fields = Lists.newArrayList();
        for (LindenSearchField field : lindenFlexibleQuery.getFields()) {
            fields.add(new FlexibleQuery.FlexibleField(field.getName(), field.getBoost()));
        }
        List<FlexibleQuery.FlexibleField> globalFields = initGlobalFields(lindenFlexibleQuery.isGlobalIDF(), lindenFlexibleQuery.getGlobalFields(), lindenFlexibleQuery.getFields());
        FlexibleQuery flexibleQuery = new FlexibleQuery(fields, globalFields, lindenFlexibleQuery.getQuery(), lindenFlexibleQuery.getModel(), config);
        flexibleQuery.setFullMatch(lindenFlexibleQuery.isFullMatch());
        flexibleQuery.setGlobalIDF(lindenFlexibleQuery.isGlobalIDF());
        flexibleQuery.setMatchRatio(lindenFlexibleQuery.getMatchRatio());
        return flexibleQuery;
    }
    return null;
}
Also used : LindenSearchField(com.xiaomi.linden.thrift.common.LindenSearchField) LindenFlexibleQuery(com.xiaomi.linden.thrift.common.LindenFlexibleQuery) FlexibleQuery(com.xiaomi.linden.lucene.query.flexiblequery.FlexibleQuery) LindenFlexibleQuery(com.xiaomi.linden.thrift.common.LindenFlexibleQuery)

Aggregations

LindenFlexibleQuery (com.xiaomi.linden.thrift.common.LindenFlexibleQuery)3 LindenSearchField (com.xiaomi.linden.thrift.common.LindenSearchField)3 LindenInputParam (com.xiaomi.linden.thrift.common.LindenInputParam)2 LindenScoreModel (com.xiaomi.linden.thrift.common.LindenScoreModel)2 FlexibleQuery (com.xiaomi.linden.lucene.query.flexiblequery.FlexibleQuery)1 LindenQuery (com.xiaomi.linden.thrift.common.LindenQuery)1 LindenSearchRequest (com.xiaomi.linden.thrift.common.LindenSearchRequest)1 LindenValue (com.xiaomi.linden.thrift.common.LindenValue)1 Test (org.junit.Test)1