Search in sources :

Example 76 with LindenResult

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

the class TestMultiLindenCore method testTimeDivision.

@Test
public void testTimeDivision() throws Exception {
    String path = FilenameUtils.concat(INDEX_DIR, "time/");
    lindenConfig.setIndexDirectory(path).setMultiIndexDivisionType(LindenConfig.MultiIndexDivisionType.TIME_HOUR).setLindenCoreMode(LindenConfig.LindenCoreMode.MULTI).setMultiIndexMaxLiveIndexNum(10);
    Deencapsulation.setField(DocNumLimitMultiIndexStrategy.class, "INDEX_CHECK_INTERVAL_MILLISECONDS", -10);
    lindenCore = new MultiLindenCoreImpl(lindenConfig);
    for (int i = 0; i < 400; ++i) {
        JSONObject json = new JSONObject();
        json.put("type", "index");
        JSONObject content = new JSONObject();
        content.put("id", i);
        content.put("title", "test " + i);
        json.put("content", content);
        handleRequest(json.toJSONString());
    }
    lindenCore.commit();
    lindenCore.refresh();
    Assert.assertEquals(8, lindenCore.getServiceInfo().getIndexNamesSize());
    Assert.assertEquals(400, lindenCore.getServiceInfo().getDocsNum());
    String bql = "select * from linden source";
    LindenSearchRequest request = bqlCompiler.compile(bql).getSearchRequest();
    LindenResult result = lindenCore.search(request);
    Assert.assertEquals(400, result.getTotalHits());
    for (int i = 0; i < 400; ++i) {
        JSONObject json = new JSONObject();
        json.put("type", "index");
        JSONObject content = new JSONObject();
        content.put("id", i);
        content.put("title", "test " + (400 + i));
        json.put("content", content);
        handleRequest(json.toJSONString());
    }
    lindenCore.commit();
    lindenCore.refresh();
    result = lindenCore.search(request);
    Assert.assertEquals(500, result.getTotalHits());
    Assert.assertEquals(10, lindenCore.getServiceInfo().getIndexNamesSize());
    Assert.assertEquals(500, lindenCore.getServiceInfo().getDocsNum());
    bql = "delete from linden where query is 'title:400 title:401'";
    LindenDeleteRequest deleteRequest = bqlCompiler.compile(bql).getDeleteRequest();
    lindenCore.delete(deleteRequest);
    lindenCore.commit();
    lindenCore.refresh();
    Assert.assertEquals(498, lindenCore.getServiceInfo().getDocsNum());
    lindenCore.close();
}
Also used : LindenDeleteRequest(com.xiaomi.linden.thrift.common.LindenDeleteRequest) LindenResult(com.xiaomi.linden.thrift.common.LindenResult) JSONObject(com.alibaba.fastjson.JSONObject) LindenSearchRequest(com.xiaomi.linden.thrift.common.LindenSearchRequest) MultiLindenCoreImpl(com.xiaomi.linden.core.search.MultiLindenCoreImpl) Test(org.junit.Test)

Example 77 with LindenResult

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

the class TestMultiLindenCore method testIndexNameDivision.

@Test
public void testIndexNameDivision() throws Exception {
    String path = FilenameUtils.concat(INDEX_DIR, "name/");
    lindenConfig.setIndexDirectory(path).setLindenCoreMode(LindenConfig.LindenCoreMode.MULTI).setMultiIndexDivisionType(LindenConfig.MultiIndexDivisionType.INDEX_NAME);
    lindenCore = new MultiLindenCoreImpl(lindenConfig);
    for (int i = 0; i < 400; ++i) {
        JSONObject json = new JSONObject();
        json.put("type", "index");
        json.put("index", "multi_" + i / 50);
        JSONObject content = new JSONObject();
        content.put("id", i);
        content.put("title", "test " + i);
        json.put("content", content);
        handleRequest(json.toJSONString());
    }
    lindenCore.commit();
    lindenCore.refresh();
    Assert.assertEquals(8, lindenCore.getServiceInfo().getIndexNamesSize());
    Assert.assertEquals(400, lindenCore.getServiceInfo().getDocsNum());
    String bql = "select * from linden source";
    LindenSearchRequest request = bqlCompiler.compile(bql).getSearchRequest();
    LindenResult result = lindenCore.search(request);
    Assert.assertEquals(400, result.getTotalHits());
    bql = "select * from multi_0 source";
    request = bqlCompiler.compile(bql).getSearchRequest();
    result = lindenCore.search(request);
    Assert.assertEquals(50, result.getTotalHits());
    bql = "select * from non-existed source";
    request = bqlCompiler.compile(bql).getSearchRequest();
    result = lindenCore.search(request);
    Assert.assertFalse(result.isSuccess());
    bql = "select * from multi_1, multi_2 source";
    request = bqlCompiler.compile(bql).getSearchRequest();
    result = lindenCore.search(request);
    Assert.assertEquals(100, result.getTotalHits());
    handleRequest("{\"type\":\"delete_index\", \"index\":\"multi_0\"}");
    bql = "select * from multi_0 source";
    request = bqlCompiler.compile(bql).getSearchRequest();
    result = lindenCore.search(request);
    Assert.assertFalse(result.isSuccess());
    handleRequest("{\"type\":\"delete_index\", \"index\":\"multi_2\"}");
    Assert.assertEquals(6, lindenCore.getServiceInfo().getIndexNamesSize());
    Assert.assertEquals(300, lindenCore.getServiceInfo().getDocsNum());
    // delete doc 50, 200, 300, 390, 397, doc 0 and doc 100 doesn't exist since multi_0 and multi_1 index are deleted
    bql = "delete from linden where query is 'title:0 title:50 title:100 title:200 title:300 title:390 title:397'";
    LindenDeleteRequest deleteRequest = bqlCompiler.compile(bql).getDeleteRequest();
    lindenCore.delete(deleteRequest);
    lindenCore.commit();
    lindenCore.refresh();
    Assert.assertEquals(295, lindenCore.getServiceInfo().getDocsNum());
    lindenCore.close();
}
Also used : LindenDeleteRequest(com.xiaomi.linden.thrift.common.LindenDeleteRequest) LindenResult(com.xiaomi.linden.thrift.common.LindenResult) JSONObject(com.alibaba.fastjson.JSONObject) LindenSearchRequest(com.xiaomi.linden.thrift.common.LindenSearchRequest) MultiLindenCoreImpl(com.xiaomi.linden.core.search.MultiLindenCoreImpl) Test(org.junit.Test)

