Search in sources :

Example 26 with LindenQuery

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

the class TestLindenCore method flexibleQueryEmptyFieldTest.

@Test
public void flexibleQueryEmptyFieldTest() throws IOException {
    String function = "   if (tagnum() != 0) {\n" + "      return 10f;\n" + "    } else if (tagstr().length > 0) {\n" + "      return 100f;\n" + "    } else {\n" + "      return 1f;\n" + "    }";
    LindenQuery flexQuery = new LindenFlexibleQueryBuilder().setQuery("lucene").addField("title").addModel("test", function).build();
    LindenResult result = lindenCore.search(new LindenSearchRequest().setQuery(flexQuery));
    Assert.assertEquals(4, result.getTotalHits());
    Assert.assertEquals("3", result.getHits().get(0).getId());
    Assert.assertEquals(100f, result.getHits().get(0).getScore(), 0.01);
    Assert.assertEquals("2", result.getHits().get(1).getId());
    Assert.assertEquals(10f, result.getHits().get(1).getScore(), 0.01);
}
Also used : LindenResult(com.xiaomi.linden.thrift.common.LindenResult) LindenFlexibleQueryBuilder(com.xiaomi.linden.thrift.builder.query.LindenFlexibleQueryBuilder) LindenQuery(com.xiaomi.linden.thrift.common.LindenQuery) LindenSearchRequest(com.xiaomi.linden.thrift.common.LindenSearchRequest) Test(org.junit.Test)

Example 27 with LindenQuery

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

the class DisMaxQueryConstructor method construct.

@Override
protected Query construct(LindenQuery lindenQuery, LindenConfig config) throws Exception {
    if (!lindenQuery.isSetDisMaxQuery()) {
        return null;
    }
    LindenDisMaxQuery disMaxQuery = lindenQuery.getDisMaxQuery();
    DisjunctionMaxQuery disjunctionMaxQuery = new DisjunctionMaxQuery((float) disMaxQuery.getTie());
    for (LindenQuery subLindenQuery : disMaxQuery.getQueries()) {
        Query query = QueryConstructor.constructQuery(subLindenQuery, config);
        if (query != null) {
            disjunctionMaxQuery.add(query);
        }
    }
    return disjunctionMaxQuery;
}
Also used : LindenQuery(com.xiaomi.linden.thrift.common.LindenQuery) Query(org.apache.lucene.search.Query) LindenQuery(com.xiaomi.linden.thrift.common.LindenQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) LindenDisMaxQuery(com.xiaomi.linden.thrift.common.LindenDisMaxQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) LindenDisMaxQuery(com.xiaomi.linden.thrift.common.LindenDisMaxQuery)

Example 28 with LindenQuery

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

the class QueryStringQueryConstructor method construct.

@Override
protected Query construct(LindenQuery lindenQuery, LindenConfig config) throws IOException {
    LindenQueryStringQuery stringQuery = lindenQuery.getQueryString();
    QueryParser.Operator op = QueryParser.Operator.OR;
    if (stringQuery.isSetOperator() && stringQuery.getOperator() == Operator.AND) {
        op = QueryParser.Operator.AND;
    }
    QueryParser queryParser = new LindenQueryParser(config);
    String content = stringQuery.getQuery();
    try {
        queryParser.setDefaultOperator(op);
        Query query = queryParser.parse(content);
        // disable coord
        if (query instanceof BooleanQuery) {
            BooleanQuery bQuery = (BooleanQuery) query;
            BooleanQuery booleanQuery = new BooleanQuery(stringQuery.isDisableCoord());
            BooleanClause[] clauses = bQuery.getClauses();
            for (BooleanClause clause : clauses) {
                booleanQuery.add(clause);
            }
            booleanQuery.setBoost(query.getBoost());
            query = booleanQuery;
        }
        return query;
    } catch (ParseException e) {
        throw new IOException(Throwables.getStackTraceAsString(e));
    }
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) BooleanQuery(org.apache.lucene.search.BooleanQuery) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) Query(org.apache.lucene.search.Query) LindenQuery(com.xiaomi.linden.thrift.common.LindenQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) LindenQueryStringQuery(com.xiaomi.linden.thrift.common.LindenQueryStringQuery) LindenQueryStringQuery(com.xiaomi.linden.thrift.common.LindenQueryStringQuery) ParseException(org.apache.lucene.queryparser.classic.ParseException) IOException(java.io.IOException)

