|
| 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.matrix.stats; |
| 20 | + |
| 21 | +import org.apache.lucene.document.Document; |
| 22 | +import org.apache.lucene.document.Field; |
| 23 | +import org.apache.lucene.document.SortedNumericDocValuesField; |
| 24 | +import org.apache.lucene.document.StringField; |
| 25 | +import org.apache.lucene.index.IndexReader; |
| 26 | +import org.apache.lucene.index.RandomIndexWriter; |
| 27 | +import org.apache.lucene.search.IndexSearcher; |
| 28 | +import org.apache.lucene.search.MatchAllDocsQuery; |
| 29 | +import org.apache.lucene.store.Directory; |
| 30 | +import org.apache.lucene.util.NumericUtils; |
| 31 | +import org.elasticsearch.index.mapper.MappedFieldType; |
| 32 | +import org.elasticsearch.index.mapper.NumberFieldMapper; |
| 33 | +import org.elasticsearch.search.aggregations.AggregatorTestCase; |
| 34 | + |
| 35 | +import java.util.Arrays; |
| 36 | +import java.util.Collections; |
| 37 | + |
| 38 | +public class MatrixStatsAggregatorTests extends AggregatorTestCase { |
| 39 | + |
| 40 | + public void testNoData() throws Exception { |
| 41 | + MappedFieldType ft = |
| 42 | + new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE); |
| 43 | + ft.setName("field"); |
| 44 | + |
| 45 | + try (Directory directory = newDirectory(); |
| 46 | + RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) { |
| 47 | + if (randomBoolean()) { |
| 48 | + indexWriter.addDocument(Collections.singleton(new StringField("another_field", "value", Field.Store.NO))); |
| 49 | + } |
| 50 | + try (IndexReader reader = indexWriter.getReader()) { |
| 51 | + IndexSearcher searcher = new IndexSearcher(reader); |
| 52 | + MatrixStatsAggregationBuilder aggBuilder = new MatrixStatsAggregationBuilder("my_agg") |
| 53 | + .fields(Collections.singletonList("field")); |
| 54 | + InternalMatrixStats stats = search(searcher, new MatchAllDocsQuery(), aggBuilder, ft); |
| 55 | + assertNull(stats.getStats()); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public void testTwoFields() throws Exception { |
| 61 | + String fieldA = "a"; |
| 62 | + MappedFieldType ftA = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE); |
| 63 | + ftA.setName(fieldA); |
| 64 | + String fieldB = "b"; |
| 65 | + MappedFieldType ftB = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE); |
| 66 | + ftB.setName(fieldB); |
| 67 | + |
| 68 | + try (Directory directory = newDirectory(); |
| 69 | + RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) { |
| 70 | + |
| 71 | + int numDocs = scaledRandomIntBetween(8192, 16384); |
| 72 | + Double[] fieldAValues = new Double[numDocs]; |
| 73 | + Double[] fieldBValues = new Double[numDocs]; |
| 74 | + for (int docId = 0; docId < numDocs; docId++) { |
| 75 | + Document document = new Document(); |
| 76 | + fieldAValues[docId] = randomDouble(); |
| 77 | + document.add(new SortedNumericDocValuesField(fieldA, NumericUtils.doubleToSortableLong(fieldAValues[docId]))); |
| 78 | + |
| 79 | + fieldBValues[docId] = randomDouble(); |
| 80 | + document.add(new SortedNumericDocValuesField(fieldB, NumericUtils.doubleToSortableLong(fieldBValues[docId]))); |
| 81 | + indexWriter.addDocument(document); |
| 82 | + } |
| 83 | + |
| 84 | + MultiPassStats multiPassStats = new MultiPassStats(fieldA, fieldB); |
| 85 | + multiPassStats.computeStats(Arrays.asList(fieldAValues), Arrays.asList(fieldBValues)); |
| 86 | + try (IndexReader reader = indexWriter.getReader()) { |
| 87 | + IndexSearcher searcher = new IndexSearcher(reader); |
| 88 | + MatrixStatsAggregationBuilder aggBuilder = new MatrixStatsAggregationBuilder("my_agg") |
| 89 | + .fields(Arrays.asList(fieldA, fieldB)); |
| 90 | + InternalMatrixStats stats = search(searcher, new MatchAllDocsQuery(), aggBuilder, ftA, ftB); |
| 91 | + multiPassStats.assertNearlyEqual(new MatrixStatsResults(stats.getStats())); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | +} |
0 commit comments