Search in sources :

Example 46 with LindenSearchRequest

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

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

the class TestLindenWordDelimiterAnalyzer method testIndexMode.

@Test
public void testIndexMode() throws IOException {
    String bql = "select * from linden by query is \"title:刘华清\"";
    LindenSearchRequest request = bqlCompiler.compile(bql).getSearchRequest();
    LindenResult result = lindenCore.search(request);
    Assert.assertEquals(3, result.getTotalHits());
    // phrase test
    bql = "select * from linden by query is 'title:\"刘华清\"'";
    request = bqlCompiler.compile(bql).getSearchRequest();
    result = lindenCore.search(request);
    Assert.assertEquals(2, result.getTotalHits());
    bql = "select * from linden by query is 'title:\"海军上将刘华清\"'";
    request = bqlCompiler.compile(bql).getSearchRequest();
    result = lindenCore.search(request);
    Assert.assertEquals(2, result.getTotalHits());
    // snippet test
    bql = "select * from linden by query is 'title:(海军上将刘华清中国龙)' snippet title";
    request = bqlCompiler.compile(bql).getSearchRequest();
    result = lindenCore.search(request);
    Assert.assertEquals(3, result.getTotalHits());
    Assert.assertEquals("<b>海</b><b>军</b><b>上</b><b>将</b><b>刘</b><b>华</b><b>清</b>!!! <b>中</b><b>国</b> 人民 李玉洁 尚铁<b>龙</b> 胡晓光", result.getHits().get(0).getSnippets().get("title").getSnippet());
    Assert.assertEquals("<b>海</b><b>军</b><b>上</b><b>将</b><b>刘</b><b>华</b><b>清</b>", result.getHits().get(1).getSnippets().get("title").getSnippet());
}
Also used : LindenResult(com.xiaomi.linden.thrift.common.LindenResult) LindenSearchRequest(com.xiaomi.linden.thrift.common.LindenSearchRequest) Test(org.junit.Test)

Example 48 with LindenSearchRequest

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

the class TestGlobalIDF method testGlobalIdf.

@Test
public void testGlobalIdf() throws IOException {
    String bql = "select * from linden by flexible_query is 'lucene' global_idf in (title, field1)\n" + "using model test\n" + "begin\n" + "    return 1;\n" + "end\n" + "source explain;";
    LindenSearchRequest request = bqlCompiler.compile(bql).getSearchRequest();
    LindenResult result = lindenCore.search(request);
    Assert.assertEquals(result.getHits().get(0).getExplanation().getDetails().get(0).getDetails().get(0).getDescription(), result.getHits().get(1).getExplanation().getDetails().get(0).getDetails().get(0).getDescription());
}
Also used : LindenResult(com.xiaomi.linden.thrift.common.LindenResult) LindenSearchRequest(com.xiaomi.linden.thrift.common.LindenSearchRequest) Test(org.junit.Test)

Example 49 with LindenSearchRequest

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

the class TestLindenMetricPlugin method basicTest.

@Test
public void basicTest() throws Exception {
    LindenSearchRequest request = new LindenSearchRequest().setQuery(LindenQueryBuilder.buildTermQuery("title", "lucene"));
    LindenResult result = lindenCore.search(request);
    Assert.assertEquals(4, result.getTotalHits());
}
Also used : LindenResult(com.xiaomi.linden.thrift.common.LindenResult) LindenSearchRequest(com.xiaomi.linden.thrift.common.LindenSearchRequest) Test(org.junit.Test)

Example 50 with LindenSearchRequest

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

the class CoreLindenCluster method coreSearch.

