Search in sources :

Example 1 with SearchHit

use of io.druid.query.search.search.SearchHit in project druid by druid-io.

the class SearchBinaryFn method apply.

@Override
public Result<SearchResultValue> apply(Result<SearchResultValue> arg1, Result<SearchResultValue> arg2) {
    if (arg1 == null) {
        return arg2;
    }
    if (arg2 == null) {
        return arg1;
    }
    final int limit = gran instanceof AllGranularity ? this.limit : -1;
    SearchResultValue arg1Vals = arg1.getValue();
    SearchResultValue arg2Vals = arg2.getValue();
    Iterable<SearchHit> merged = Iterables.mergeSorted(Arrays.asList(arg1Vals, arg2Vals), searchSortSpec.getComparator());
    int maxSize = arg1Vals.getValue().size() + arg2Vals.getValue().size();
    if (limit > 0) {
        maxSize = Math.min(limit, maxSize);
    }
    List<SearchHit> results = Lists.newArrayListWithExpectedSize(maxSize);
    SearchHit prev = null;
    for (SearchHit searchHit : merged) {
        if (prev == null) {
            prev = searchHit;
            continue;
        }
        if (prev.equals(searchHit)) {
            if (prev.getCount() != null && searchHit.getCount() != null) {
                prev = new SearchHit(prev.getDimension(), prev.getValue(), prev.getCount() + searchHit.getCount());
            } else {
                prev = new SearchHit(prev.getDimension(), prev.getValue());
            }
        } else {
            results.add(prev);
            prev = searchHit;
            if (limit > 0 && results.size() >= limit) {
                break;
            }
        }
    }
    if (prev != null && (limit < 0 || results.size() < limit)) {
        results.add(prev);
    }
    final DateTime timestamp = gran instanceof AllGranularity ? arg1.getTimestamp() : gran.bucketStart(arg1.getTimestamp());
    return new Result<SearchResultValue>(timestamp, new SearchResultValue(results));
}
Also used : SearchHit(io.druid.query.search.search.SearchHit) AllGranularity(io.druid.java.util.common.granularity.AllGranularity) DateTime(org.joda.time.DateTime) Result(io.druid.query.Result)

Example 2 with SearchHit

use of io.druid.query.search.search.SearchHit in project druid by druid-io.

the class SearchQueryRunner method run.

@Override
public Sequence<Result<SearchResultValue>> run(final Query<Result<SearchResultValue>> input, Map<String, Object> responseContext) {
    if (!(input instanceof SearchQuery)) {
        throw new ISE("Got a [%s] which isn't a %s", input.getClass(), SearchQuery.class);
    }
    final SearchQuery query = (SearchQuery) input;
    final List<SearchQueryExecutor> plan = strategySelector.strategize(query).getExecutionPlan(query, segment);
    final Object2IntRBTreeMap<SearchHit> retVal = new Object2IntRBTreeMap<>(query.getSort().getComparator());
    retVal.defaultReturnValue(0);
    int remain = query.getLimit();
    for (final SearchQueryExecutor executor : plan) {
        retVal.putAll(executor.execute(remain));
        remain -= retVal.size();
    }
    return makeReturnResult(segment, query.getLimit(), retVal);
}
Also used : SearchQuery(io.druid.query.search.search.SearchQuery) SearchHit(io.druid.query.search.search.SearchHit) ISE(io.druid.java.util.common.ISE) SearchQueryExecutor(io.druid.query.search.search.SearchQueryExecutor) Object2IntRBTreeMap(it.unimi.dsi.fastutil.objects.Object2IntRBTreeMap)

Example 3 with SearchHit

use of io.druid.query.search.search.SearchHit in project druid by druid-io.

the class SearchBinaryFnTest method testMergeCountWithNull.

@Test
public void testMergeCountWithNull() {
    Result<SearchResultValue> r1 = new Result<SearchResultValue>(currTime, new SearchResultValue(ImmutableList.<SearchHit>of(new SearchHit("blah", "foo"))));
    Result<SearchResultValue> r2 = new Result<SearchResultValue>(currTime, new SearchResultValue(ImmutableList.<SearchHit>of(new SearchHit("blah", "foo", 3))));
    Result<SearchResultValue> expected = r1;
    Result<SearchResultValue> actual = new SearchBinaryFn(new SearchSortSpec(StringComparators.LEXICOGRAPHIC), Granularities.ALL, Integer.MAX_VALUE).apply(r1, r2);
    Assert.assertEquals(expected.getTimestamp(), actual.getTimestamp());
    assertSearchMergeResult(expected.getValue(), actual.getValue());
}
Also used : SearchHit(io.druid.query.search.search.SearchHit) SearchSortSpec(io.druid.query.search.search.SearchSortSpec) Result(io.druid.query.Result) Test(org.junit.Test)

