Search in sources :

Example 1 with RangeQueryBuilder

use of org.elasticsearch.index.query.RangeQueryBuilder in project elasticsearch by elastic.

the class EquivalenceIT method testRandomRanges.

// Make sure that unordered, reversed, disjoint and/or overlapping ranges are supported
// Duel with filters
public void testRandomRanges() throws Exception {
    final int numDocs = scaledRandomIntBetween(500, 5000);
    final double[][] docs = new double[numDocs][];
    for (int i = 0; i < numDocs; ++i) {
        final int numValues = randomInt(5);
        docs[i] = new double[numValues];
        for (int j = 0; j < numValues; ++j) {
            docs[i][j] = randomDouble() * 100;
        }
    }
    prepareCreate("idx").addMapping("type", jsonBuilder().startObject().startObject("type").startObject("properties").startObject("values").field("type", "double").endObject().endObject().endObject().endObject()).execute().actionGet();
    for (int i = 0; i < docs.length; ++i) {
        XContentBuilder source = jsonBuilder().startObject().startArray("values");
        for (int j = 0; j < docs[i].length; ++j) {
            source = source.value(docs[i][j]);
        }
        source = source.endArray().endObject();
        client().prepareIndex("idx", "type").setSource(source).execute().actionGet();
    }
    assertNoFailures(client().admin().indices().prepareRefresh("idx").setIndicesOptions(IndicesOptions.lenientExpandOpen()).get());
    final int numRanges = randomIntBetween(1, 20);
    final double[][] ranges = new double[numRanges][];
    for (int i = 0; i < ranges.length; ++i) {
        switch(randomInt(2)) {
            case 0:
                ranges[i] = new double[] { Double.NEGATIVE_INFINITY, randomInt(100) };
                break;
            case 1:
                ranges[i] = new double[] { randomInt(100), Double.POSITIVE_INFINITY };
                break;
            case 2:
                ranges[i] = new double[] { randomInt(100), randomInt(100) };
                break;
            default:
                throw new AssertionError();
        }
    }
    RangeAggregationBuilder query = range("range").field("values");
    for (int i = 0; i < ranges.length; ++i) {
        String key = Integer.toString(i);
        if (ranges[i][0] == Double.NEGATIVE_INFINITY) {
            query.addUnboundedTo(key, ranges[i][1]);
        } else if (ranges[i][1] == Double.POSITIVE_INFINITY) {
            query.addUnboundedFrom(key, ranges[i][0]);
        } else {
            query.addRange(key, ranges[i][0], ranges[i][1]);
        }
    }
    SearchRequestBuilder reqBuilder = client().prepareSearch("idx").addAggregation(query);
    for (int i = 0; i < ranges.length; ++i) {
        RangeQueryBuilder filter = QueryBuilders.rangeQuery("values");
        if (ranges[i][0] != Double.NEGATIVE_INFINITY) {
            filter = filter.from(ranges[i][0]);
        }
        if (ranges[i][1] != Double.POSITIVE_INFINITY) {
            filter = filter.to(ranges[i][1]);
        }
        reqBuilder = reqBuilder.addAggregation(filter("filter" + i, filter));
    }
    SearchResponse resp = reqBuilder.execute().actionGet();
    Range range = resp.getAggregations().get("range");
    List<? extends Bucket> buckets = range.getBuckets();
    HashMap<String, Bucket> bucketMap = new HashMap<>(buckets.size());
    for (Bucket bucket : buckets) {
        bucketMap.put(bucket.getKeyAsString(), bucket);
    }
    for (int i = 0; i < ranges.length; ++i) {
        long count = 0;
        for (double[] values : docs) {
            for (double value : values) {
                if (value >= ranges[i][0] && value < ranges[i][1]) {
                    ++count;
                    break;
                }
            }
        }
        final Range.Bucket bucket = bucketMap.get(Integer.toString(i));
        assertEquals(bucket.getKeyAsString(), Integer.toString(i), bucket.getKeyAsString());
        assertEquals(bucket.getKeyAsString(), count, bucket.getDocCount());
        final Filter filter = resp.getAggregations().get("filter" + i);
        assertThat(filter.getDocCount(), equalTo(count));
    }
}
Also used : Bucket(org.elasticsearch.search.aggregations.bucket.range.Range.Bucket) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) HashMap(java.util.HashMap) RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) Range(org.elasticsearch.search.aggregations.bucket.range.Range) RangeAggregationBuilder(org.elasticsearch.search.aggregations.bucket.range.RangeAggregationBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) Bucket(org.elasticsearch.search.aggregations.bucket.range.Range.Bucket) Filter(org.elasticsearch.search.aggregations.bucket.filter.Filter) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 2 with RangeQueryBuilder

