You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by da...@apache.org on 2018/11/02 11:16:21 UTC
[12/56] lucene-solr:jira/gradle: Add :solr:contrib:analytics module
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/f0366b94/solr/contrib/analytics/src/test/java/org/apache/solr/analytics/value/CastingStringValueTest.java
----------------------------------------------------------------------
diff --git a/solr/contrib/analytics/src/test/java/org/apache/solr/analytics/value/CastingStringValueTest.java b/solr/contrib/analytics/src/test/java/org/apache/solr/analytics/value/CastingStringValueTest.java
new file mode 100644
index 0000000..3286a15
--- /dev/null
+++ b/solr/contrib/analytics/src/test/java/org/apache/solr/analytics/value/CastingStringValueTest.java
@@ -0,0 +1,120 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF 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.
+ */
+package org.apache.solr.analytics.value;
+
+import java.util.Arrays;
+import java.util.Iterator;
+
+import org.apache.solr.SolrTestCaseJ4;
+import org.apache.solr.analytics.value.AnalyticsValueStream.ExpressionType;
+import org.apache.solr.analytics.value.FillableTestValue.TestStringValue;
+import org.apache.solr.analytics.value.constant.ConstantStringValue;
+import org.junit.Test;
+
+public class CastingStringValueTest extends SolrTestCaseJ4 {
+
+ @Test
+ public void objectCastingTest() {
+ TestStringValue val = new TestStringValue();
+
+ assertTrue(val instanceof AnalyticsValue);
+ AnalyticsValue casted = (AnalyticsValue)val;
+
+ val.setValue("string 1").setExists(true);
+ assertEquals("string 1", casted.getObject());
+ assertTrue(casted.exists());
+
+ val.setValue("abc").setExists(true);
+ assertEquals("abc", casted.getObject());
+ assertTrue(casted.exists());
+ }
+
+ @Test
+ public void stringStreamCastingTest() {
+ TestStringValue val = new TestStringValue();
+
+ assertTrue(val instanceof StringValueStream);
+ StringValueStream casted = (StringValueStream)val;
+
+ // No values
+ val.setExists(false);
+ casted.streamStrings( value -> {
+ assertTrue("There should be no values to stream", false);
+ });
+
+ // Multiple Values
+ val.setValue("abcd").setExists(true);
+ Iterator<String> values = Arrays.asList("abcd").iterator();
+ casted.streamStrings( value -> {
+ assertTrue(values.hasNext());
+ assertEquals(values.next(), value);
+ });
+ assertFalse(values.hasNext());
+ }
+
+ @Test
+ public void objectStreamCastingTest() {
+ TestStringValue val = new TestStringValue();
+
+ assertTrue(val instanceof AnalyticsValueStream);
+ AnalyticsValueStream casted = (AnalyticsValueStream)val;
+
+ // No values
+ val.setExists(false);
+ casted.streamObjects( value -> {
+ assertTrue("There should be no values to stream", false);
+ });
+
+ // Multiple Values
+ val.setValue("abcd").setExists(true);
+ Iterator<Object> values = Arrays.<Object>asList("abcd").iterator();
+ casted.streamObjects( value -> {
+ assertTrue(values.hasNext());
+ assertEquals(values.next(), value);
+ });
+ assertFalse(values.hasNext());
+ }
+
+ @Test
+ public void constantConversionTest() {
+ TestStringValue val = new TestStringValue(ExpressionType.CONST);
+ val.setValue("asd23n23").setExists(true);
+ AnalyticsValueStream conv = val.convertToConstant();
+ assertTrue(conv instanceof ConstantStringValue);
+ assertEquals("asd23n23", ((ConstantStringValue)conv).getString());
+
+ val = new TestStringValue(ExpressionType.FIELD);
+ val.setValue("asd23n23").setExists(true);
+ conv = val.convertToConstant();
+ assertSame(val, conv);
+
+ val = new TestStringValue(ExpressionType.UNREDUCED_MAPPING);
+ val.setValue("asd23n23").setExists(true);
+ conv = val.convertToConstant();
+ assertSame(val, conv);
+
+ val = new TestStringValue(ExpressionType.REDUCTION);
+ val.setValue("asd23n23").setExists(true);
+ conv = val.convertToConstant();
+ assertSame(val, conv);
+
+ val = new TestStringValue(ExpressionType.REDUCED_MAPPING);
+ val.setValue("asd23n23").setExists(true);
+ conv = val.convertToConstant();
+ assertSame(val, conv);
+ }
+}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/f0366b94/solr/contrib/analytics/src/test/java/org/apache/solr/analytics/value/ConstantValueTest.java
----------------------------------------------------------------------
diff --git a/solr/contrib/analytics/src/test/java/org/apache/solr/analytics/value/ConstantValueTest.java b/solr/contrib/analytics/src/test/java/org/apache/solr/analytics/value/ConstantValueTest.java
new file mode 100644
index 0000000..81ccd1f
--- /dev/null
+++ b/solr/contrib/analytics/src/test/java/org/apache/solr/analytics/value/ConstantValueTest.java
@@ -0,0 +1,368 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF 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.
+ */
+package org.apache.solr.analytics.value;
+
+import java.time.Instant;
+import java.time.format.DateTimeParseException;
+import java.util.Date;
+
+import org.apache.solr.SolrTestCaseJ4;
+import org.apache.solr.analytics.value.FillableTestValue.TestIntValue;
+import org.apache.solr.analytics.value.constant.ConstantBooleanValue;
+import org.apache.solr.analytics.value.constant.ConstantDateValue;
+import org.apache.solr.analytics.value.constant.ConstantDoubleValue;
+import org.apache.solr.analytics.value.constant.ConstantFloatValue;
+import org.apache.solr.analytics.value.constant.ConstantIntValue;
+import org.apache.solr.analytics.value.constant.ConstantLongValue;
+import org.apache.solr.analytics.value.constant.ConstantStringValue;
+import org.apache.solr.analytics.value.constant.ConstantValue;
+import org.junit.Test;
+
+public class ConstantValueTest extends SolrTestCaseJ4 {
+
+
+ @Test
+ public void constantParsingTest() throws DateTimeParseException {
+ // Int
+ AnalyticsValueStream uncasted = ConstantValue.creatorFunction.apply("1234");
+ assertTrue(uncasted instanceof ConstantIntValue);
+ assertEquals(1234, ((ConstantIntValue)uncasted).getInt());
+
+ // Long
+ uncasted = ConstantValue.creatorFunction.apply("1234123412341234");
+ assertTrue(uncasted instanceof ConstantLongValue);
+ assertEquals(1234123412341234L, ((ConstantLongValue)uncasted).getLong());
+
+ // Floats cannot currently be implicitly created
+
+ // Double
+ uncasted = ConstantValue.creatorFunction.apply("12341234.12341234");
+ assertTrue(uncasted instanceof ConstantDoubleValue);
+ assertEquals(12341234.12341234, ((ConstantDoubleValue)uncasted).getDouble(), .000000001);
+
+ // String
+ uncasted = ConstantValue.creatorFunction.apply("'abcdef'");
+ assertTrue(uncasted instanceof ConstantStringValue);
+ assertEquals("abcdef", ((ConstantStringValue)uncasted).getString());
+
+ uncasted = ConstantValue.creatorFunction.apply("\"abcdef\"");
+ assertTrue(uncasted instanceof ConstantStringValue);
+ assertEquals("abcdef", ((ConstantStringValue)uncasted).getString());
+
+ // Date
+ uncasted = ConstantValue.creatorFunction.apply("1800-01-01T10:30:15.33Z");
+ assertTrue(uncasted instanceof ConstantDateValue);
+ assertEquals(Date.from(Instant.parse("1800-01-01T10:30:15.33Z")), ((ConstantDateValue)uncasted).getDate());
+ }
+
+ @Test
+ public void constantConversionTest() {
+ AnalyticsValueStream val = new ConstantBooleanValue(true);
+ assertSame(val, val.convertToConstant());
+
+ val = new ConstantIntValue(123);
+ assertSame(val, val.convertToConstant());
+
+ val = new ConstantLongValue(123L);
+ assertSame(val, val.convertToConstant());
+
+ val = new ConstantFloatValue(123F);
+ assertSame(val, val.convertToConstant());
+
+ val = new ConstantDoubleValue(123.0);
+ assertSame(val, val.convertToConstant());
+
+ val = new ConstantDateValue(123L);
+ assertSame(val, val.convertToConstant());
+
+ val = new ConstantStringValue("123");
+ assertSame(val, val.convertToConstant());
+ }
+
+ @Test
+ public void constantBooleanTest() {
+ ConstantBooleanValue val = new ConstantBooleanValue(true);
+
+ assertTrue(val.exists());
+ assertEquals(true, val.getBoolean());
+ assertEquals("true", val.getString());
+ assertEquals(Boolean.TRUE, val.getObject());
+
+ TestIntValue counter = new TestIntValue();
+ counter.setValue(0);
+ val.streamBooleans( value -> {
+ assertEquals(true, value);
+ assertEquals(0, counter.getInt());
+ counter.setValue(1);
+ });
+ counter.setValue(0);
+ val.streamStrings( value -> {
+ assertEquals("true", value);
+ assertEquals(0, counter.getInt());
+ counter.setValue(1);
+ });
+ counter.setValue(0);
+ val.streamObjects( value -> {
+ assertEquals(Boolean.TRUE, value);
+ assertEquals(0, counter.getInt());
+ counter.setValue(1);
+ });
+
+
+ val = new ConstantBooleanValue(false);
+
+ assertTrue(val.exists());
+ assertEquals(false, val.getBoolean());
+ assertEquals("false", val.getString());
+ assertEquals(Boolean.FALSE, val.getObject());
+
+ counter.setValue(0);
+ val.streamBooleans( value -> {
+ assertEquals(false, value);
+ assertEquals(0, counter.getInt());
+ counter.setValue(1);
+ });
+ counter.setValue(0);
+ val.streamStrings( value -> {
+ assertEquals("false", value);
+ assertEquals(0, counter.getInt());
+ counter.setValue(1);
+ });
+ counter.setValue(0);
+ val.streamObjects( value -> {
+ assertEquals(Boolean.FALSE, value);
+ assertEquals(0, counter.getInt());
+ counter.setValue(1);
+ });
+ }
+
+ @Test
+ public void constantIntTest() {
+ ConstantIntValue val = new ConstantIntValue(24);
+
+ assertTrue(val.exists());
+ assertEquals(24, val.getInt());
+ assertEquals(24L, val.getLong());
+ assertEquals(24F, val.getFloat(), .00001);
+ assertEquals(24.0, val.getDouble(), .00001);
+ assertEquals("24", val.getString());
+ assertEquals(24, val.getObject());
+
+ TestIntValue counter = new TestIntValue();
+ counter.setValue(0);
+ val.streamInts( value -> {
+ assertEquals(24, value);
+ assertEquals(0, counter.getInt());
+ counter.setValue(1);
+ });
+ counter.setValue(0);
+ val.streamLongs( value -> {
+ assertEquals(24L, value);
+ assertEquals(0, counter.getInt());
+ counter.setValue(1);
+ });
+ counter.setValue(0);
+ val.streamFloats( value -> {
+ assertEquals(24F, value, .00001);
+ assertEquals(0, counter.getInt());
+ counter.setValue(1);
+ });
+ counter.setValue(0);
+ val.streamDoubles( value -> {
+ assertEquals(24.0, value, .00001);
+ assertEquals(0, counter.getInt());
+ counter.setValue(1);
+ });
+ counter.setValue(0);
+ val.streamStrings( value -> {
+ assertEquals("24", value);
+ assertEquals(0, counter.getInt());
+ counter.setValue(1);
+ });
+ counter.setValue(0);
+ val.streamObjects( value -> {
+ assertEquals(24, value);
+ assertEquals(0, counter.getInt());
+ counter.setValue(1);
+ });
+ }
+
+ @Test
+ public void constantLongTest() {
+ ConstantLongValue val = new ConstantLongValue(24L);
+
+ assertTrue(val.exists());
+ assertEquals(24L, val.getLong());
+ assertEquals(24.0, val.getDouble(), .00001);
+ assertEquals("24", val.getString());
+ assertEquals(24L, val.getObject());
+
+ TestIntValue counter = new TestIntValue();
+ counter.setValue(0);
+ val.streamLongs( value -> {
+ assertEquals(24L, value);
+ assertEquals(0, counter.getInt());
+ counter.setValue(1);
+ });
+ counter.setValue(0);
+ val.streamDoubles( value -> {
+ assertEquals(24.0, value, .00001);
+ assertEquals(0, counter.getInt());
+ counter.setValue(1);
+ });
+ counter.setValue(0);
+ val.streamStrings( value -> {
+ assertEquals("24", value);
+ assertEquals(0, counter.getInt());
+ counter.setValue(1);
+ });
+ counter.setValue(0);
+ val.streamObjects( value -> {
+ assertEquals(24L, value);
+ assertEquals(0, counter.getInt());
+ counter.setValue(1);
+ });
+ }
+
+ @Test
+ public void constantFloatTest() {
+ ConstantFloatValue val = new ConstantFloatValue(24F);
+
+ assertTrue(val.exists());
+ assertEquals(24F, val.getFloat(), .00001);
+ assertEquals(24.0, val.getDouble(), .00001);
+ assertEquals("24.0", val.getString());
+ assertEquals(24F, val.getObject());
+
+ TestIntValue counter = new TestIntValue();
+ counter.setValue(0);
+ val.streamFloats( value -> {
+ assertEquals(24F, value, .00001);
+ assertEquals(0, counter.getInt());
+ counter.setValue(1);
+ });
+ counter.setValue(0);
+ val.streamDoubles( value -> {
+ assertEquals(24.0, value, .00001);
+ assertEquals(0, counter.getInt());
+ counter.setValue(1);
+ });
+ counter.setValue(0);
+ val.streamStrings( value -> {
+ assertEquals("24.0", value);
+ assertEquals(0, counter.getInt());
+ counter.setValue(1);
+ });
+ counter.setValue(0);
+ val.streamObjects( value -> {
+ assertEquals(24F, value);
+ assertEquals(0, counter.getInt());
+ counter.setValue(1);
+ });
+ }
+
+ @Test
+ public void constantDoubleTest() {
+ ConstantDoubleValue val = new ConstantDoubleValue(24.0);
+
+ assertTrue(val.exists());
+ assertEquals(24.0, val.getDouble(), .00001);
+ assertEquals("24.0", val.getString());
+ assertEquals(24.0, val.getObject());
+
+ TestIntValue counter = new TestIntValue();
+ counter.setValue(0);
+ val.streamDoubles( value -> {
+ assertEquals(24.0, value, .00001);
+ assertEquals(0, counter.getInt());
+ counter.setValue(1);
+ });
+ counter.setValue(0);
+ val.streamStrings( value -> {
+ assertEquals("24.0", value);
+ assertEquals(0, counter.getInt());
+ counter.setValue(1);
+ });
+ counter.setValue(0);
+ val.streamObjects( value -> {
+ assertEquals(24.0, value);
+ assertEquals(0, counter.getInt());
+ counter.setValue(1);
+ });
+ }
+
+ @Test
+ public void constantDateTest() throws DateTimeParseException {
+ Date date = Date.from(Instant.parse("1800-01-01T10:30:15Z"));
+ ConstantDateValue val = new ConstantDateValue(date.getTime());
+
+ assertTrue(val.exists());
+ assertEquals(date.getTime(), val.getLong());
+ assertEquals(date, val.getDate());
+ assertEquals("1800-01-01T10:30:15Z", val.getString());
+ assertEquals(date, val.getObject());
+
+ TestIntValue counter = new TestIntValue();
+ counter.setValue(0);
+ val.streamLongs( value -> {
+ assertEquals(date.getTime(), value);
+ assertEquals(0, counter.getInt());
+ counter.setValue(1);
+ });
+ counter.setValue(0);
+ val.streamDates( value -> {
+ assertEquals(date, value);
+ assertEquals(0, counter.getInt());
+ counter.setValue(1);
+ });
+ counter.setValue(0);
+ val.streamStrings( value -> {
+ assertEquals("1800-01-01T10:30:15Z", value);
+ assertEquals(0, counter.getInt());
+ counter.setValue(1);
+ });
+ counter.setValue(0);
+ val.streamObjects( value -> {
+ assertEquals(date, value);
+ assertEquals(0, counter.getInt());
+ counter.setValue(1);
+ });
+ }
+
+ @Test
+ public void constantStringTest() {
+ ConstantStringValue val = new ConstantStringValue("abcdef");
+
+ assertTrue(val.exists());
+ assertEquals("abcdef", val.getString());
+ assertEquals("abcdef", val.getObject());
+
+ TestIntValue counter = new TestIntValue();
+ counter.setValue(0);
+ val.streamStrings( value -> {
+ assertEquals("abcdef", value);
+ assertEquals(0, counter.getInt());
+ counter.setValue(1);
+ });
+ counter.setValue(0);
+ val.streamObjects( value -> {
+ assertEquals("abcdef", value);
+ assertEquals(0, counter.getInt());
+ counter.setValue(1);
+ });
+ }
+}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/f0366b94/solr/contrib/analytics/src/test/java/org/apache/solr/analytics/value/FillableTestValue.java
----------------------------------------------------------------------
diff --git a/solr/contrib/analytics/src/test/java/org/apache/solr/analytics/value/FillableTestValue.java b/solr/contrib/analytics/src/test/java/org/apache/solr/analytics/value/FillableTestValue.java
new file mode 100644
index 0000000..7facef9
--- /dev/null
+++ b/solr/contrib/analytics/src/test/java/org/apache/solr/analytics/value/FillableTestValue.java
@@ -0,0 +1,641 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF 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.
+ */
+package org.apache.solr.analytics.value;
+
+import java.time.Instant;
+import java.time.format.DateTimeParseException;
+import java.util.function.Consumer;
+import java.util.function.DoubleConsumer;
+import java.util.function.IntConsumer;
+import java.util.function.LongConsumer;
+
+import org.apache.solr.analytics.util.function.BooleanConsumer;
+import org.apache.solr.analytics.util.function.FloatConsumer;
+import org.apache.solr.analytics.value.AnalyticsValue.AbstractAnalyticsValue;
+import org.apache.solr.analytics.value.AnalyticsValueStream.AbstractAnalyticsValueStream;
+import org.apache.solr.analytics.value.BooleanValue.AbstractBooleanValue;
+import org.apache.solr.analytics.value.BooleanValueStream.AbstractBooleanValueStream;
+import org.apache.solr.analytics.value.DateValue.AbstractDateValue;
+import org.apache.solr.analytics.value.DateValueStream.AbstractDateValueStream;
+import org.apache.solr.analytics.value.DoubleValue.AbstractDoubleValue;
+import org.apache.solr.analytics.value.DoubleValueStream.AbstractDoubleValueStream;
+import org.apache.solr.analytics.value.FloatValue.AbstractFloatValue;
+import org.apache.solr.analytics.value.FloatValueStream.AbstractFloatValueStream;
+import org.apache.solr.analytics.value.IntValue.AbstractIntValue;
+import org.apache.solr.analytics.value.IntValueStream.AbstractIntValueStream;
+import org.apache.solr.analytics.value.LongValue.AbstractLongValue;
+import org.apache.solr.analytics.value.LongValueStream.AbstractLongValueStream;
+import org.apache.solr.analytics.value.StringValue.AbstractStringValue;
+import org.apache.solr.analytics.value.StringValueStream.AbstractStringValueStream;
+
+public class FillableTestValue {
+ public static class TestAnalyticsValue extends AbstractAnalyticsValue {
+ private final ExpressionType expressionType;
+
+ private Object value;
+ private boolean exists;
+
+ public TestAnalyticsValue() {
+ this(ExpressionType.CONST);
+ }
+
+ public TestAnalyticsValue(ExpressionType expressionType) {
+ this.expressionType = expressionType;
+ }
+
+ public TestAnalyticsValue setValue(Object value) {
+ this.value = value;
+ return this;
+ }
+
+ public TestAnalyticsValue setExists(boolean exists) {
+ this.exists = exists;
+ return this;
+ }
+
+ @Override
+ public Object getObject() {
+ return value;
+ }
+
+ @Override
+ public boolean exists() {
+ return exists;
+ }
+
+ @Override
+ public String getName() { return "test_analytics_value"; }
+
+ @Override
+ public String getExpressionStr() { return "test_analytics_value"; }
+
+ @Override
+ public ExpressionType getExpressionType() { return expressionType; }
+ }
+
+ public static class TestAnalyticsValueStream extends AbstractAnalyticsValueStream {
+ private final ExpressionType expressionType;
+
+ private Object[] values;
+
+ public TestAnalyticsValueStream() {
+ this(ExpressionType.CONST);
+ }
+
+ public TestAnalyticsValueStream(ExpressionType expressionType) {
+ this.expressionType = expressionType;
+ }
+
+ public TestAnalyticsValueStream setValues(Object... values) {
+ this.values = values;
+ return this;
+ }
+
+ @Override
+ public void streamObjects(Consumer<Object> cons) {
+ for (int i = 0; i < values.length; ++i) {
+ cons.accept(values[i]);
+ }
+ }
+
+ @Override
+ public String getName() { return "test_analytics_value_stream"; }
+
+ @Override
+ public String getExpressionStr() { return "test_analytics_value_stream"; }
+
+ @Override
+ public ExpressionType getExpressionType() { return expressionType; }
+ }
+
+ public static class TestIntValue extends AbstractIntValue {
+ private final ExpressionType expressionType;
+
+ private int value;
+ private boolean exists;
+
+ public TestIntValue() {
+ this(ExpressionType.CONST);
+ }
+
+ public TestIntValue(ExpressionType expressionType) {
+ this.expressionType = expressionType;
+ }
+
+ public TestIntValue setValue(int value) {
+ this.value = value;
+ return this;
+ }
+
+ public TestIntValue setExists(boolean exists) {
+ this.exists = exists;
+ return this;
+ }
+
+ @Override
+ public int getInt() {
+ return value;
+ }
+
+ @Override
+ public boolean exists() {
+ return exists;
+ }
+
+ @Override
+ public String getName() { return "test_int_value"; }
+
+ @Override
+ public String getExpressionStr() { return "test_int_value"; }
+
+ @Override
+ public ExpressionType getExpressionType() { return expressionType; }
+ }
+
+ public static class TestIntValueStream extends AbstractIntValueStream {
+ private int[] values;
+
+ public TestIntValueStream() {
+ this.values = new int[0];
+ }
+
+ public TestIntValueStream setValues(int... values) {
+ this.values = values;
+ return this;
+ }
+
+ @Override
+ public void streamInts(IntConsumer cons) {
+ for (int i = 0; i < values.length; ++i) {
+ cons.accept(values[i]);
+ }
+ }
+
+ @Override
+ public String getName() { return "test_int_value_stream"; }
+
+ @Override
+ public String getExpressionStr() { return "test_int_value_stream"; }
+
+ @Override
+ public ExpressionType getExpressionType() { return ExpressionType.UNREDUCED_MAPPING; }
+ }
+
+ public static class TestLongValue extends AbstractLongValue {
+ private final ExpressionType expressionType;
+
+ private long value;
+ private boolean exists;
+
+ public TestLongValue() {
+ this(ExpressionType.CONST);
+ }
+
+ public TestLongValue(ExpressionType expressionType) {
+ this.expressionType = expressionType;
+ }
+
+ public TestLongValue setValue(long value) {
+ this.value = value;
+ return this;
+ }
+
+ public TestLongValue setExists(boolean exists) {
+ this.exists = exists;
+ return this;
+ }
+
+ @Override
+ public long getLong() {
+ return value;
+ }
+
+ @Override
+ public boolean exists() {
+ return exists;
+ }
+
+ @Override
+ public String getName() { return "test_long_value"; }
+
+ @Override
+ public String getExpressionStr() { return "test_long_value"; }
+
+ @Override
+ public ExpressionType getExpressionType() { return expressionType; }
+ }
+
+ public static class TestLongValueStream extends AbstractLongValueStream {
+ private long[] values;
+
+ public TestLongValueStream() {
+ this.values = new long[0];
+ }
+
+ public TestLongValueStream setValues(long... values) {
+ this.values = values;
+ return this;
+ }
+
+ @Override
+ public void streamLongs(LongConsumer cons) {
+ for (int i = 0; i < values.length; ++i) {
+ cons.accept(values[i]);
+ }
+ }
+
+ @Override
+ public String getName() { return "test_long_value_stream"; }
+
+ @Override
+ public String getExpressionStr() { return "test_long_value_stream"; }
+
+ @Override
+ public ExpressionType getExpressionType() { return ExpressionType.UNREDUCED_MAPPING; }
+ }
+
+ public static class TestFloatValue extends AbstractFloatValue {
+ private final ExpressionType expressionType;
+
+ private float value;
+ private boolean exists;
+
+ public TestFloatValue() {
+ this(ExpressionType.CONST);
+ }
+
+ public TestFloatValue(ExpressionType expressionType) {
+ this.expressionType = expressionType;
+ }
+
+ public TestFloatValue setValue(float value) {
+ this.value = value;
+ return this;
+ }
+
+ public TestFloatValue setExists(boolean exists) {
+ this.exists = exists;
+ return this;
+ }
+
+ @Override
+ public float getFloat() {
+ return value;
+ }
+
+ @Override
+ public boolean exists() {
+ return exists;
+ }
+
+ @Override
+ public String getName() { return "test_float_value"; }
+
+ @Override
+ public String getExpressionStr() { return "test_float_value"; }
+
+ @Override
+ public ExpressionType getExpressionType() { return expressionType; }
+ }
+
+ public static class TestFloatValueStream extends AbstractFloatValueStream {
+ private float[] values;
+
+ public TestFloatValueStream() {
+ this.values = new float[0];
+ }
+
+ public TestFloatValueStream setValues(float... values) {
+ this.values = values;
+ return this;
+ }
+
+ @Override
+ public void streamFloats(FloatConsumer cons) {
+ for (int i = 0; i < values.length; ++i) {
+ cons.accept(values[i]);
+ }
+ }
+
+ @Override
+ public String getName() { return "test_float_value_stream"; }
+
+ @Override
+ public String getExpressionStr() { return "test_float_value_stream"; }
+
+ @Override
+ public ExpressionType getExpressionType() { return ExpressionType.UNREDUCED_MAPPING; }
+ }
+
+ public static class TestDoubleValue extends AbstractDoubleValue {
+ private final ExpressionType expressionType;
+
+ private double value;
+ private boolean exists;
+
+ public TestDoubleValue() {
+ this(ExpressionType.CONST);
+ }
+
+ public TestDoubleValue(ExpressionType expressionType) {
+ this.expressionType = expressionType;
+ }
+
+ public TestDoubleValue setValue(double value) {
+ this.value = value;
+ return this;
+ }
+
+ public TestDoubleValue setExists(boolean exists) {
+ this.exists = exists;
+ return this;
+ }
+
+ @Override
+ public double getDouble() {
+ return value;
+ }
+
+ @Override
+ public boolean exists() {
+ return exists;
+ }
+
+ @Override
+ public String getName() { return "test_double_value"; }
+
+ @Override
+ public String getExpressionStr() { return "test_double_value"; }
+
+ @Override
+ public ExpressionType getExpressionType() { return expressionType; }
+ }
+
+ public static class TestDoubleValueStream extends AbstractDoubleValueStream {
+ private double[] values;
+
+ public TestDoubleValueStream() {
+ this.values = new double[0];
+ }
+
+ public TestDoubleValueStream setValues(double... values) {
+ this.values = values;
+ return this;
+ }
+
+ @Override
+ public void streamDoubles(DoubleConsumer cons) {
+ for (int i = 0; i < values.length; ++i) {
+ cons.accept(values[i]);
+ }
+ }
+
+ @Override
+ public String getName() { return "test_double_value_stream"; }
+
+ @Override
+ public String getExpressionStr() { return "test_double_value_stream"; }
+
+ @Override
+ public ExpressionType getExpressionType() { return ExpressionType.UNREDUCED_MAPPING; }
+ }
+
+ public static class TestBooleanValue extends AbstractBooleanValue {
+ private final ExpressionType expressionType;
+
+ private boolean value;
+ private boolean exists;
+
+ public TestBooleanValue() {
+ this(ExpressionType.CONST);
+ }
+
+ public TestBooleanValue(ExpressionType expressionType) {
+ this.expressionType = expressionType;
+ }
+
+ public TestBooleanValue setValue(boolean value) {
+ this.value = value;
+ return this;
+ }
+
+ public TestBooleanValue setExists(boolean exists) {
+ this.exists = exists;
+ return this;
+ }
+
+ @Override
+ public boolean getBoolean() {
+ return value;
+ }
+
+ @Override
+ public boolean exists() {
+ return exists;
+ }
+
+ @Override
+ public String getName() { return "test_boolean_value"; }
+
+ @Override
+ public String getExpressionStr() { return "test_boolean_value"; }
+
+ @Override
+ public ExpressionType getExpressionType() { return expressionType; }
+ }
+
+ public static class TestBooleanValueStream extends AbstractBooleanValueStream {
+ private boolean[] values;
+
+ public TestBooleanValueStream() {
+ this.values = new boolean[0];
+ }
+
+ public TestBooleanValueStream setValues(boolean... values) {
+ this.values = values;
+ return this;
+ }
+
+ @Override
+ public void streamBooleans(BooleanConsumer cons) {
+ for (int i = 0; i < values.length; ++i) {
+ cons.accept(values[i]);
+ }
+ }
+
+ @Override
+ public String getName() { return "test_boolean_value_stream"; }
+
+ @Override
+ public String getExpressionStr() { return "test_boolean_value_stream"; }
+
+ @Override
+ public ExpressionType getExpressionType() { return ExpressionType.UNREDUCED_MAPPING; }
+ }
+
+ public static class TestDateValue extends AbstractDateValue {
+ private final ExpressionType expressionType;
+
+ private long value;
+ private boolean exists;
+
+ public TestDateValue() {
+ this(ExpressionType.CONST);
+ }
+
+ public TestDateValue(ExpressionType expressionType) {
+ this.expressionType = expressionType;
+ }
+
+ public TestDateValue setValue(String value) {
+ try {
+ this.value = Instant.parse(value).toEpochMilli();
+ } catch (DateTimeParseException e) {
+ this.value = 0;
+ }
+ return this;
+ }
+
+ public TestDateValue setExists(boolean exists) {
+ this.exists = exists;
+ return this;
+ }
+
+ @Override
+ public long getLong() {
+ return value;
+ }
+
+ @Override
+ public boolean exists() {
+ return exists;
+ }
+
+ @Override
+ public String getName() { return "test_date_value"; }
+
+ @Override
+ public String getExpressionStr() { return "test_date_value"; }
+
+ @Override
+ public ExpressionType getExpressionType() { return expressionType; }
+ }
+
+ public static class TestDateValueStream extends AbstractDateValueStream {
+ private String[] values;
+
+ public TestDateValueStream() {
+ this.values = new String[0];
+ }
+
+ public TestDateValueStream setValues(String... values) {
+ this.values = values;
+ return this;
+ }
+
+ @Override
+ public void streamLongs(LongConsumer cons) {
+ for (int i = 0; i < values.length; ++i) {
+ try {
+ cons.accept(Instant.parse(values[i]).toEpochMilli());
+ } catch (DateTimeParseException e) { }
+ }
+ }
+
+ @Override
+ public String getName() { return "test_date_value_stream"; }
+
+ @Override
+ public String getExpressionStr() { return "test_date_value_stream"; }
+
+ @Override
+ public ExpressionType getExpressionType() { return ExpressionType.UNREDUCED_MAPPING; }
+ }
+
+ public static class TestStringValue extends AbstractStringValue {
+ private final ExpressionType expressionType;
+
+ private String value;
+ private boolean exists;
+
+ public TestStringValue() {
+ this(ExpressionType.CONST);
+ }
+
+ public TestStringValue(ExpressionType expressionType) {
+ this.expressionType = expressionType;
+ }
+
+ public TestStringValue setValue(String value) {
+ this.value = value;
+ return this;
+ }
+
+ public TestStringValue setExists(boolean exists) {
+ this.exists = exists;
+ return this;
+ }
+
+ @Override
+ public String getString() {
+ return value;
+ }
+
+ @Override
+ public boolean exists() {
+ return exists;
+ }
+
+ @Override
+ public String getName() { return "test_string_value"; }
+
+ @Override
+ public String getExpressionStr() { return "test_string_value"; }
+
+ @Override
+ public ExpressionType getExpressionType() { return expressionType; }
+ }
+
+ public static class TestStringValueStream extends AbstractStringValueStream {
+ private String[] values;
+
+ public TestStringValueStream() {
+ this.values = new String[0];
+ }
+
+ public TestStringValueStream setValues(String... values) {
+ this.values = values;
+ return this;
+ }
+
+ @Override
+ public void streamStrings(Consumer<String> cons) {
+ for (int i = 0; i < values.length; ++i) {
+ cons.accept(values[i]);
+ }
+ }
+
+ @Override
+ public String getName() { return "test_string_value_stream"; }
+
+ @Override
+ public String getExpressionStr() { return "test_string_value_stream"; }
+
+ @Override
+ public ExpressionType getExpressionType() { return ExpressionType.UNREDUCED_MAPPING; }
+ }
+}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/f0366b94/solr/contrib/analytics/src/test/org/apache/solr/analytics/ExpressionFactoryTest.java
----------------------------------------------------------------------
diff --git a/solr/contrib/analytics/src/test/org/apache/solr/analytics/ExpressionFactoryTest.java b/solr/contrib/analytics/src/test/org/apache/solr/analytics/ExpressionFactoryTest.java
deleted file mode 100644
index b4f5b95..0000000
--- a/solr/contrib/analytics/src/test/org/apache/solr/analytics/ExpressionFactoryTest.java
+++ /dev/null
@@ -1,249 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF 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.
- */
-package org.apache.solr.analytics;
-
-import java.util.Arrays;
-import java.util.HashSet;
-import java.util.Set;
-
-import org.apache.solr.SolrTestCaseJ4;
-import org.apache.solr.analytics.function.ReductionCollectionManager;
-import org.apache.solr.analytics.value.constant.ConstantValue;
-import org.apache.solr.schema.IndexSchema;
-import org.junit.BeforeClass;
-import org.junit.Test;
-
-public class ExpressionFactoryTest extends SolrTestCaseJ4 {
-
- private static IndexSchema indexSchema;
-
- @BeforeClass
- public static void createSchemaAndFields() throws Exception {
- initCore("solrconfig-analytics.xml","schema-analytics.xml");
- assertU(adoc("id", "1",
- "int_i", "1",
- "int_im", "1",
- "long_l", "1",
- "long_lm", "1",
- "float_f", "1",
- "float_fm", "1",
- "double_d", "1",
- "double_dm", "1",
- "date_dt", "1800-12-31T23:59:59Z",
- "date_dtm", "1800-12-31T23:59:59Z",
- "string_s", "1",
- "string_sm", "1",
- "boolean_b", "true",
- "boolean_bm", "false"
- ));
- assertU(commit());
-
- indexSchema = h.getCore().getLatestSchema();
- }
-
- private ExpressionFactory getExpressionFactory() {
- ExpressionFactory fact = new ExpressionFactory(indexSchema);
- fact.startRequest();
- return fact;
- }
-
- @Test
- public void userDefinedVariableFunctionTest() {
- ExpressionFactory fact = getExpressionFactory();
-
- // Single parameter function
- fact.startRequest();
- fact.addUserDefinedVariableFunction("single_func(a)", "sum(add(a,double_d,float_f))");
- assertEquals("div(sum(add(int_i,double_d,float_f)),count(string_s))", fact.createExpression("div(single_func(int_i),count(string_s))").getExpressionStr());
-
- // Multi parameter function
- fact.startRequest();
- fact.addUserDefinedVariableFunction("multi_func(a,b,c)", "median(if(boolean_b,add(a,b),c))");
- assertEquals("div(median(if(boolean_b,add(int_i,double_d),float_f)),count(string_s))", fact.createExpression("div(multi_func(int_i,double_d,float_f),count(string_s))").getExpressionStr());
-
- // Function within function
- fact.startRequest();
- fact.addUserDefinedVariableFunction("inner_func(a,b)", "div(add(a,b),b)");
- fact.addUserDefinedVariableFunction("outer_func(a,b,c)", "pow(inner_func(a,b),c)");
- assertEquals("div(median(pow(div(add(int_i,double_d),double_d),float_f)),count(string_s))", fact.createExpression("div(median(outer_func(int_i,double_d,float_f)),count(string_s))").getExpressionStr());
-
- // Variable parameter function
- fact.startRequest();
- fact.addUserDefinedVariableFunction("var_func(a,b..)", "div(add(b),a)");
- assertEquals("unique(div(add(double_d,float_f),int_i))", fact.createExpression("unique(var_func(int_i,double_d,float_f))").getExpressionStr());
- assertEquals("unique(div(add(double_d,float_f,long_l),int_i))", fact.createExpression("unique(var_func(int_i,double_d,float_f,long_l))").getExpressionStr());
-
- // Variable parameter function with for-each
- fact.startRequest();
- fact.addUserDefinedVariableFunction("var_func_fe(a,b..)", "div(add(b:abs(_)),a)");
- assertEquals("unique(div(add(abs(double_d),abs(float_f)),int_i))", fact.createExpression("unique(var_func_fe(int_i,double_d,float_f))").getExpressionStr());
- assertEquals("unique(div(add(abs(double_d),abs(float_f),abs(long_l)),int_i))", fact.createExpression("unique(var_func_fe(int_i,double_d,float_f,long_l))").getExpressionStr());
- }
-
- @Test
- public void reuseFunctionsTest() {
- ExpressionFactory fact = getExpressionFactory();
-
- // Two ungrouped exactly the same expression
- fact.startRequest();
- assertTrue("The objects of the two mapping expressions are not the same.",
- fact.createExpression("pow(int_i,double_d)") == fact.createExpression("pow(int_i,double_d)"));
- assertTrue("The objects of the two reduced expressions are not the same.",
- fact.createExpression("unique(add(int_i,double_d))") == fact.createExpression("unique(add(int_i,double_d))"));
-
- // Two ungrouped different expressions
- fact.startRequest();
- assertFalse("The objects of the two mapping expressions are not the same.",
- fact.createExpression("pow(int_i,double_d)") == fact.createExpression("pow(int_i,float_f)"));
- assertFalse("The objects of the two reduced expressions are not the same.",
- fact.createExpression("unique(add(int_i,double_d))") == fact.createExpression("unique(add(int_i,float_f))"));
-
- // Grouped and ungrouped mapping expression
- fact.startRequest();
- Object ungrouped = fact.createExpression("pow(int_i,double_d)");
- fact.startGrouping();
- Object grouped = fact.createExpression("pow(int_i,double_d)");
- assertTrue("The objects of the two mapping expressions are not the same.", ungrouped == grouped);
-
- // Grouped and ungrouped diferent mapping expressions
- fact.startRequest();
- ungrouped = fact.createExpression("pow(int_i,double_d)");
- fact.startGrouping();
- grouped = fact.createExpression("pow(int_i,float_f)");
- assertFalse("The objects of the two mapping expressions are not the same.", ungrouped == grouped);
-
- // Grouped and ungrouped reduced expression.
- fact.startRequest();
- ungrouped = fact.createExpression("unique(add(int_i,double_d))");
- fact.startGrouping();
- grouped = fact.createExpression("unique(add(int_i,double_d))");
- assertTrue("The objects of the two mapping expressions are not the same.", ungrouped == grouped);
-
- // Grouped and ungrouped different reduced expressions.
- fact.startRequest();
- ungrouped = fact.createExpression("unique(add(int_i,double_d))");
- fact.startGrouping();
- grouped = fact.createExpression("unique(add(int_i,float_f))");
- assertFalse("The objects of the two mapping expressions are the same.", ungrouped == grouped);
- }
-
- @Test
- public void constantFunctionConversionTest() {
- ExpressionFactory fact = getExpressionFactory();
- fact.startRequest();
-
- assertTrue(fact.createExpression("add(1,2)") instanceof ConstantValue);
- assertTrue(fact.createExpression("add(1,2,2,3,4)") instanceof ConstantValue);
- assertTrue(fact.createExpression("add(1)") instanceof ConstantValue);
- assertTrue(fact.createExpression("concat(add(1,2), ' is a number')") instanceof ConstantValue);
-
- assertFalse(fact.createExpression("sum(add(1,2))") instanceof ConstantValue);
- assertFalse(fact.createExpression("sum(int_i)") instanceof ConstantValue);
- assertFalse(fact.createExpression("sub(1,long_l)") instanceof ConstantValue);
- }
-
- public void testReductionManager(ReductionCollectionManager manager, boolean hasExpressions, String... fields) {
- Set<String> usedFields = new HashSet<>(Arrays.asList(fields));
- manager.getUsedFields().forEach( field -> {
- assertTrue("Field '" + field.getName() + "' is either not unique or should not exist in the reductionManager.", usedFields.remove(field.getName()));
- });
- assertEquals(hasExpressions, manager.needsCollection());
- }
-
- @Test
- public void reductionManagerCreationTest() {
- ExpressionFactory fact = getExpressionFactory();
-
- // No expressions
- fact.startRequest();
- testReductionManager(fact.createReductionManager(false), false);
-
- // No fields
- fact.startRequest();
- fact.createExpression("sum(add(1,2))");
- testReductionManager(fact.createReductionManager(false), true);
-
- // Multiple expressions
- fact.startRequest();
- fact.createExpression("unique(add(int_i,float_f))");
- fact.createExpression("sum(add(int_i,double_d))");
- testReductionManager(fact.createReductionManager(false), true, "int_i", "float_f", "double_d");
- }
-
- @Test
- public void groupingReductionManagerCreationTest() {
- ExpressionFactory fact = getExpressionFactory();
-
- // No grouped expressions
- fact.startRequest();
- fact.createExpression("unique(add(int_i,float_f))");
- fact.createExpression("sum(add(int_i,double_d))");
- fact.startGrouping();
- testReductionManager(fact.createGroupingReductionManager(false), false);
-
- // No grouped fields
- fact.startRequest();
- fact.createExpression("unique(add(int_i,float_f))");
- fact.createExpression("sum(add(int_i,double_d))");
- fact.startGrouping();
- fact.createExpression("sum(add(1,2))");
- testReductionManager(fact.createGroupingReductionManager(false), true);
-
- // Single grouping, no ungrouped
- fact.startRequest();
- fact.startGrouping();
- fact.createExpression("unique(add(int_i,float_f))");
- fact.createExpression("sum(add(int_i,double_d))");
- testReductionManager(fact.createGroupingReductionManager(false), true, "int_i", "float_f", "double_d");
-
- // Single grouping, with ungrouped
- fact.startRequest();
- fact.createExpression("count(string_s)");
- fact.startGrouping();
- fact.createExpression("unique(add(int_i,float_f))");
- fact.createExpression("sum(add(int_i,double_d))");
- testReductionManager(fact.createGroupingReductionManager(false), true, "int_i", "float_f", "double_d");
-
- // Multiple groupings, no ungrouped
- fact.startRequest();
- fact.startGrouping();
- fact.createExpression("unique(add(int_i,float_f))");
- fact.createExpression("sum(add(int_i,double_d))");
- testReductionManager(fact.createGroupingReductionManager(false), true, "int_i", "float_f", "double_d");
- fact.startGrouping();
- testReductionManager(fact.createGroupingReductionManager(false), false);
- fact.startGrouping();
- fact.createExpression("unique(add(int_i,float_f))");
- fact.createExpression("ordinal(1,concat(string_s,'-extra'))");
- testReductionManager(fact.createGroupingReductionManager(false), true, "int_i", "float_f", "string_s");
-
- // Multiple groupings, with grouped
- fact.startRequest();
- fact.createExpression("count(string_s)");
- fact.createExpression("median(date_math(date_dt,'+1DAY'))");
- fact.startGrouping();
- fact.createExpression("unique(add(int_i,float_f))");
- fact.createExpression("sum(add(int_i,double_d))");
- testReductionManager(fact.createGroupingReductionManager(false), true, "int_i", "float_f", "double_d");
- fact.startGrouping();
- testReductionManager(fact.createGroupingReductionManager(false), false);
- fact.startGrouping();
- fact.createExpression("unique(add(int_i,float_f))");
- fact.createExpression("ordinal(1,concat(string_s,'-extra'))");
- testReductionManager(fact.createGroupingReductionManager(false), true, "int_i", "float_f", "string_s");
- }
-}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/f0366b94/solr/contrib/analytics/src/test/org/apache/solr/analytics/NoFacetTest.java
----------------------------------------------------------------------
diff --git a/solr/contrib/analytics/src/test/org/apache/solr/analytics/NoFacetTest.java b/solr/contrib/analytics/src/test/org/apache/solr/analytics/NoFacetTest.java
deleted file mode 100644
index 6a5f8c5..0000000
--- a/solr/contrib/analytics/src/test/org/apache/solr/analytics/NoFacetTest.java
+++ /dev/null
@@ -1,248 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF 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.
- */
-package org.apache.solr.analytics;
-
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import org.junit.BeforeClass;
-import org.junit.Test;
-
-public class NoFacetTest extends SolrAnalyticsTestCase {
-
- @BeforeClass
- public static void populate() throws Exception {
- for (int j = 0; j < NUM_LOOPS; ++j) {
- int i = j%INT;
- long l = j%LONG;
- float f = j%FLOAT;
- double d = j%DOUBLE;
- String dt = (1800+j%DATE) + "-12-31T23:59:59Z";
- String dtm = (1800+j%DATE + 10) + "-12-31T23:59:59Z";
- String s = "str" + (j%STRING);
- List<String> fields = new ArrayList<>();
- fields.add("id"); fields.add("1000"+j);
-
- if ( i != 0 ) {
- fields.add("int_i"); fields.add("" + i);
- fields.add("int_im"); fields.add("" + i);
- fields.add("int_im"); fields.add("" + (i+10));
- }
-
- if ( l != 0l ) {
- fields.add("long_l"); fields.add("" + l);
- fields.add("long_lm"); fields.add("" + l);
- fields.add("long_lm"); fields.add("" + (l+10));
- }
-
- if ( f != 0.0f ) {
- fields.add("float_f"); fields.add("" + f);
- fields.add("float_fm"); fields.add("" + f);
- fields.add("float_fm"); fields.add("" + (f+10));
- }
-
- if ( d != 0.0d ) {
- fields.add("double_d"); fields.add("" + d);
- fields.add("double_dm"); fields.add("" + d);
- fields.add("double_dm"); fields.add("" + (d+10));
- }
-
- if ( (j%DATE) != 0 ) {
- fields.add("date_dt"); fields.add(dt);
- fields.add("date_dtm"); fields.add(dt);
- fields.add("date_dtm"); fields.add(dtm);
- }
-
- if ( (j%STRING) != 0 ) {
- fields.add("string_s"); fields.add(s);
- fields.add("string_sm"); fields.add(s);
- fields.add("string_sm"); fields.add(s + "_second");
- }
-
- addDoc(fields);
- }
- commitDocs();
- }
-
- static public final int INT = 7;
- static public final int LONG = 2;
- static public final int FLOAT = 6;
- static public final int DOUBLE = 5;
- static public final int DATE = 3;
- static public final int STRING = 4;
- static public final int NUM_LOOPS = 20;
-
- @Test
- public void countTest() throws Exception {
- Map<String, ETP> expressions = new HashMap<>();
- expressions.put("single", new ETP("count(long_l)", 10L));
- expressions.put("multi", new ETP("count(string_sm)", 30L));
-
- testExpressions(expressions);
- }
-
- @Test
- public void docCountTest() throws Exception {
- Map<String, ETP> expressions = new HashMap<>();
- expressions.put("single", new ETP("doc_count(date_dt)", 13L));
- expressions.put("multi", new ETP("doc_count(float_fm)", 16L));
-
- testExpressions(expressions);
- }
-
- @Test
- public void missingTest() throws Exception {
- Map<String, ETP> expressions = new HashMap<>();
- expressions.put("single", new ETP("missing(string_s)", 5L));
- expressions.put("multi", new ETP("missing(date_dtm)", 7L));
-
- testExpressions(expressions);
- }
-
- @Test
- public void uniqueTest() throws Exception {
- Map<String, ETP> expressions = new HashMap<>();
- expressions.put("int", new ETP("unique(int_i)", 6L));
- expressions.put("longs", new ETP("unique(long_lm)", 2L));
- expressions.put("float", new ETP("unique(float_f)", 5L));
- expressions.put("doubles", new ETP("unique(double_dm)", 8L));
- expressions.put("dates", new ETP("unique(date_dt)", 2L));
- expressions.put("strings", new ETP("unique(string_sm)", 6L));
-
- testExpressions(expressions);
- }
-
- @Test
- public void minTest() throws Exception {
- Map<String, ETP> expressions = new HashMap<>();
- expressions.put("int", new ETP("min(int_i)", 1));
- expressions.put("longs", new ETP("min(long_lm)", 1L));
- expressions.put("float", new ETP("min(float_f)", 1.0F));
- expressions.put("doubles", new ETP("min(double_dm)", 1.0));
- expressions.put("dates", new ETP("min(date_dt)", "1801-12-31T23:59:59Z"));
- expressions.put("strings", new ETP("min(string_sm)", "str1"));
-
- testExpressions(expressions);
- }
-
- @Test
- public void maxTest() throws Exception {
- Map<String, ETP> expressions = new HashMap<>();
- expressions.put("int", new ETP("max(int_i)", 6));
- expressions.put("longs", new ETP("max(long_lm)", 11L));
- expressions.put("float", new ETP("max(float_f)", 5.0F));
- expressions.put("doubles", new ETP("max(double_dm)", 14.0));
- expressions.put("dates", new ETP("max(date_dt)", "1802-12-31T23:59:59Z"));
- expressions.put("strings", new ETP("max(string_sm)", "str3_second"));
-
- testExpressions(expressions);
- }
-
- @Test
- public void sumTest() throws Exception {
- Map<String, ETP> expressions = new HashMap<>();
- expressions.put("single", new ETP("sum(int_i)", 57.0));
- expressions.put("multi", new ETP("sum(long_lm)", 120.0));
-
- testExpressions(expressions);
- }
-
- @Test
- public void meanTest() throws Exception {
- Map<String, ETP> expressions = new HashMap<>();
- expressions.put("single", new ETP("mean(int_i)", 3.3529411764));
- expressions.put("multi", new ETP("mean(long_lm)", 6.0));
-
- testExpressions(expressions);
- }
-
- @Test
- public void weightedMeanTest() throws Exception {
- Map<String, ETP> expressions = new HashMap<>();
- expressions.put("single", new ETP("wmean(int_i, long_l)", 3.33333333333));
- expressions.put("multi", new ETP("wmean(double_d, float_f)", 2.470588235));
-
- testExpressions(expressions);
- }
-
- @Test
- public void sumOfSquaresTest() throws Exception {
- Map<String, ETP> expressions = new HashMap<>();
- expressions.put("single", new ETP("sumofsquares(int_i)", 237.0));
- expressions.put("multi", new ETP("sumofsquares(long_lm)", 1220.0));
-
- testExpressions(expressions);
- }
-
- @Test
- public void varianceTest() throws Exception {
- Map<String, ETP> expressions = new HashMap<>();
- expressions.put("single", new ETP("variance(int_i)", 2.6989619377162));
- expressions.put("multi", new ETP("variance(long_lm)", 25.0));
-
- testExpressions(expressions);
- }
-
- @Test
- public void standardDeviationTest() throws Exception {
- Map<String, ETP> expressions = new HashMap<>();
- expressions.put("single", new ETP("stddev(int_i)", 1.6428517698551));
- expressions.put("multi", new ETP("stddev(long_lm)", 5.0));
-
- testExpressions(expressions);
- }
-
- @Test
- public void medianTest() throws Exception {
- Map<String, ETP> expressions = new HashMap<>();
- expressions.put("int", new ETP("median(int_i)", 3.0));
- expressions.put("longs", new ETP("median(long_lm)", 6.0));
- expressions.put("float", new ETP("median(float_f)", 3.0));
- expressions.put("doubles", new ETP("median(double_dm)", 7.5));
- expressions.put("dates", new ETP("median(date_dt)", "1801-12-31T23:59:59Z"));
-
- testExpressions(expressions);
- }
-
- @Test
- public void percentileTest() throws Exception {
- Map<String, ETP> expressions = new HashMap<>();
- expressions.put("int", new ETP("percentile(20,int_i)", 2));
- expressions.put("longs", new ETP("percentile(80,long_lm)", 11L));
- expressions.put("float", new ETP("percentile(40,float_f)", 2.0F));
- expressions.put("doubles", new ETP("percentile(50,double_dm)", 11.0));
- expressions.put("dates", new ETP("percentile(0,date_dt)", "1801-12-31T23:59:59Z"));
- expressions.put("strings", new ETP("percentile(99.99,string_sm)", "str3_second"));
-
- testExpressions(expressions);
- }
-
- @Test
- public void ordinalTest() throws Exception {
- Map<String, ETP> expressions = new HashMap<>();
- expressions.put("int", new ETP("ordinal(15,int_i)", 5));
- expressions.put("longs", new ETP("ordinal(11,long_lm)", 11L));
- expressions.put("float", new ETP("ordinal(-5,float_f)", 4.0F));
- expressions.put("doubles", new ETP("ordinal(1,double_dm)", 1.0));
- expressions.put("dates", new ETP("ordinal(-1,date_dt)", "1802-12-31T23:59:59Z"));
- expressions.put("strings", new ETP("ordinal(6,string_sm)", "str1_second"));
-
- testExpressions(expressions);
- }
-}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/f0366b94/solr/contrib/analytics/src/test/org/apache/solr/analytics/OverallAnalyticsTest.java
----------------------------------------------------------------------
diff --git a/solr/contrib/analytics/src/test/org/apache/solr/analytics/OverallAnalyticsTest.java b/solr/contrib/analytics/src/test/org/apache/solr/analytics/OverallAnalyticsTest.java
deleted file mode 100644
index de6d6d2..0000000
--- a/solr/contrib/analytics/src/test/org/apache/solr/analytics/OverallAnalyticsTest.java
+++ /dev/null
@@ -1,320 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF 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.
- */
-package org.apache.solr.analytics;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import org.apache.solr.analytics.facet.SolrAnalyticsFacetTestCase;
-import org.junit.BeforeClass;
-import org.junit.Test;
-
-public class OverallAnalyticsTest extends SolrAnalyticsFacetTestCase {
-
- @BeforeClass
- public static void populate() throws Exception {
- for (int j = 0; j < NUM_LOOPS; ++j) {
- int i = j%INT;
- long l = j%LONG;
- float f = j%FLOAT;
- double d = j%DOUBLE;
- String dt = (1800+j%DATE) + "-12-31T23:59:59Z";
- String dtm = (1800+j%DATE + 10) + "-12-31T23:59:59Z";
- String s = "str" + (j%STRING);
- List<String> fields = new ArrayList<>();
- fields.add("id"); fields.add("1000"+j);
-
- if ( i != 0 ) {
- fields.add("int_i"); fields.add("" + i);
- fields.add("int_im"); fields.add("" + i);
- fields.add("int_im"); fields.add("" + (i+10));
- }
-
- if ( l != 0l ) {
- fields.add("long_l"); fields.add("" + l);
- fields.add("long_lm"); fields.add("" + l);
- fields.add("long_lm"); fields.add("" + (l+10));
- }
-
- if ( f != 0.0f ) {
- fields.add("float_f"); fields.add("" + f);
- fields.add("float_fm"); fields.add("" + f);
- fields.add("float_fm"); fields.add("" + (f+10));
- }
-
- if ( d != 0.0d ) {
- fields.add("double_d"); fields.add("" + d);
- fields.add("double_dm"); fields.add("" + d);
- fields.add("double_dm"); fields.add("" + (d+10));
- }
-
- if ( (j%DATE) != 0 ) {
- fields.add("date_dt"); fields.add(dt);
- fields.add("date_dtm"); fields.add(dt);
- fields.add("date_dtm"); fields.add(dtm);
- }
-
- if ( (j%STRING) != 0 ) {
- fields.add("string_s"); fields.add(s);
- fields.add("string_sm"); fields.add(s);
- fields.add("string_sm"); fields.add(s + "_second");
- }
-
- addDoc(fields);
- }
- commitDocs();
- }
-
- static public final int INT = 7;
- static public final int LONG = 2;
- static public final int FLOAT = 6;
- static public final int DOUBLE = 5;
- static public final int DATE = 3;
- static public final int STRING = 4;
- static public final int NUM_LOOPS = 20;
-
- @Test
- public void pivotFacetTest() throws Exception {
- String analyticsRequest = "{"
- + "\n 'expressions' : { "
- + "\n 'floor_mean_add' : 'floor(mean(add(int_i,long_lm)))', "
- + "\n 'consts' : 'max(mod(\\'_\\', string_s, 1800-01-01T11:59:59Z))' "
- + "\n }, "
- + "\n 'functions' : { "
- + "\n 'mod(a,b..)' : 'concat_sep(a, b:concat(_,\\'-con\\'))', "
- + "\n 'rep_0(num)' : 'fill_missing(num,0)' "
- + "\n }, "
- + "\n 'groupings': { "
- + "\n 'grouping0' : { "
- + "\n 'expressions' : { "
- + "\n 'mean' : 'mean(float_f)', "
- + "\n 'count2' : 'count(string_sm)' "
- + "\n }, "
- + "\n 'facets' : { "
- + "\n 'ranges' : { "
- + "\n 'type' : 'range', "
- + "\n 'field': 'int_im', "
- + "\n 'start': '2', "
- + "\n 'end' : '13', "
- + "\n 'gaps' : ['2', '5', '3'], "
- + "\n 'hardend' : false "
- + "\n } "
- + "\n } "
- + "\n }, "
- + "\n 'grouping1' : { "
- + "\n 'expressions' : { "
- + "\n 'mean' : 'mean(rep_0(int_i))', "
- + "\n 'count' : 'count(long_lm)' "
- + "\n }, "
- + "\n 'facets' : { "
- + "\n 'queries' : { "
- + "\n 'type' : 'query', "
- + "\n 'queries' : { "
- + "\n 'float < 3' : 'float_f:[* TO 3]', "
- + "\n 'string = 1, date = 2' : 'string_s:\"str1\" AND date_dt:\"1802-12-31T23:59:59Z\"' "
- + "\n } "
- + "\n }, "
- + "\n 'pivoting' : { "
- + "\n 'type' : 'pivot', "
- + "\n 'pivots' : [ "
- + "\n { "
- + "\n 'name' : 'strings', "
- + "\n 'expression' : 'string_sm', "
- + "\n 'sort' : { "
- + "\n 'criteria' : [ "
- + "\n { "
- + "\n 'type' : 'expression', "
- + "\n 'expression' : 'mean', "
- + "\n 'direction' : 'ascending' "
- + "\n }, "
- + "\n { "
- + "\n 'type' : 'facetvalue', "
- + "\n 'direction' : 'descending' "
- + "\n } "
- + "\n ], "
- + "\n 'limit' : 3, "
- + "\n 'offset' : 1 "
- + "\n } "
- + "\n }, "
- + "\n { "
- + "\n 'name' : 'date', "
- + "\n 'expression' : 'fill_missing(date_dt, \\'No Date\\')', "
- + "\n 'sort' : { "
- + "\n 'criteria' : [ "
- + "\n { "
- + "\n 'type' : 'expression', "
- + "\n 'expression' : 'count', "
- + "\n 'direction' : 'ascending' "
- + "\n }, "
- + "\n { "
- + "\n 'type' : 'expression', "
- + "\n 'expression' : 'mean', "
- + "\n 'direction' : 'ascending' "
- + "\n } "
- + "\n ], "
- + "\n 'limit' : 2 "
- + "\n } "
- + "\n } "
- + "\n ] "
- + "\n } "
- + "\n } "
- + "\n } "
- + "\n } "
- + "\n} ";
-
- String test = "== {"
- + "\n 'results' : { "
- + "\n 'floor_mean_add' : 9, "
- + "\n 'consts' : 'str3-con_1800-01-01T11:59:59Z-con' "
- + "\n }, "
- + "\n 'groupings' : { "
- + "\n 'grouping0' : { "
- + "\n 'ranges' : [ "
- + "\n { "
- + "\n 'value' : '[2 TO 4)', "
- + "\n 'results' : { "
- + "\n 'mean' : 3.5, "
- + "\n 'count2' : 10 "
- + "\n } "
- + "\n }, "
- + "\n { "
- + "\n 'value' : '[4 TO 9)', "
- + "\n 'results' : { "
- + "\n 'mean' : 3.2, "
- + "\n 'count2' : 12 "
- + "\n } "
- + "\n }, "
- + "\n { "
- + "\n 'value' : '[9 TO 12)', "
- + "\n 'results' : { "
- + "\n 'mean' : 2.0, "
- + "\n 'count2' : 4 "
- + "\n } "
- + "\n }, "
- + "\n { "
- + "\n 'value' : '[12 TO 15)', "
- + "\n 'results' : { "
- + "\n 'mean' : 3.75, "
- + "\n 'count2' : 14 "
- + "\n } "
- + "\n } "
- + "\n ] "
- + "\n }, "
- + "\n 'grouping1': { "
- + "\n 'queries' : [ "
- + "\n { "
- + "\n 'value' : 'float < 3', "
- + "\n 'results' : { "
- + "\n 'mean' : 2.1, "
- + "\n 'count' : 14 "
- + "\n } "
- + "\n }, "
- + "\n { "
- + "\n 'value' : 'string = 1, date = 2', "
- + "\n 'results' : { "
- + "\n 'mean' : 4.0, "
- + "\n 'count' : 4 "
- + "\n } "
- + "\n } "
- + "\n ], "
- + "\n 'pivoting':["
- + "\n { "
- + "\n 'pivot' : 'strings', "
- + "\n 'value' : 'str3', "
- + "\n 'results' : { "
- + "\n 'mean' : 2.6, "
- + "\n 'count' : 10 "
- + "\n }, "
- + "\n 'children' : [ "
- + "\n { "
- + "\n 'pivot' : 'date', "
- + "\n 'value' : '1802-12-31T23:59:59Z', "
- + "\n 'results' : { "
- + "\n 'mean' : 4.0, "
- + "\n 'count' : 2 "
- + "\n } "
- + "\n }, "
- + "\n { "
- + "\n 'pivot' : 'date', "
- + "\n 'value' : 'No Date', "
- + "\n 'results' : { "
- + "\n 'mean' : 2.0, "
- + "\n 'count' : 4 "
- + "\n } "
- + "\n } "
- + "\n ]"
- + "\n }, "
- + "\n { "
- + "\n 'pivot' : 'strings', "
- + "\n 'value' : 'str2_second', "
- + "\n 'results' : { "
- + "\n 'mean' : 3.0, "
- + "\n 'count' : 0 "
- + "\n }, "
- + "\n 'children' : [ "
- + "\n { "
- + "\n 'pivot' : 'date', "
- + "\n 'value' : '1802-12-31T23:59:59Z', "
- + "\n 'results' : { "
- + "\n 'mean' : 1.0, "
- + "\n 'count' : 0 "
- + "\n } "
- + "\n }, "
- + "\n { "
- + "\n 'pivot' : 'date', "
- + "\n 'value' : '1801-12-31T23:59:59Z', "
- + "\n 'results' : { "
- + "\n 'mean' : 3.0, "
- + "\n 'count' : 0 "
- + "\n } "
- + "\n } "
- + "\n ]"
- + "\n }, "
- + "\n { "
- + "\n 'pivot' : 'strings', "
- + "\n 'value' : 'str2', "
- + "\n 'results' : { "
- + "\n 'mean' : 3.0, "
- + "\n 'count' : 0 "
- + "\n }, "
- + "\n 'children' : [ "
- + "\n { "
- + "\n 'pivot' : 'date', "
- + "\n 'value' : '1802-12-31T23:59:59Z', "
- + "\n 'results' : { "
- + "\n 'mean' : 1.0, "
- + "\n 'count' : 0 "
- + "\n } "
- + "\n }, "
- + "\n { "
- + "\n 'pivot' : 'date', "
- + "\n 'value' : '1801-12-31T23:59:59Z', "
- + "\n 'results' : { "
- + "\n 'mean' : 3.0, "
- + "\n 'count' : 0 "
- + "\n } "
- + "\n } "
- + "\n ]"
- + "\n } "
- + "\n ]"
- + "\n }"
- + "\n }"
- + "\n}";
-
- testAnalytics(analyticsRequest, test);
- }
-}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/f0366b94/solr/contrib/analytics/src/test/org/apache/solr/analytics/SolrAnalyticsTestCase.java
----------------------------------------------------------------------
diff --git a/solr/contrib/analytics/src/test/org/apache/solr/analytics/SolrAnalyticsTestCase.java b/solr/contrib/analytics/src/test/org/apache/solr/analytics/SolrAnalyticsTestCase.java
deleted file mode 100644
index 993c1d3..0000000
--- a/solr/contrib/analytics/src/test/org/apache/solr/analytics/SolrAnalyticsTestCase.java
+++ /dev/null
@@ -1,349 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF 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.
- */
-package org.apache.solr.analytics;
-
-import java.io.IOException;
-import java.time.Instant;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Comparator;
-import java.util.Date;
-import java.util.List;
-import java.util.Map;
-import java.util.stream.Collectors;
-
-import org.apache.solr.JSONTestUtil;
-import org.apache.solr.client.solrj.SolrServerException;
-import org.apache.solr.client.solrj.request.CollectionAdminRequest;
-import org.apache.solr.client.solrj.request.QueryRequest;
-import org.apache.solr.client.solrj.request.UpdateRequest;
-import org.apache.solr.client.solrj.response.QueryResponse;
-import org.apache.solr.cloud.AbstractDistribZkTestBase;
-import org.apache.solr.cloud.SolrCloudTestCase;
-import org.apache.solr.common.params.ModifiableSolrParams;
-import org.apache.solr.common.params.SolrParams;
-import org.apache.solr.common.util.Utils;
-import org.junit.BeforeClass;
-
-public class SolrAnalyticsTestCase extends SolrCloudTestCase {
- private static final double DEFAULT_DELTA = .0000001;
-
- protected static final String COLLECTIONORALIAS = "collection1";
- protected static final int TIMEOUT = DEFAULT_TIMEOUT;
- protected static final String id = "id";
-
- private static UpdateRequest cloudReq;
-
- @BeforeClass
- public static void setupCollection() throws Exception {
- // Single-sharded core
- initCore("solrconfig-analytics.xml", "schema-analytics.xml");
- h.update("<delete><query>*:*</query></delete>");
-
- // Solr Cloud
- configureCluster(4)
- .addConfig("conf", configset("cloud-analytics"))
- .configure();
-
- CollectionAdminRequest.createCollection(COLLECTIONORALIAS, "conf", 2, 1).process(cluster.getSolrClient());
- AbstractDistribZkTestBase.waitForRecoveriesToFinish(COLLECTIONORALIAS, cluster.getSolrClient().getZkStateReader(),
- false, true, TIMEOUT);
-
- new UpdateRequest()
- .deleteByQuery("*:*")
- .commit(cluster.getSolrClient(), COLLECTIONORALIAS);
-
- cloudReq = new UpdateRequest();
- }
-
- protected static void cleanIndex() throws Exception {
- h.update("<delete><query>*:*</query></delete>");
-
- new UpdateRequest()
- .deleteByQuery("*:*")
- .commit(cluster.getSolrClient(), COLLECTIONORALIAS);
- }
-
- protected static void addDoc(List<String> fieldsAndValues) {
- assertU(adoc(fieldsAndValues.toArray(new String[0])));
- cloudReq.add(fieldsAndValues.toArray(new String[0]));
- }
-
- protected static void commitDocs() {
- assertU(commit());
- try {
- cloudReq.commit(cluster.getSolrClient(), COLLECTIONORALIAS);
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- cloudReq = new UpdateRequest();
- }
-
- private void testResults(SolrParams params, String analyticsRequest, String... tests) {
- String coreJson = queryCoreJson(params);
- Object cloudObj = queryCloudObject(params);
-
- for (String test : tests) {
- if (test == null || test.length()==0) continue;
- // Single-Sharded
- String err = null;
- try {
- err = JSONTestUtil.match(coreJson, test, DEFAULT_DELTA);
- } catch (Exception e) {
- err = e.getMessage();
- } finally {
- assertNull("query failed JSON validation. test= Single-Sharded Collection" +
- "\n error=" + err +
- "\n expected =" + test +
- "\n response = " + coreJson +
- "\n analyticsRequest = " + analyticsRequest, err);
- }
-
- // Cloud
- err = null;
- try {
- err = JSONTestUtil.matchObj(cloudObj, test, DEFAULT_DELTA);
- } catch (Exception e) {
- err = e.getMessage();
- } finally {
- assertNull("query failed JSON validation. test= Solr Cloud Collection" +
- "\n error=" + err +
- "\n expected =" + test +
- "\n response = " + Utils.toJSONString(cloudObj) +
- "\n analyticsRequest = " + analyticsRequest, err);
- }
- }
- }
-
- private String queryCoreJson(SolrParams params) {
- try {
- return JQ(req(params));
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
-
- private Object queryCloudObject(SolrParams params) {
- QueryResponse resp;
- try {
- cluster.waitForAllNodes(10000);
- QueryRequest qreq = new QueryRequest(params);
- resp = qreq.process(cluster.getSolrClient(), COLLECTIONORALIAS);
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- return convertDatesToStrings(resp.getResponse().asShallowMap());
- }
-
- protected void testAnalytics(String analyticsRequest, String... tests) throws IOException, InterruptedException, SolrServerException {
- ModifiableSolrParams params = new ModifiableSolrParams();
- params.set("q", "*:*");
- params.set("indent", "true");
- params.set("rows", "0");
- params.set("wt", "json");
-
- params.set("analytics", analyticsRequest);
-
- String[] revisedTests = Arrays.stream(tests).map( test -> "analytics_response/" + test).toArray( size -> new String[size]);
- testResults(params, analyticsRequest, revisedTests);
- }
-
- protected void testExpressions(Map<String, ETP> expressions) throws Exception {
- StringBuilder analyticsRequest = new StringBuilder("{ \"expressions\": {");
- String expressionsStr = expressions.entrySet()
- .stream()
- .map( entry -> '"' + entry.getKey() + "\":\"" + entry.getValue().expression + '"')
- .reduce((a,b) -> a + ',' + b)
- .orElseGet(() -> "");
- analyticsRequest.append(expressionsStr);
- analyticsRequest.append("}}");
-
- String results = expressions.entrySet()
- .stream()
- .map( entry -> '"' + entry.getKey() + "\":" + entry.getValue().expectedResultStr())
- .reduce((a,b) -> a + ',' + b)
- .orElseGet(() -> "");
-
- testAnalytics(analyticsRequest.toString(), "results=={"+results+", \"_UNORDERED_\":true}");
- }
-
- protected void testGrouping(String grouping,
- Map<String, String> expressions,
- Map<String, String> facets,
- Map<String, List<FVP>> results,
- boolean sortAscending) throws Exception {
- testGroupingSorted(grouping,
- expressions,
- facets,
- results,
- ", 'sort': { 'criteria' : [{'type': 'facetvalue', 'direction': '" + (sortAscending ? "ascending" : "descending") + "'}]}",
- (fvp1, fvp2) -> fvp1.facetValue.compareTo(fvp2.facetValue),
- sortAscending);
- }
-
- @SuppressWarnings("unchecked")
- protected void testGrouping(String grouping,
- Map<String, String> expressions,
- Map<String, String> facets,
- Map<String, List<FVP>> results,
- String sortExpression,
- boolean sortAscending) throws Exception {
- testGroupingSorted(grouping,
- expressions,
- facets,
- results,
- ", 'sort': { 'criteria' : [{'type': 'expression', 'expression': '" + sortExpression + "', 'direction': '" + (sortAscending ? "ascending" : "descending") + "'}]}",
- (fvp1, fvp2) -> fvp1.expectedResults.get(sortExpression).compareTo(fvp2.expectedResults.get(sortExpression)),
- sortAscending);
- }
-
- protected void testGrouping(String grouping,
- Map<String, String> expressions,
- Map<String, String> facets,
- Map<String, List<FVP>> results) throws Exception {
- testGroupingSorted(grouping,
- expressions,
- facets,
- results,
- "",
- (fvp1, fvp2) -> fvp1.compareTo(fvp2),
- true);
- }
-
- private void testGroupingSorted(String grouping,
- Map<String, String> expressions,
- Map<String, String> facets,
- Map<String, List<FVP>> results,
- String sort,
- Comparator<FVP> comparator,
- boolean sortAscending) throws Exception {
- StringBuilder analyticsRequest = new StringBuilder("{ \"groupings\": { \"" + grouping + "\" : { \"expressions\" : {");
- String expressionsStr = expressions.entrySet()
- .stream()
- .map( entry -> '"' + entry.getKey() + "\":\"" + entry.getValue() + '"')
- .collect(Collectors.joining(" , "));
- analyticsRequest.append(expressionsStr);
- analyticsRequest.append("}, \"facets\": {");
- String facetsStr = facets.entrySet()
- .stream()
- .map( entry -> '"' + entry.getKey() + "\":" + entry.getValue().replaceFirst("}\\s*$", sort) + "}")
- .collect(Collectors.joining(" , "));
- analyticsRequest.append(facetsStr);
- analyticsRequest.append("}}}}");
-
- String groupingResults = results.entrySet()
- .stream()
- .map( facet -> {
- String resultList = facet.getValue()
- .stream()
- .sorted(sortAscending ? comparator : comparator.reversed())
- .map( fvp -> fvp.toJsonResults() )
- .collect(Collectors.joining(" , "));
- return '"' + facet.getKey() + "\" : [ " + resultList + " ]";
- })
- .collect(Collectors.joining(" , "));
-
- testAnalytics(analyticsRequest.toString(), "groupings/" + grouping + "=={"+groupingResults+", \"_UNORDERED_\":true}");
- }
-
- private static String resultToJson(Object result) {
- if (result instanceof String) {
- return '"' + result.toString() + '"';
- }
- return result.toString();
- }
-
- /*
- * Expression Test Pair, contains the expression and the expected result
- */
- protected static class ETP {
- final String expression;
- final Object expectedResult;
-
- public ETP(String expression, Object expectedResult) {
- this.expression = expression;
- this.expectedResult = expectedResult;
- }
-
- public String expectedResultStr() {
- if (expectedResult instanceof String) {
- return '"' + expectedResult.toString() + '"';
- }
- return expectedResult.toString();
- }
- }
-
- /*
- * FacetValuePair, contains the expression and the expected result
- */
- @SuppressWarnings("rawtypes")
- protected static class FVP implements Comparable<FVP> {
- final private int order;
- final public String facetValue;
- final public Map<String, Comparable> expectedResults;
-
- public FVP(int order, String facetValue, Map<String, Comparable> expectedResults) {
- this.order = order;
- this.facetValue = facetValue;
- this.expectedResults = expectedResults;
- }
-
- public String toJsonResults() {
- String valueResults = expectedResults.entrySet()
- .stream()
- .map( result -> '"' + result.getKey() + "\":" + resultToJson(result.getValue()))
- .collect(Collectors.joining(" , "));
- return "{ \"value\" : \"" + facetValue + "\", \"results\": { " + valueResults + ", \"_UNORDERED_\":true } }";
- }
-
- @Override
- public int compareTo(FVP other) {
- return Integer.compare(order, other.order);
- }
- }
-
- /*
- * FacetValuePair, contains the expression and the expected result
- */
- @SuppressWarnings("rawtypes")
- protected static class PivotFVP extends FVP {
- final public String pivot;
- final public List<FVP> children;
-
- public PivotFVP(int order, String pivot, String facetValue, Map<String, Comparable> expectedResults) {
- super(order, facetValue, expectedResults);
- this.pivot = pivot;
- children = new ArrayList<>();
- }
-
- public void addChild(FVP child) {
- children.add(child);
- }
- }
-
- @SuppressWarnings("unchecked")
- protected static Object convertDatesToStrings(Object value) {
- if (value instanceof Date) {
- return Instant.ofEpochMilli(((Date)value).getTime()).toString();
- } else if (value instanceof Map) {
- ((Map<String,Object>)value).replaceAll( (key, obj) -> convertDatesToStrings(obj));
- } else if (value instanceof List) {
- ((List<Object>)value).replaceAll( obj -> convertDatesToStrings(obj));
- }
- return value;
- }
-}