Example 29 with LindenQuery

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

the class RangeQueryConstructor method construct.

@Override
protected Query construct(LindenQuery lindenQuery, LindenConfig config) throws IOException {
    if (lindenQuery.isSetRangeQuery()) {
        LindenRangeQuery lindenRangeQuery = lindenQuery.getRangeQuery();
        LindenRange range = lindenRangeQuery.getRange();
        String fieldName = range.getField();
        LindenType type = range.getType();
        String start = range.getStartValue();
        String end = range.getEndValue();
        boolean startClose = range.isStartClosed();
        boolean endClose = range.isEndClosed();
        Query query = null;
        switch(type) {
            case STRING:
            case FACET:
                query = new TermRangeQuery(fieldName, bytesRefVal(start), bytesRefVal(end), startClose, endClose);
                break;
            case INTEGER:
                query = NumericRangeQuery.newIntRange(fieldName, intVal(start), intVal(end), startClose, endClose);
                break;
            case LONG:
                query = NumericRangeQuery.newLongRange(fieldName, longVal(start), longVal(end), startClose, endClose);
                break;
            case DOUBLE:
                query = NumericRangeQuery.newDoubleRange(fieldName, doubleVal(start), doubleVal(end), startClose, endClose);
                break;
            case FLOAT:
                query = NumericRangeQuery.newFloatRange(fieldName, floatVal(start), floatVal(end), startClose, endClose);
                break;
        }
        return query;
    }
    // todo throw exception.
    return null;
}
Also used : Query(org.apache.lucene.search.Query) NumericRangeQuery(org.apache.lucene.search.NumericRangeQuery) LindenQuery(com.xiaomi.linden.thrift.common.LindenQuery) LindenRangeQuery(com.xiaomi.linden.thrift.common.LindenRangeQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) LindenRangeQuery(com.xiaomi.linden.thrift.common.LindenRangeQuery) LindenType(com.xiaomi.linden.thrift.common.LindenType) LindenRange(com.xiaomi.linden.thrift.common.LindenRange)

Aggregations

LindenQuery (com.xiaomi.linden.thrift.common.LindenQuery)29 LindenFilter (com.xiaomi.linden.thrift.common.LindenFilter)17 LindenSearchRequest (com.xiaomi.linden.thrift.common.LindenSearchRequest)11 Test (org.junit.Test)10 LindenBooleanQueryBuilder (com.xiaomi.linden.thrift.builder.query.LindenBooleanQueryBuilder)9 LindenType (com.xiaomi.linden.thrift.common.LindenType)7 AbstractMap (java.util.AbstractMap)7 Map (java.util.Map)7 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)7 LindenBooleanFilterBuilder (com.xiaomi.linden.thrift.builder.filter.LindenBooleanFilterBuilder)6 HashMap (java.util.HashMap)6 Query (org.apache.lucene.search.Query)5 LindenBooleanFilter (com.xiaomi.linden.thrift.common.LindenBooleanFilter)4 LindenBooleanSubFilter (com.xiaomi.linden.thrift.common.LindenBooleanSubFilter)4 LindenFlexibleQueryBuilder (com.xiaomi.linden.thrift.builder.query.LindenFlexibleQueryBuilder)3 LindenResult (com.xiaomi.linden.thrift.common.LindenResult)3 LindenWildcardQuery (com.xiaomi.linden.thrift.common.LindenWildcardQuery)3 ArrayList (java.util.ArrayList)3 LindenQueryStringQueryBuilder (com.xiaomi.linden.thrift.builder.query.LindenQueryStringQueryBuilder)2 GroupParam (com.xiaomi.linden.thrift.common.GroupParam)2