use of org.elasticsearch.index.query.RangeQueryBuilder in project elasticsearch by elastic.

the class CreateIndexIT method testCreateAndDeleteIndexConcurrently.

public void testCreateAndDeleteIndexConcurrently() throws InterruptedException {
    createIndex("test");
    final AtomicInteger indexVersion = new AtomicInteger(0);
    final Object indexVersionLock = new Object();
    final CountDownLatch latch = new CountDownLatch(1);
    int numDocs = randomIntBetween(1, 10);
    for (int i = 0; i < numDocs; i++) {
        client().prepareIndex("test", "test").setSource("index_version", indexVersion.get()).get();
    }
    synchronized (indexVersionLock) {
        // not necessarily needed here but for completeness we lock here too
        indexVersion.incrementAndGet();
    }
    client().admin().indices().prepareDelete("test").execute(new // this happens async!!!
    ActionListener<DeleteIndexResponse>() {

        @Override
        public void onResponse(DeleteIndexResponse deleteIndexResponse) {
            Thread thread = new Thread() {

                @Override
                public void run() {
                    try {
                        // recreate that index
                        client().prepareIndex("test", "test").setSource("index_version", indexVersion.get()).get();
                        synchronized (indexVersionLock) {
                            // we sync here since we have to ensure that all indexing operations below for a given ID are done before
                            // we increment the index version otherwise a doc that is in-flight could make it into an index that it
                            // was supposed to be deleted for and our assertion fail...
                            indexVersion.incrementAndGet();
                        }
                        // from here on all docs with index_version == 0|1 must be gone!!!! only 2 are ok;
                        assertAcked(client().admin().indices().prepareDelete("test").get());
                    } finally {
                        latch.countDown();
                    }
                }
            };
            thread.start();
        }

        @Override
        public void onFailure(Exception e) {
            throw new RuntimeException(e);
        }
    });
    numDocs = randomIntBetween(100, 200);
    for (int i = 0; i < numDocs; i++) {
        try {
            synchronized (indexVersionLock) {
                client().prepareIndex("test", "test").setSource("index_version", indexVersion.get()).setTimeout(TimeValue.timeValueSeconds(10)).get();
            }
        } catch (IndexNotFoundException inf) {
        // fine
        } catch (UnavailableShardsException ex) {
            assertEquals(ex.getCause().getClass(), IndexNotFoundException.class);
        // fine we run into a delete index while retrying
        }
    }
    latch.await();
    refresh();
    // we only really assert that we never reuse segments of old indices or anything like this here and that nothing fails with
    // crazy exceptions
    SearchResponse expected = client().prepareSearch("test").setIndicesOptions(IndicesOptions.lenientExpandOpen()).setQuery(new RangeQueryBuilder("index_version").from(indexVersion.get(), true)).get();
    SearchResponse all = client().prepareSearch("test").setIndicesOptions(IndicesOptions.lenientExpandOpen()).get();
    assertEquals(expected + " vs. " + all, expected.getHits().getTotalHits(), all.getHits().getTotalHits());
    logger.info("total: {}", expected.getHits().getTotalHits());
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) UnavailableShardsException(org.elasticsearch.action.UnavailableShardsException) SearchResponse(org.elasticsearch.action.search.SearchResponse) DeleteIndexResponse(org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse) UnavailableShardsException(org.elasticsearch.action.UnavailableShardsException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException)

