Search in sources :

Example 1 with Aggregator

use of org.elasticsearch.search.aggregations.Aggregator in project elasticsearch by elastic.

the class NestedAggregatorTests method testResetRootDocId.

public void testResetRootDocId() throws Exception {
    Directory directory = newDirectory();
    IndexWriterConfig iwc = new IndexWriterConfig(null);
    iwc.setMergePolicy(NoMergePolicy.INSTANCE);
    RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory, iwc);
    List<Document> documents = new ArrayList<>();
    // 1 segment with, 1 root document, with 3 nested sub docs
    Document document = new Document();
    document.add(new Field(UidFieldMapper.NAME, "type#1", UidFieldMapper.Defaults.NESTED_FIELD_TYPE));
    document.add(new Field(TypeFieldMapper.NAME, "__nested_field", TypeFieldMapper.Defaults.FIELD_TYPE));
    documents.add(document);
    document = new Document();
    document.add(new Field(UidFieldMapper.NAME, "type#1", UidFieldMapper.Defaults.NESTED_FIELD_TYPE));
    document.add(new Field(TypeFieldMapper.NAME, "__nested_field", TypeFieldMapper.Defaults.FIELD_TYPE));
    documents.add(document);
    document = new Document();
    document.add(new Field(UidFieldMapper.NAME, "type#1", UidFieldMapper.Defaults.NESTED_FIELD_TYPE));
    document.add(new Field(TypeFieldMapper.NAME, "__nested_field", TypeFieldMapper.Defaults.FIELD_TYPE));
    documents.add(document);
    document = new Document();
    document.add(new Field(UidFieldMapper.NAME, "type#1", UidFieldMapper.Defaults.FIELD_TYPE));
    document.add(new Field(TypeFieldMapper.NAME, "test", TypeFieldMapper.Defaults.FIELD_TYPE));
    documents.add(document);
    indexWriter.addDocuments(documents);
    indexWriter.commit();
    documents.clear();
    // 1 segment with:
    // 1 document, with 1 nested subdoc
    document = new Document();
    document.add(new Field(UidFieldMapper.NAME, "type#2", UidFieldMapper.Defaults.NESTED_FIELD_TYPE));
    document.add(new Field(TypeFieldMapper.NAME, "__nested_field", TypeFieldMapper.Defaults.FIELD_TYPE));
    documents.add(document);
    document = new Document();
    document.add(new Field(UidFieldMapper.NAME, "type#2", UidFieldMapper.Defaults.FIELD_TYPE));
    document.add(new Field(TypeFieldMapper.NAME, "test", TypeFieldMapper.Defaults.FIELD_TYPE));
    documents.add(document);
    indexWriter.addDocuments(documents);
    documents.clear();
    // and 1 document, with 1 nested subdoc
    document = new Document();
    document.add(new Field(UidFieldMapper.NAME, "type#3", UidFieldMapper.Defaults.NESTED_FIELD_TYPE));
    document.add(new Field(TypeFieldMapper.NAME, "__nested_field", TypeFieldMapper.Defaults.FIELD_TYPE));
    documents.add(document);
    document = new Document();
    document.add(new Field(UidFieldMapper.NAME, "type#3", UidFieldMapper.Defaults.FIELD_TYPE));
    document.add(new Field(TypeFieldMapper.NAME, "test", TypeFieldMapper.Defaults.FIELD_TYPE));
    documents.add(document);
    indexWriter.addDocuments(documents);
    indexWriter.commit();
    indexWriter.close();
    IndexService indexService = createIndex("test");
    DirectoryReader directoryReader = DirectoryReader.open(directory);
    directoryReader = ElasticsearchDirectoryReader.wrap(directoryReader, new ShardId(indexService.index(), 0));
    IndexSearcher searcher = new IndexSearcher(directoryReader);
    indexService.mapperService().merge("test", new CompressedXContent(PutMappingRequest.buildFromSimplifiedDef("test", "nested_field", "type=nested").string()), MapperService.MergeReason.MAPPING_UPDATE, false);
    SearchContext context = createSearchContext(indexService);
    AggregatorFactories.Builder builder = AggregatorFactories.builder();
    NestedAggregationBuilder factory = new NestedAggregationBuilder("test", "nested_field");
    builder.addAggregator(factory);
    AggregatorFactories factories = builder.build(context, null);
    context.aggregations(new SearchContextAggregations(factories));
    Aggregator[] aggs = factories.createTopLevelAggregators();
    BucketCollector collector = BucketCollector.wrap(Arrays.asList(aggs));
    collector.preCollection();
    // A regular search always exclude nested docs, so we use NonNestedDocsFilter.INSTANCE here (otherwise MatchAllDocsQuery would be sufficient)
    // We exclude root doc with uid type#2, this will trigger the bug if we don't reset the root doc when we process a new segment, because
    // root doc type#3 and root doc type#1 have the same segment docid
    BooleanQuery.Builder bq = new BooleanQuery.Builder();
    bq.add(Queries.newNonNestedFilter(), Occur.MUST);
    bq.add(new TermQuery(new Term(UidFieldMapper.NAME, "type#2")), Occur.MUST_NOT);
    searcher.search(new ConstantScoreQuery(bq.build()), collector);
    collector.postCollection();
    Nested nested = (Nested) aggs[0].buildAggregation(0);
    // The bug manifests if 6 docs are returned, because currentRootDoc isn't reset the previous child docs from the first segment are emitted as hits.
    assertThat(nested.getDocCount(), equalTo(4L));
    directoryReader.close();
    directory.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) BooleanQuery(org.apache.lucene.search.BooleanQuery) IndexService(org.elasticsearch.index.IndexService) ArrayList(java.util.ArrayList) SearchContext(org.elasticsearch.search.internal.SearchContext) Document(org.apache.lucene.document.Document) ShardId(org.elasticsearch.index.shard.ShardId) Field(org.apache.lucene.document.Field) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) AggregatorFactories(org.elasticsearch.search.aggregations.AggregatorFactories) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) Directory(org.apache.lucene.store.Directory) TermQuery(org.apache.lucene.search.TermQuery) ElasticsearchDirectoryReader(org.elasticsearch.common.lucene.index.ElasticsearchDirectoryReader) DirectoryReader(org.apache.lucene.index.DirectoryReader) SearchContextAggregations(org.elasticsearch.search.aggregations.SearchContextAggregations) Aggregator(org.elasticsearch.search.aggregations.Aggregator) Term(org.apache.lucene.index.Term) BucketCollector(org.elasticsearch.search.aggregations.BucketCollector) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 2 with Aggregator