Example 4 with SearchHit

use of io.druid.query.search.search.SearchHit in project druid by druid-io.

the class SearchBinaryFnTest method testMergeDay.

@Test
public void testMergeDay() {
    Result<SearchResultValue> r1 = new Result<SearchResultValue>(currTime, new SearchResultValue(ImmutableList.<SearchHit>of(new SearchHit("blah", "foo"))));
    Result<SearchResultValue> r2 = new Result<SearchResultValue>(currTime, new SearchResultValue(ImmutableList.<SearchHit>of(new SearchHit("blah2", "foo2"))));
    Result<SearchResultValue> expected = new Result<SearchResultValue>(Granularities.DAY.bucketStart(currTime), new SearchResultValue(ImmutableList.<SearchHit>of(new SearchHit("blah", "foo"), new SearchHit("blah2", "foo2"))));
    Result<SearchResultValue> actual = new SearchBinaryFn(new SearchSortSpec(StringComparators.LEXICOGRAPHIC), Granularities.DAY, Integer.MAX_VALUE).apply(r1, r2);
    Assert.assertEquals(expected.getTimestamp(), actual.getTimestamp());
    assertSearchMergeResult(expected.getValue(), actual.getValue());
}
Also used : SearchHit(io.druid.query.search.search.SearchHit) SearchSortSpec(io.druid.query.search.search.SearchSortSpec) Result(io.druid.query.Result) Test(org.junit.Test)

Example 5 with SearchHit

use of io.druid.query.search.search.SearchHit in project druid by druid-io.

the class SearchBinaryFnTest method testMergeOneResultNull.

@Test
public void testMergeOneResultNull() {
    Result<SearchResultValue> r1 = new Result<SearchResultValue>(currTime, new SearchResultValue(ImmutableList.<SearchHit>of(new SearchHit("blah", "foo"))));
    Result<SearchResultValue> r2 = null;
    Result<SearchResultValue> expected = r1;
    Result<SearchResultValue> actual = new SearchBinaryFn(new SearchSortSpec(StringComparators.LEXICOGRAPHIC), Granularities.ALL, Integer.MAX_VALUE).apply(r1, r2);
    Assert.assertEquals(expected.getTimestamp(), actual.getTimestamp());
    assertSearchMergeResult(expected.getValue(), actual.getValue());
}
Also used : SearchHit(io.druid.query.search.search.SearchHit) SearchSortSpec(io.druid.query.search.search.SearchSortSpec) Result(io.druid.query.Result) Test(org.junit.Test)

Aggregations

SearchHit (io.druid.query.search.search.SearchHit)57 Test (org.junit.Test)45 Result (io.druid.query.Result)25 SearchQuery (io.druid.query.search.search.SearchQuery)22 SearchSortSpec (io.druid.query.search.search.SearchSortSpec)16 DateTime (org.joda.time.DateTime)12 QueryRunner (io.druid.query.QueryRunner)8 SearchResultValue (io.druid.query.search.SearchResultValue)7 HashMap (java.util.HashMap)6 DefaultDimensionSpec (io.druid.query.dimension.DefaultDimensionSpec)4 ExtractionDimensionSpec (io.druid.query.dimension.ExtractionDimensionSpec)4 AndDimFilter (io.druid.query.filter.AndDimFilter)4 DimFilter (io.druid.query.filter.DimFilter)4 ExtractionDimFilter (io.druid.query.filter.ExtractionDimFilter)4 SelectorDimFilter (io.druid.query.filter.SelectorDimFilter)4 TimeFormatExtractionFn (io.druid.query.extraction.TimeFormatExtractionFn)3 LookupExtractionFn (io.druid.query.lookup.LookupExtractionFn)3 QueryableIndexSegment (io.druid.segment.QueryableIndexSegment)3 ExtractionFn (io.druid.query.extraction.ExtractionFn)2 JavaScriptExtractionFn (io.druid.query.extraction.JavaScriptExtractionFn)2