Example 3 with RangeQueryBuilder

use of org.elasticsearch.index.query.RangeQueryBuilder in project zipkin by openzipkin.

the class ElasticsearchSpanStore method getTraces.

@Override
public ListenableFuture<List<List<Span>>> getTraces(final QueryRequest request) {
    long endMillis = request.endTs;
    long beginMillis = endMillis - request.lookback;
    BoolQueryBuilder filter = boolQuery().must(rangeQuery("timestamp_millis").gte(beginMillis).lte(endMillis));
    if (request.serviceName != null) {
        filter.must(boolQuery().should(nestedQuery("annotations", termQuery("annotations.endpoint.serviceName", request.serviceName))).should(nestedQuery("binaryAnnotations", termQuery("binaryAnnotations.endpoint.serviceName", request.serviceName))));
    }
    if (request.spanName != null) {
        filter.must(termQuery("name", request.spanName));
    }
    for (String annotation : request.annotations) {
        BoolQueryBuilder annotationQuery = boolQuery().must(termQuery("annotations.value", annotation));
        if (request.serviceName != null) {
            annotationQuery.must(termQuery("annotations.endpoint.serviceName", request.serviceName));
        }
        filter.must(nestedQuery("annotations", annotationQuery));
    }
    for (Map.Entry<String, String> kv : request.binaryAnnotations.entrySet()) {
        // In our index template, we make sure the binaryAnnotation value is indexed as string,
        // meaning non-string values won't even be indexed at all. This means that we can only
        // match string values here, which happens to be exactly what we want.
        BoolQueryBuilder binaryAnnotationQuery = boolQuery().must(termQuery("binaryAnnotations.key", kv.getKey())).must(termQuery("binaryAnnotations.value", kv.getValue()));
        if (request.serviceName != null) {
            binaryAnnotationQuery.must(termQuery("binaryAnnotations.endpoint.serviceName", request.serviceName));
        }
        filter.must(nestedQuery("binaryAnnotations", binaryAnnotationQuery));
    }
    if (request.minDuration != null) {
        RangeQueryBuilder durationQuery = rangeQuery("duration").gte(request.minDuration);
        if (request.maxDuration != null) {
            durationQuery.lte(request.maxDuration);
        }
        filter.must(durationQuery);
    }
    Set<String> strings = indexNameFormatter.indexNamePatternsForRange(beginMillis, endMillis);
    final String[] indices = strings.toArray(new String[0]);
    // We need to filter to traces that contain at least one span that matches the request,
    // but the zipkin API is supposed to order traces by first span, regardless of if it was
    // filtered or not. This is not possible without either multiple, heavyweight queries
    // or complex multiple indexing, defeating much of the elegance of using elasticsearch for this.
    // So we fudge and order on the first span among the filtered spans - in practice, there should
    // be no significant difference in user experience since span start times are usually very
    // close to each other in human time.
    ListenableFuture<List<String>> traceIds = client.collectBucketKeys(indices, boolQuery().must(matchAllQuery()).filter(filter), AggregationBuilders.terms("traceId_agg").field("traceId").subAggregation(AggregationBuilders.min("timestamps_agg").field("timestamp_millis")).order(Order.aggregation("timestamps_agg", false)).size(request.limit));
    return transform(traceIds, new AsyncFunction<List<String>, List<List<Span>>>() {

        @Override
        public ListenableFuture<List<List<Span>>> apply(List<String> input) {
            return getTracesByIds(input, indices, request);
        }
    });
}
Also used : RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) Span(zipkin.Span) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) List(java.util.List) Map(java.util.Map)

Example 4 with RangeQueryBuilder

use of org.elasticsearch.index.query.RangeQueryBuilder in project YCSB by brianfrankcooper.

the class ElasticsearchClient method scan.