use of org.elasticsearch.search.aggregations.Aggregator in project elasticsearch by elastic.

the class AggregationPath method resolveTopmostAggregator.

/**
     * Resolves the topmost aggregator pointed by this path using the given root as a point of reference.
     *
     * @param root      The point of reference of this path
     * @return          The first child aggregator of the root pointed by this path
     */
public Aggregator resolveTopmostAggregator(Aggregator root) {
    AggregationPath.PathElement token = pathElements.get(0);
    Aggregator aggregator = root.subAggregator(token.name);
    assert (aggregator instanceof SingleBucketAggregator) || (aggregator instanceof NumericMetricsAggregator) : "this should be picked up before aggregation execution - on validate";
    return aggregator;
}
Also used : NumericMetricsAggregator(org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregator) Aggregator(org.elasticsearch.search.aggregations.Aggregator) SingleBucketAggregator(org.elasticsearch.search.aggregations.bucket.SingleBucketAggregator) NumericMetricsAggregator(org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregator) SingleBucketAggregator(org.elasticsearch.search.aggregations.bucket.SingleBucketAggregator)

Example 3 with Aggregator

use of org.elasticsearch.search.aggregations.Aggregator in project elasticsearch by elastic.

the class AggregationPath method validate.

/**
     * Validates this path over the given aggregator as a point of reference.
     *
     * @param root  The point of reference of this path
     */
