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);
}
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());
}
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());
}
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());
}
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);
}
Aggregations