/**
   * Perform a range scan for a set of records in the database. Each field/value
   * pair from the result will be stored in a HashMap.
   *
   * @param table
   *          The name of the table
   * @param startkey
   *          The record key of the first record to read.
   * @param recordcount
   *          The number of records to read
   * @param fields
   *          The list of fields to read, or null for all of them
   * @param result
   *          A Vector of HashMaps, where each HashMap is a set field/value
   *          pairs for one record
   * @return Zero on success, a non-zero error code on error. See this class's
   *         description for a discussion of error codes.
   */
@Override
public Status scan(String table, String startkey, int recordcount, Set<String> fields, Vector<HashMap<String, ByteIterator>> result) {
    try {
        final RangeQueryBuilder rangeQuery = rangeQuery("_id").gte(startkey);
        final SearchResponse response = client.prepareSearch(indexKey).setTypes(table).setQuery(rangeQuery).setSize(recordcount).execute().actionGet();
        HashMap<String, ByteIterator> entry;
        for (SearchHit hit : response.getHits()) {
            entry = new HashMap<>(fields.size());
            for (String field : fields) {
                entry.put(field, new StringByteIterator((String) hit.getSource().get(field)));
            }
            result.add(entry);
        }
        return Status.OK;
    } catch (Exception e) {
        e.printStackTrace();
        return Status.ERROR;
    }
}
Also used : StringByteIterator(com.yahoo.ycsb.StringByteIterator) ByteIterator(com.yahoo.ycsb.ByteIterator) SearchHit(org.elasticsearch.search.SearchHit) StringByteIterator(com.yahoo.ycsb.StringByteIterator) RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) DBException(com.yahoo.ycsb.DBException) UnknownHostException(java.net.UnknownHostException) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 5 with RangeQueryBuilder

use of org.elasticsearch.index.query.RangeQueryBuilder in project fess by codelibs.

the class BsAccessTokenCQ method setToken_GreaterThan.

public void setToken_GreaterThan(String token, ConditionOptionCall<RangeQueryBuilder> opLambda) {
    final Object _value = token;
    RangeQueryBuilder builder = regRangeQ("token", ConditionKey.CK_GREATER_THAN, _value);
    if (opLambda != null) {
        opLambda.callback(builder);
    }
}
Also used : RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder)

Aggregations

RangeQueryBuilder (org.elasticsearch.index.query.RangeQueryBuilder)1199 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)6 FunctionScoreQueryBuilder (org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder)5 MatchAllQueryBuilder (org.elasticsearch.index.query.MatchAllQueryBuilder)4 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)4 SearchResponse (org.elasticsearch.action.search.SearchResponse)3 CommonTermsQueryBuilder (org.elasticsearch.index.query.CommonTermsQueryBuilder)3 ExistsQueryBuilder (org.elasticsearch.index.query.ExistsQueryBuilder)3 IdsQueryBuilder (org.elasticsearch.index.query.IdsQueryBuilder)3 MatchPhrasePrefixQueryBuilder (org.elasticsearch.index.query.MatchPhrasePrefixQueryBuilder)3 MatchPhraseQueryBuilder (org.elasticsearch.index.query.MatchPhraseQueryBuilder)3 MatchQueryBuilder (org.elasticsearch.index.query.MatchQueryBuilder)3 MoreLikeThisQueryBuilder (org.elasticsearch.index.query.MoreLikeThisQueryBuilder)3 PrefixQueryBuilder (org.elasticsearch.index.query.PrefixQueryBuilder)3 QueryStringQueryBuilder (org.elasticsearch.index.query.QueryStringQueryBuilder)3 RegexpQueryBuilder (org.elasticsearch.index.query.RegexpQueryBuilder)3 SpanTermQueryBuilder (org.elasticsearch.index.query.SpanTermQueryBuilder)3 TermQueryBuilder (org.elasticsearch.index.query.TermQueryBuilder)3 TermsQueryBuilder (org.elasticsearch.index.query.TermsQueryBuilder)3 WildcardQueryBuilder (org.elasticsearch.index.query.WildcardQueryBuilder)3