public void validate(Aggregator root) {
    Aggregator aggregator = root;
    for (int i = 0; i < pathElements.size(); i++) {
        aggregator = aggregator.subAggregator(pathElements.get(i).name);
        if (aggregator == null) {
            throw new AggregationExecutionException("Invalid term-aggregator order path [" + this + "]. Unknown aggregation [" + pathElements.get(i).name + "]");
        }
        if (i < pathElements.size() - 1) {
            if (!(aggregator instanceof SingleBucketAggregator)) {
                throw new AggregationExecutionException("Invalid terms aggregation order path [" + this + "]. Terms buckets can only be sorted on a sub-aggregator path " + "that is built out of zero or more single-bucket aggregations within the path and a final " + "single-bucket or a metrics aggregation at the path end. Sub-path [" + subPath(0, i + 1) + "] points to non single-bucket aggregation");
            }
            if (pathElements.get(i).key != null) {
                throw new AggregationExecutionException("Invalid terms aggregation order path [" + this + "]. Terms buckets can only be sorted on a sub-aggregator path " + "that is built out of zero or more single-bucket aggregations within the path and a " + "final single-bucket or a metrics aggregation at the path end. Sub-path [" + subPath(0, i + 1) + "] points to non single-bucket aggregation");
            }
        }
    }
    boolean singleBucket = aggregator instanceof SingleBucketAggregator;
    if (!singleBucket && !(aggregator instanceof NumericMetricsAggregator)) {
        throw new AggregationExecutionException("Invalid terms aggregation order path [" + this + "]. Terms buckets can only be sorted on a sub-aggregator path " + "that is built out of zero or more single-bucket aggregations within the path and a final " + "single-bucket or a metrics aggregation at the path end.");
    }
    AggregationPath.PathElement lastToken = lastPathElement();
    if (singleBucket) {
        if (lastToken.key != null && !"doc_count".equals(lastToken.key)) {
            throw new AggregationExecutionException("Invalid terms aggregation order path [" + this + "]. Ordering on a single-bucket aggregation can only be done on its doc_count. " + "Either drop the key (a la \"" + lastToken.name + "\") or change it to \"doc_count\" (a la \"" + lastToken.name + ".doc_count\")");
        }
        // perfectly valid to sort on single-bucket aggregation (will be sored on its doc_count)
        return;
    }
    if (aggregator instanceof NumericMetricsAggregator.SingleValue) {
        if (lastToken.key != null && !"value".equals(lastToken.key)) {
            throw new AggregationExecutionException("Invalid terms aggregation order path [" + this + "]. Ordering on a single-value metrics aggregation can only be done on its value. " + "Either drop the key (a la \"" + lastToken.name + "\") or change it to \"value\" (a la \"" + lastToken.name + ".value\")");
        }
        // perfectly valid to sort on single metric aggregation (will be sorted on its associated value)
        return;
    }
    // the aggregator must be of a multi-value metrics type
    if (lastToken.key == null) {
        throw new AggregationExecutionException("Invalid terms aggregation order path [" + this + "]. When ordering on a multi-value metrics aggregation a metric name must be specified");
    }
    if (!((NumericMetricsAggregator.MultiValue) aggregator).hasMetric(lastToken.key)) {
        throw new AggregationExecutionException("Invalid terms aggregation order path [" + this + "]. Unknown metric name [" + lastToken.key + "] on multi-value metrics aggregation [" + lastToken.name + "]");
    }
}
Also used : NumericMetricsAggregator(org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregator) Aggregator(org.elasticsearch.search.aggregations.Aggregator) SingleBucketAggregator(org.elasticsearch.search.aggregations.bucket.SingleBucketAggregator) NumericMetricsAggregator(org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregator) SingleBucketAggregator(org.elasticsearch.search.aggregations.bucket.SingleBucketAggregator) AggregationExecutionException(org.elasticsearch.search.aggregations.AggregationExecutionException)

