1+ /*
2+ * Licensed to Elasticsearch under one or more contributor
3+ * license agreements. See the NOTICE file distributed with
4+ * this work for additional information regarding copyright
5+ * ownership. Elasticsearch licenses this file to you under
6+ * the Apache License, Version 2.0 (the "License"); you may
7+ * not use this file except in compliance with the License.
8+ * You may obtain a copy of the License at
9+ *
10+ * http://www.apache.org/licenses/LICENSE-2.0
11+ *
12+ * Unless required by applicable law or agreed to in writing,
13+ * software distributed under the License is distributed on an
14+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+ * KIND, either express or implied. See the License for the
16+ * specific language governing permissions and limitations
17+ * under the License.
18+ */
19+ package org .elasticsearch .search .aggregations .support ;
20+
21+ import java .io .IOException ;
22+ import java .util .ArrayList ;
23+ import java .util .List ;
24+ import org .apache .lucene .index .LeafReaderContext ;
25+ import org .elasticsearch .index .fielddata .NumericDoubleValues ;
26+ import org .elasticsearch .search .MultiValueMode ;
27+ import org .elasticsearch .search .aggregations .support .ValuesSource .Numeric ;
28+
29+ /**
30+ * Class to encapsulate a set of ValuesSource objects labeled by field name
31+ */
32+ public abstract class MultiValuesSource <VS extends ValuesSource > {
33+
34+ protected MultiValueMode multiValueMode ;
35+ protected String [] names ;
36+ protected VS [] values ;
37+
38+ public static class NumericMultiValuesSource extends MultiValuesSource <ValuesSource .Numeric > {
39+
40+ public NumericMultiValuesSource (List <NamedValuesSourceSpec <Numeric >> valuesSources ,
41+ MultiValueMode multiValueMode ) {
42+ super (valuesSources , multiValueMode , new ValuesSource .Numeric [0 ]);
43+ }
44+
45+ public NumericDoubleValues getField (final int ordinal , LeafReaderContext ctx )
46+ throws IOException {
47+ if (ordinal > names .length ) {
48+ throw new IndexOutOfBoundsException (
49+ "ValuesSource array index " + ordinal + " out of bounds" );
50+ }
51+ return multiValueMode .select (values [ordinal ].doubleValues (ctx ), Double .NEGATIVE_INFINITY );
52+ }
53+ }
54+
55+ public static class BytesMultiValuesSource extends MultiValuesSource <ValuesSource .Bytes > {
56+
57+ public BytesMultiValuesSource (List <NamedValuesSourceSpec <ValuesSource .Bytes >> valuesSources ,
58+ MultiValueMode multiValueMode ) {
59+ super (valuesSources , multiValueMode , new ValuesSource .Bytes [0 ]);
60+ }
61+
62+ public Object getField (final int ordinal , LeafReaderContext ctx ) throws IOException {
63+ return values [ordinal ].bytesValues (ctx );
64+ }
65+ }
66+
67+ public static class GeoPointValuesSource extends MultiValuesSource <ValuesSource .GeoPoint > {
68+
69+ public GeoPointValuesSource (List <NamedValuesSourceSpec <ValuesSource .GeoPoint >> valuesSources ,
70+ MultiValueMode multiValueMode ) {
71+ super (valuesSources , multiValueMode , new ValuesSource .GeoPoint [0 ]);
72+ }
73+ }
74+
75+ private MultiValuesSource (List <? extends NamedValuesSourceSpec <VS >> valuesSources ,
76+ MultiValueMode multiValueMode , VS [] emptyArray ) {
77+ if (valuesSources != null ) {
78+ this .names = new String [valuesSources .size ()];
79+ List <VS > valuesList = new ArrayList <VS >(valuesSources .size ());
80+ int i = 0 ;
81+ for (NamedValuesSourceSpec <VS > spec : valuesSources ) {
82+ this .names [i ++] = spec .getName ();
83+ valuesList .add (spec .getValuesSource ());
84+ }
85+ this .values = valuesList .toArray (emptyArray );
86+ } else {
87+ this .names = new String [0 ];
88+ this .values = emptyArray ;
89+ }
90+ this .multiValueMode = multiValueMode ;
91+ }
92+
93+ public boolean needsScores () {
94+ boolean needsScores = false ;
95+ for (ValuesSource value : values ) {
96+ needsScores |= value .needsScores ();
97+ }
98+ return needsScores ;
99+ }
100+
101+ public String [] fieldNames () {
102+ return this .names ;
103+ }
104+ }
0 commit comments