# -*- coding: utf-8 -*- # SPDX-License-Identifier: Apache-2.0 # # The OpenSearch Contributors require contributions made to # this file be licensed under the Apache-2.0 license or a # compatible open source license. # # Modifications Copyright OpenSearch Contributors. See # GitHub history for details. # # Licensed to Elasticsearch B.V. under one or more contributor # license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright # ownership. Elasticsearch B.V. licenses this file to you under # the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, # software distributed under the License is distributed on an # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. from __future__ import unicode_literals from opensearchpy import TransportError from pytest import raises from opensearch_dsl import Date, Document, Keyword, MultiSearch, Q, Search, Text from opensearch_dsl.response import aggs from .test_data import FLAT_DATA class Repository(Document): created_at = Date() description = Text(analyzer="snowball") tags = Keyword() @classmethod def search(cls): return super(Repository, cls).search().filter("term", commit_repo="repo") class Index: name = "git" class Commit(Document): class Index: name = "flat-git" def test_filters_aggregation_buckets_are_accessible(data_client): has_tests_query = Q("term", files="test_opensearch_dsl") s = Commit.search()[0:0] s.aggs.bucket("top_authors", "terms", field="author.name.raw").bucket( "has_tests", "filters", filters={"yes": has_tests_query, "no": ~has_tests_query} ).metric("lines", "stats", field="stats.lines") response = s.execute() assert isinstance( response.aggregations.top_authors.buckets[0].has_tests.buckets.yes, aggs.Bucket ) assert ( 35 == response.aggregations.top_authors.buckets[0].has_tests.buckets.yes.doc_count ) assert ( 228 == response.aggregations.top_authors.buckets[0].has_tests.buckets.yes.lines.max ) def test_top_hits_are_wrapped_in_response(data_client): s = Commit.search()[0:0] s.aggs.bucket("top_authors", "terms", field="author.name.raw").metric( "top_commits", "top_hits", size=5 ) response = s.execute() top_commits = response.aggregations.top_authors.buckets[0].top_commits assert isinstance(top_commits, aggs.TopHitsData) assert 5 == len(top_commits) hits = [h for h in top_commits] assert 5 == len(hits) assert isinstance(hits[0], Commit) def test_inner_hits_are_wrapped_in_response(data_client): s = Search(index="git")[0:1].query( "has_parent", parent_type="repo", inner_hits={}, query=Q("match_all") ) response = s.execute() commit = response.hits[0] assert isinstance(commit.meta.inner_hits.repo, response.__class__) assert repr(commit.meta.inner_hits.repo[0]).startswith( "