diff --git a/src/main/java/org/elasticsearch/index/fielddata/plain/ByteArrayAtomicFieldData.java b/src/main/java/org/elasticsearch/index/fielddata/plain/ByteArrayAtomicFieldData.java index 1466e23174c..fb2b6a108e0 100644 --- a/src/main/java/org/elasticsearch/index/fielddata/plain/ByteArrayAtomicFieldData.java +++ b/src/main/java/org/elasticsearch/index/fielddata/plain/ByteArrayAtomicFieldData.java @@ -485,6 +485,8 @@ public abstract class ByteArrayAtomicFieldData implements AtomicNumericFieldData public void forEachValueInDoc(int docId, ValueInDocProc proc) { if (set.get(docId)) { proc.onValue(docId, (long) values[docId]); + } else { + proc.onMissing(docId); } } } @@ -549,6 +551,8 @@ public abstract class ByteArrayAtomicFieldData implements AtomicNumericFieldData public void forEachValueInDoc(int docId, ValueInDocProc proc) { if (set.get(docId)) { proc.onValue(docId, (double) values[docId]); + } else { + proc.onMissing(docId); } } } diff --git a/src/main/java/org/elasticsearch/index/fielddata/plain/DoubleArrayAtomicFieldData.java b/src/main/java/org/elasticsearch/index/fielddata/plain/DoubleArrayAtomicFieldData.java index 39ee5cdcfd8..73aa813e995 100644 --- a/src/main/java/org/elasticsearch/index/fielddata/plain/DoubleArrayAtomicFieldData.java +++ b/src/main/java/org/elasticsearch/index/fielddata/plain/DoubleArrayAtomicFieldData.java @@ -642,6 +642,8 @@ public abstract class DoubleArrayAtomicFieldData implements AtomicNumericFieldDa public void forEachValueInDoc(int docId, ValueInDocProc proc) { if (set.get(docId)) { proc.onValue(docId, values[docId]); + } else { + proc.onMissing(docId); } } } diff --git a/src/main/java/org/elasticsearch/index/fielddata/plain/FloatArrayAtomicFieldData.java b/src/main/java/org/elasticsearch/index/fielddata/plain/FloatArrayAtomicFieldData.java index f1a19a64a2c..7d04db0721a 100644 --- a/src/main/java/org/elasticsearch/index/fielddata/plain/FloatArrayAtomicFieldData.java +++ b/src/main/java/org/elasticsearch/index/fielddata/plain/FloatArrayAtomicFieldData.java @@ -484,6 +484,8 @@ public abstract class FloatArrayAtomicFieldData implements AtomicNumericFieldDat public void forEachValueInDoc(int docId, ValueInDocProc proc) { if (set.get(docId)) { proc.onValue(docId, (long) values[docId]); + } else { + proc.onMissing(docId); } } } @@ -548,6 +550,8 @@ public abstract class FloatArrayAtomicFieldData implements AtomicNumericFieldDat public void forEachValueInDoc(int docId, ValueInDocProc proc) { if (set.get(docId)) { proc.onValue(docId, (double) values[docId]); + } else { + proc.onMissing(docId); } } } diff --git a/src/main/java/org/elasticsearch/index/fielddata/plain/IntArrayAtomicFieldData.java b/src/main/java/org/elasticsearch/index/fielddata/plain/IntArrayAtomicFieldData.java index 86af04da19b..c90fa897a18 100644 --- a/src/main/java/org/elasticsearch/index/fielddata/plain/IntArrayAtomicFieldData.java +++ b/src/main/java/org/elasticsearch/index/fielddata/plain/IntArrayAtomicFieldData.java @@ -483,6 +483,8 @@ public abstract class IntArrayAtomicFieldData implements AtomicNumericFieldData public void forEachValueInDoc(int docId, ValueInDocProc proc) { if (set.get(docId)) { proc.onValue(docId, (long) values[docId]); + } else { + proc.onMissing(docId); } } } @@ -547,6 +549,8 @@ public abstract class IntArrayAtomicFieldData implements AtomicNumericFieldData public void forEachValueInDoc(int docId, ValueInDocProc proc) { if (set.get(docId)) { proc.onValue(docId, (double) values[docId]); + } else { + proc.onMissing(docId); } } } diff --git a/src/main/java/org/elasticsearch/index/fielddata/plain/LongArrayAtomicFieldData.java b/src/main/java/org/elasticsearch/index/fielddata/plain/LongArrayAtomicFieldData.java index b1a969b87af..94974d5c14e 100644 --- a/src/main/java/org/elasticsearch/index/fielddata/plain/LongArrayAtomicFieldData.java +++ b/src/main/java/org/elasticsearch/index/fielddata/plain/LongArrayAtomicFieldData.java @@ -639,6 +639,8 @@ public abstract class LongArrayAtomicFieldData implements AtomicNumericFieldData public void forEachValueInDoc(int docId, ValueInDocProc proc) { if (set.get(docId)) { proc.onValue(docId, values[docId]); + } else { + proc.onMissing(docId); } } } @@ -703,6 +705,8 @@ public abstract class LongArrayAtomicFieldData implements AtomicNumericFieldData public void forEachValueInDoc(int docId, ValueInDocProc proc) { if (set.get(docId)) { proc.onValue(docId, (double) values[docId]); + } else { + proc.onMissing(docId); } } } diff --git a/src/main/java/org/elasticsearch/index/fielddata/plain/ShortArrayAtomicFieldData.java b/src/main/java/org/elasticsearch/index/fielddata/plain/ShortArrayAtomicFieldData.java index f71be7285cc..209117c8dec 100644 --- a/src/main/java/org/elasticsearch/index/fielddata/plain/ShortArrayAtomicFieldData.java +++ b/src/main/java/org/elasticsearch/index/fielddata/plain/ShortArrayAtomicFieldData.java @@ -483,6 +483,8 @@ public abstract class ShortArrayAtomicFieldData implements AtomicNumericFieldDat public void forEachValueInDoc(int docId, ValueInDocProc proc) { if (set.get(docId)) { proc.onValue(docId, (long) values[docId]); + } else { + proc.onMissing(docId); } } } @@ -547,6 +549,8 @@ public abstract class ShortArrayAtomicFieldData implements AtomicNumericFieldDat public void forEachValueInDoc(int docId, ValueInDocProc proc) { if (set.get(docId)) { proc.onValue(docId, (double) values[docId]); + } else { + proc.onMissing(docId); } } } diff --git a/src/test/java/org/elasticsearch/test/integration/search/facet/SimpleFacetsTests.java b/src/test/java/org/elasticsearch/test/integration/search/facet/SimpleFacetsTests.java index f5f9e734dca..5b704179623 100644 --- a/src/test/java/org/elasticsearch/test/integration/search/facet/SimpleFacetsTests.java +++ b/src/test/java/org/elasticsearch/test/integration/search/facet/SimpleFacetsTests.java @@ -19,6 +19,7 @@ package org.elasticsearch.test.integration.search.facet; +import org.elasticsearch.ElasticSearchException; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchType; import org.elasticsearch.action.search.ShardSearchFailure; @@ -46,6 +47,7 @@ import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; +import java.io.IOException; import java.util.concurrent.TimeUnit; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; @@ -139,7 +141,104 @@ public class SimpleFacetsTests extends AbstractNodesTests { assertThat(facet.getEntries().get(1).getCount(), equalTo(1)); } } + + @Test + public void testFacetNumeric() throws ElasticSearchException, IOException { + // TODO we should test this with more complex queries + try { + client.admin().indices().prepareDelete("test").execute().actionGet(); + } catch (Exception e) { + // ignore + } + + client.admin().indices().prepareCreate("test") + .addMapping("type", jsonBuilder().startObject().startObject("type").startObject("properties") + .startObject("byte").field("type", "byte").endObject() + .startObject("short").field("type", "short").endObject() + .startObject("integer").field("type", "integer").endObject() + .startObject("long").field("type", "long").endObject() + .startObject("float").field("type", "float").endObject() + .startObject("double").field("type", "double").endObject() + .endObject().endObject().endObject()) + .execute().actionGet(); + client.admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().execute().actionGet(); + for (int i = 0; i < 100; i++) { + client.prepareIndex("test", "type", ""+i).setSource(jsonBuilder().startObject() + .field("name", ""+i) + .field("byte", i ) + .field("short", i + Byte.MAX_VALUE) + .field("integer", i + Short.MAX_VALUE) + .field("long", i + Integer.MAX_VALUE) + .field("float", (float)i) + .field("double", (double)i) + .endObject()).execute().actionGet(); + } + + for (int i = 0; i < 10; i++) { + client.prepareIndex("test", "type", ""+(i + 100)).setSource(jsonBuilder().startObject() + .field("foo", ""+i) + .endObject()).execute().actionGet(); + } + + client.admin().indices().prepareFlush().setRefresh(true).execute().actionGet(); + SearchResponse searchResponse = client.prepareSearch() + .setQuery(matchAllQuery()) + .addFacet(termsFacet("double").field("double").size(10)) + .addFacet(termsFacet("float").field("float").size(10)) + .addFacet(termsFacet("integer").field("integer").size(10)) + .addFacet(termsFacet("long").field("long").size(10)) + .addFacet(termsFacet("short").field("short").size(10)) + .addFacet(termsFacet("byte").field("byte").size(10)) + + .addFacet(termsFacet("termFacet").field("name").size(10)) + .execute().actionGet(); + + assertThat(searchResponse.getHits().getTotalHits(), equalTo(110l)); + TermsFacet facet = searchResponse.getFacets().facet("termFacet"); + assertThat(facet.getName(), equalTo("termFacet")); + assertThat(facet.getEntries().size(), equalTo(10)); + assertThat(facet.getTotalCount(), equalTo(100l)); + assertThat(facet.getOtherCount(), equalTo(90l)); + assertThat(facet.getMissingCount(), equalTo(10l)); + + facet = searchResponse.getFacets().facet("double"); + assertThat(facet.getName(), equalTo("double")); + assertThat(facet.getEntries().size(), equalTo(10)); + assertThat(facet.getTotalCount(), equalTo(100l)); + assertThat(facet.getOtherCount(), equalTo(90l)); + assertThat(facet.getMissingCount(), equalTo(10l)); + + facet = searchResponse.getFacets().facet("float"); + assertThat(facet.getName(), equalTo("float")); + assertThat(facet.getEntries().size(), equalTo(10)); + assertThat(facet.getTotalCount(), equalTo(100l)); + assertThat(facet.getOtherCount(), equalTo(90l)); + assertThat(facet.getMissingCount(), equalTo(10l)); + + facet = searchResponse.getFacets().facet("long"); + assertThat(facet.getName(), equalTo("long")); + assertThat(facet.getEntries().size(), equalTo(10)); + assertThat(facet.getTotalCount(), equalTo(100l)); + assertThat(facet.getOtherCount(), equalTo(90l)); + assertThat(facet.getMissingCount(), equalTo(10l)); + + facet = searchResponse.getFacets().facet("integer"); + assertThat(facet.getName(), equalTo("integer")); + assertThat(facet.getEntries().size(), equalTo(10)); + assertThat(facet.getTotalCount(), equalTo(100l)); + assertThat(facet.getOtherCount(), equalTo(90l)); + assertThat(facet.getMissingCount(), equalTo(10l)); + + facet = searchResponse.getFacets().facet("short"); + assertThat(facet.getName(), equalTo("short")); + assertThat(facet.getEntries().size(), equalTo(10)); + assertThat(facet.getTotalCount(), equalTo(100l)); + assertThat(facet.getOtherCount(), equalTo(90l)); + assertThat(facet.getMissingCount(), equalTo(10l)); + } + + @Test public void testSearchFilter() throws Exception { try {