Example 78 with LindenResult

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

the class CoreLindenServiceImpl method search.

@Override
public Future<LindenResult> search(final LindenSearchRequest request) {
    final Stopwatch sw = Stopwatch.createStarted();
    return instanceExecutorPool.apply(new Function0<LindenResult>() {

        @Override
        public LindenResult apply() {
            LindenResult result = null;
            String logTag = null;
            long eps = 0;
            try {
                eps = sw.elapsed(TimeUnit.MILLISECONDS);
                if (eps > 10) {
                    LOGGER.warn("Warning: instanceExecutorPool took " + eps + "ms to start search.");
                    if (eps > instanceFuturePoolWaitTimeout) {
                        result = buildLindenFailedResult("Waiting time is too long, " + eps + "ms in instance future pool");
                        logTag = "instancePoolWaitTimeout";
                        return result;
                    }
                }
                result = lindenCore.search(request);
                if (result.isSuccess()) {
                    logTag = "instanceSearch";
                } else {
                    logTag = "instanceFailureSearch";
                }
            } catch (Exception e) {
                String errorStackInfo = Throwables.getStackTraceAsString(e);
                result = buildLindenFailedResult(errorStackInfo);
                logTag = "instanceExceptionalSearch";
            } finally {
                metricsManager.time(sw.elapsed(TimeUnit.NANOSECONDS), logTag);
                long cost = sw.elapsed(TimeUnit.MILLISECONDS);
                result.setCost((int) cost);
                if (result.isSuccess()) {
                    if (cost >= slowQueryThresholdMillis) {
                        SLOW_LOGGER.warn("Slow request: {}, totalHits: {}, waits: {} ms , cost: {} ms.", request.getOriginQuery(), result.getTotalHits(), eps, result.getCost());
                    } else {
                        LOGGER.info("Instance search request succeeded, request: {}, hits: {}, cost: {} ms.", request.getOriginQuery(), result.getHitsSize(), result.getCost());
                    }
                } else {
                    LOGGER.error("Instance search request failed, request: {}, error: {}, cost: {} ms.", request.getOriginQuery(), result.getError(), result.getCost());
                }
                return result;
            }
        }
    });
}
Also used : LindenResult(com.xiaomi.linden.thrift.common.LindenResult) Stopwatch(com.google.common.base.Stopwatch) IOException(java.io.IOException)

Example 79 with LindenResult

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

the class LindenController method search.

@RequestMapping(value = "/search", method = RequestMethod.GET)
@ResponseBody
public String search(@RequestParam("bql") String bql) {
    LindenResult result;
    try {
        Future<LindenResult> future = LindenAdmin.getService().handleClusterSearchRequest(bql);
        result = Await.result(future, Duration.apply(30000, TimeUnit.MILLISECONDS));
    } catch (Exception e) {
        result = new LindenResult();
        result.setSuccess(false).setError(Throwables.getStackTraceAsString(e));
    }
    return ThriftToJSON(result);
}
Also used : LindenResult(com.xiaomi.linden.thrift.common.LindenResult) TException(org.apache.thrift.TException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

LindenResult (com.xiaomi.linden.thrift.common.LindenResult)79 LindenSearchRequest (com.xiaomi.linden.thrift.common.LindenSearchRequest)69 Test (org.junit.Test)69 IOException (java.io.IOException)7 LindenQuery (com.xiaomi.linden.thrift.common.LindenQuery)5 JSONObject (com.alibaba.fastjson.JSONObject)4 Stopwatch (com.google.common.base.Stopwatch)4 MultiLindenCoreImpl (com.xiaomi.linden.core.search.MultiLindenCoreImpl)3 LindenFlexibleQueryBuilder (com.xiaomi.linden.thrift.builder.query.LindenFlexibleQueryBuilder)3 LindenDeleteRequest (com.xiaomi.linden.thrift.common.LindenDeleteRequest)3 LindenHit (com.xiaomi.linden.thrift.common.LindenHit)3 LindenRequest (com.xiaomi.linden.thrift.common.LindenRequest)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 FlexibleQuery (com.xiaomi.linden.lucene.query.flexiblequery.FlexibleQuery)2 Response (com.xiaomi.linden.thrift.common.Response)2 BooleanQuery (org.apache.lucene.search.BooleanQuery)2 DisjunctionMaxQuery (org.apache.lucene.search.DisjunctionMaxQuery)2 FilteredQuery (org.apache.lucene.search.FilteredQuery)2 Query (org.apache.lucene.search.Query)2