Example 4 with Aggregator

use of org.elasticsearch.search.aggregations.Aggregator in project elasticsearch by elastic.

the class AggregationPath method resolveAggregator.

/**
     * Resolves the aggregator pointed by this path using the given root as a point of reference.
     *
     * @param root      The point of reference of this path
     * @return          The aggregator pointed by this path starting from the given aggregator as a point of reference
     */
public Aggregator resolveAggregator(Aggregator root) {
    Aggregator aggregator = root;
    for (int i = 0; i < pathElements.size(); i++) {
        AggregationPath.PathElement token = pathElements.get(i);
        aggregator = aggregator.subAggregator(token.name);
        assert (aggregator instanceof SingleBucketAggregator && i <= pathElements.size() - 1) || (aggregator instanceof NumericMetricsAggregator && i == pathElements.size() - 1) : "this should be picked up before aggregation execution - on validate";
    }
    return aggregator;
}
Also used : NumericMetricsAggregator(org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregator) Aggregator(org.elasticsearch.search.aggregations.Aggregator) SingleBucketAggregator(org.elasticsearch.search.aggregations.bucket.SingleBucketAggregator) NumericMetricsAggregator(org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregator) SingleBucketAggregator(org.elasticsearch.search.aggregations.bucket.SingleBucketAggregator)

Example 5 with Aggregator

use of org.elasticsearch.search.aggregations.Aggregator in project elasticsearch by elastic.

the class GeoHashGridAggregatorTests method testCase.

private void testCase(Query query, String field, int precision, CheckedConsumer<RandomIndexWriter, IOException> buildIndex, Consumer<InternalGeoHashGrid> verify) throws IOException {
    Directory directory = newDirectory();
    RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
    buildIndex.accept(indexWriter);
    indexWriter.close();
    IndexReader indexReader = DirectoryReader.open(directory);
    IndexSearcher indexSearcher = newSearcher(indexReader, true, true);
    GeoGridAggregationBuilder aggregationBuilder = new GeoGridAggregationBuilder("_name").field(field);
    aggregationBuilder.precision(precision);
    MappedFieldType fieldType = new GeoPointFieldMapper.GeoPointFieldType();
    fieldType.setHasDocValues(true);
    fieldType.setName(FIELD_NAME);
    try (Aggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType)) {
        aggregator.preCollection();
        indexSearcher.search(query, aggregator);
        aggregator.postCollection();
        verify.accept((InternalGeoHashGrid) aggregator.buildAggregation(0L));
    }
    indexReader.close();
    directory.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) IndexReader(org.apache.lucene.index.IndexReader) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) Aggregator(org.elasticsearch.search.aggregations.Aggregator) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Aggregations

Aggregator (org.elasticsearch.search.aggregations.Aggregator)5 SingleBucketAggregator (org.elasticsearch.search.aggregations.bucket.SingleBucketAggregator)3 NumericMetricsAggregator (org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregator)3 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)2 IndexSearcher (org.apache.lucene.search.IndexSearcher)2 Directory (org.apache.lucene.store.Directory)2 ArrayList (java.util.ArrayList)1 Document (org.apache.lucene.document.Document)1 Field (org.apache.lucene.document.Field)1 DirectoryReader (org.apache.lucene.index.DirectoryReader)1 IndexReader (org.apache.lucene.index.IndexReader)1 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)1 Term (org.apache.lucene.index.Term)1 BooleanQuery (org.apache.lucene.search.BooleanQuery)1 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)1 TermQuery (org.apache.lucene.search.TermQuery)1 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)1 ElasticsearchDirectoryReader (org.elasticsearch.common.lucene.index.ElasticsearchDirectoryReader)1 IndexService (org.elasticsearch.index.IndexService)1 MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)1