public LindenResult coreSearch(final LindenSearchRequest request) throws IOException {
    List<Future<BoxedUnit>> futures = new ArrayList<>();
    List<String> hosts = new ArrayList<>();
    final List<LindenResult> resultList = new ArrayList<>();
    if (request.isSetRouteParam() && request.getRouteParam().isSetShardParams()) {
        for (final ShardRouteParam routeParam : request.getRouteParam().getShardParams()) {
            ShardClient client = clients.get(routeParam.getShardId());
            if (client != null && client.isAvailable()) {
                LindenSearchRequest subRequest = request;
                if (routeParam.isSetEarlyParam()) {
                    subRequest = new LindenSearchRequest(request);
                    subRequest.setEarlyParam(routeParam.getEarlyParam());
                }
                final Map.Entry<String, Future<LindenResult>> hostFuturePair = client.search(subRequest);
                hosts.add(hostFuturePair.getKey());
                futures.add(hostFuturePair.getValue().transformedBy(new FutureTransformer<LindenResult, BoxedUnit>() {

                    @Override
                    public BoxedUnit map(LindenResult lindenResult) {
                        synchronized (resultList) {
                            resultList.add(lindenResult);
                            if (!lindenResult.isSuccess()) {
                                LOGGER.error("Shard [{}] host [{}] failed to get search result : {}", routeParam.getShardId(), hostFuturePair.getKey(), lindenResult.getError());
                            }
                        }
                        return BoxedUnit.UNIT;
                    }

                    @Override
                    public BoxedUnit handle(Throwable t) {
                        LOGGER.error("Shard [{}] host [{}] failed to get search result : {}", routeParam.getShardId(), hostFuturePair.getKey(), Throwables.getStackTraceAsString(t));
                        return BoxedUnit.UNIT;
                    }
                }));
            } else {
                LOGGER.warn("Route to Shard [{}] failed.", routeParam.getShardId());
            }
        }
    } else {
        LindenSearchRequest subRequest = request;
        for (final Map.Entry<Integer, ShardClient> entry : clients.entrySet()) {
            if (entry.getValue().isAvailable()) {
                final Map.Entry<String, Future<LindenResult>> hostFuturePair = entry.getValue().search(subRequest);
                hosts.add(hostFuturePair.getKey());
                futures.add(hostFuturePair.getValue().transformedBy(new FutureTransformer<LindenResult, BoxedUnit>() {

                    @Override
                    public BoxedUnit map(LindenResult lindenResult) {
                        synchronized (resultList) {
                            resultList.add(lindenResult);
                            if (!lindenResult.isSuccess()) {
                                LOGGER.error("Shard [{}] host [{}] failed to get search result : {}", entry.getKey(), hostFuturePair.getKey(), lindenResult.getError());
                            }
                            return BoxedUnit.UNIT;
                        }
                    }

                    @Override
                    public BoxedUnit handle(Throwable t) {
                        LOGGER.error("Shard [{}] host [{}] failed to get search result : {}", entry.getKey(), hostFuturePair.getKey(), Throwables.getStackTraceAsString(t));
                        return BoxedUnit.UNIT;
                    }
                }));
            }
        }
    }
    Future<List<BoxedUnit>> collected = Future.collect(futures);
    try {
        if (clusterFutureAwaitTimeout == 0) {
            Await.result(collected);
        } else {
            Await.result(collected, Duration.apply(clusterFutureAwaitTimeout, TimeUnit.MILLISECONDS));
        }
    } catch (Exception e) {
        LOGGER.error("Failed to get all results, exception: {}", Throwables.getStackTraceAsString(e));
        LOGGER.error(getHostFutureInfo(hosts, futures));
        if (resultList.size() == 0) {
            return new LindenResult().setSuccess(false).setError("Failed to get any shard result, " + Throwables.getStackTraceAsString(e));
        }
    }
    return ResultMerger.merge(request, resultList);
}
Also used : FutureTransformer(com.twitter.util.FutureTransformer) ArrayList(java.util.ArrayList) LindenSearchRequest(com.xiaomi.linden.thrift.common.LindenSearchRequest) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) LindenResult(com.xiaomi.linden.thrift.common.LindenResult) ShardRouteParam(com.xiaomi.linden.thrift.common.ShardRouteParam) Future(com.twitter.util.Future) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

LindenSearchRequest (com.xiaomi.linden.thrift.common.LindenSearchRequest)95 Test (org.junit.Test)90 LindenResult (com.xiaomi.linden.thrift.common.LindenResult)69 LindenQuery (com.xiaomi.linden.thrift.common.LindenQuery)13 LindenFilter (com.xiaomi.linden.thrift.common.LindenFilter)9 LindenBooleanQueryBuilder (com.xiaomi.linden.thrift.builder.query.LindenBooleanQueryBuilder)5 LindenBooleanFilter (com.xiaomi.linden.thrift.common.LindenBooleanFilter)5 IOException (java.io.IOException)5 JSONObject (com.alibaba.fastjson.JSONObject)4 LindenBooleanSubFilter (com.xiaomi.linden.thrift.common.LindenBooleanSubFilter)4 LindenDeleteRequest (com.xiaomi.linden.thrift.common.LindenDeleteRequest)4 LindenScoreModel (com.xiaomi.linden.thrift.common.LindenScoreModel)4 Stopwatch (com.google.common.base.Stopwatch)3 MultiLindenCoreImpl (com.xiaomi.linden.core.search.MultiLindenCoreImpl)3 LindenFlexibleQueryBuilder (com.xiaomi.linden.thrift.builder.query.LindenFlexibleQueryBuilder)3 LindenFacet (com.xiaomi.linden.thrift.common.LindenFacet)3 LindenFacetDimAndPath (com.xiaomi.linden.thrift.common.LindenFacetDimAndPath)3 LindenFacetParam (com.xiaomi.linden.thrift.common.LindenFacetParam)3 LindenRequest (com.xiaomi.linden.thrift.common.LindenRequest)3 LindenTerm (com.xiaomi.linden.thrift.common.LindenTerm)3