1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
# 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.
require 'test_helper'
module Elasticsearch
module Test
class NestedAggregationIntegrationTest < ::Elasticsearch::Test::IntegrationTestCase
include Elasticsearch::DSL::Search
context "A nested aggregation" do
startup do
Elasticsearch::Extensions::Test::Cluster.start(number_of_nodes: 1) if ENV['SERVER'] and not Elasticsearch::Extensions::Test::Cluster.running?(number_of_nodes: 1)
end
setup do
@client.indices.create index: 'products-test', body: {
mappings: {
properties: {
title: {type: 'text'},
category: {type: 'keyword'},
offers: {
type: 'nested',
properties: {
name: {type: 'text'},
price: {type: 'double'}
}
}
}
}
}
@client.index index: 'products-test',
body: { title: 'A',
category: 'audio',
offers: [ { name: 'A1', price: 100 }, { name: 'A2', price: 120 } ] }
@client.index index: 'products-test',
body: { title: 'B',
category: 'audio',
offers: [ { name: 'B1', price: 200 }, { name: 'B2', price: 180 } ] }
@client.index index: 'products-test',
body: { title: 'C',
category: 'video',
offers: [ { name: 'C1', price: 300 }, { name: 'C2', price: 350 } ] }
@client.indices.refresh index: 'products-test'
end
should "return the minimal price from offers" do
response = @client.search index: 'products-test', body: search {
query { match title: 'A' }
aggregation :offers do
nested do
path 'offers'
aggregation :min_price do
min field: 'offers.price'
end
end
end
}.to_hash
assert_equal 100, response['aggregations']['offers']['min_price']['value'].to_i
end
should "return the top categories for offer price range" do
response = @client.search index: 'products-test', body: search {
query do
bool do
must do
nested do
path 'offers'
query do
bool do
filter do
range 'offers.price' do
gte 100
lte 300
end
end
end
end
end
end
end
end
aggregation :offers do
nested do
path 'offers'
aggregation :top_categories do
reverse_nested do
aggregation :top_category_per_offer do
terms field: 'category'
end
end
end
end
end
}.to_hash
assert_equal 2, response['aggregations']['offers']['top_categories']['top_category_per_offer']['buckets'].size
assert_equal 'audio', response['aggregations']['offers']['top_categories']['top_category_per_offer']['buckets'][0]['key']
assert_equal 'video', response['aggregations']['offers']['top_categories']['top_category_per_offer']['buckets'][1]['key']
end
end
end
end
end
|