@Test
  public void numeric_key_and_valuefields() {
    ContentIndexQuery query = new ContentIndexQuery("");

    final String facetDefinition =
        "<facets>\n"
            + "    <ranges name=\"myRangeFacet\">\n"
            + "            <range to=\"1\"/>\n"
            + "            <range from=\"1\" to=\"10\"/>\n"
            + "            <range from=\"10\" to=\"100\"/>\n"
            + "            <range from=\"100\" />\n"
            + "        <index>data.price</index>\n"
            + "        <value-index>data.myValue</value-index>\n"
            + "    </ranges>\n"
            + "</facets>\n";

    query.setFacets(facetDefinition);

    createAndIndexContent(1, new String[] {"0", "2"}, new String[] {"data.price", "data.myValue"});
    createAndIndexContent(
        2, new String[] {"0.99", "4"}, new String[] {"data.price", "data.myValue"});
    createAndIndexContent(3, new String[] {"1", "6"}, new String[] {"data.price", "data.myValue"});
    createAndIndexContent(
        4, new String[] {"1.0", "8"}, new String[] {"data.price", "data.myValue"});
    createAndIndexContent(
        5, new String[] {"10.0", "10"}, new String[] {"data.price", "data.myValue"});
    createAndIndexContent(
        6, new String[] {"100", "12"}, new String[] {"data.price", "data.myValue"});
    createAndIndexContent(
        7, new String[] {"101", "14"}, new String[] {"data.price", "data.myValue"});
    createAndIndexContent(
        8, new String[] {"1000", "16"}, new String[] {"data.price", "data.myValue"});

    flushIndex();

    final ContentResultSet result = contentIndexService.query(query);

    final Iterator<RangeFacetResultEntry> iterator = getResultIterator(result, 4);

    // NOTE: ES-range facets threats 'from' as 'from & including' and 'to' as 'to !including'
    assertNextEntry(iterator, 2L); // 0, 0.99
    assertNextEntry(iterator, 2L); // 1, 1.0
    assertNextEntry(iterator, 1L); // 10.0
    assertNextEntry(iterator, 3L); // 100, 101, 1000

    final String expectedXml =
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
            + "<content>\n"
            + "  <facets>\n"
            + "    <ranges name=\"myRangeFacet\">\n"
            + "      <range hits=\"2\" to=\"1.0\" min=\"2.0\" mean=\"3.0\" max=\"4.0\" sum=\"6.0\" />\n"
            + "      <range hits=\"2\" from=\"1.0\" to=\"10.0\" min=\"6.0\" mean=\"7.0\" max=\"8.0\" sum=\"14.0\" />\n"
            + "      <range hits=\"1\" from=\"10.0\" to=\"100.0\" min=\"10.0\" mean=\"10.0\" max=\"10.0\" sum=\"10.0\" />\n"
            + "      <range hits=\"3\" from=\"100.0\" min=\"12.0\" mean=\"14.0\" max=\"16.0\" sum=\"42.0\" />\n"
            + "    </ranges>\n"
            + "  </facets>\n"
            + "</content>\n";

    createAndCompareResultAsXml(result, expectedXml);
  }
  @Test
  public void key_and_value_fields() {
    createAndIndexContent(
        1, new String[] {"100", "1"}, new String[] {"data.myKey", "data.myValue"});
    createAndIndexContent(
        2, new String[] {"101", "2"}, new String[] {"data.myKey", "data.myValue"});
    createAndIndexContent(
        3, new String[] {"200", "3"}, new String[] {"data.myKey", "data.myValue"});
    createAndIndexContent(
        4, new String[] {"201", "4"}, new String[] {"data.myKey", "data.myValue"});
    createAndIndexContent(
        5, new String[] {"300", "5"}, new String[] {"data.myKey", "data.myValue"});
    createAndIndexContent(
        6, new String[] {"401", "6"}, new String[] {"data.myKey", "data.myValue"});
    createAndIndexContent(
        7, new String[] {"501", "7"}, new String[] {"data.myKey", "data.myValue"});
    createAndIndexContent(
        8, new String[] {"1000", "8"}, new String[] {"data.myKey", "data.myValue"});
    flushIndex();

    ContentIndexQuery query = new ContentIndexQuery("");
    final String facetDefinition =
        "<facets>\n"
            + "    <histogram name=\"myHistogramFacet\">\n"
            + "        <index>data.myKey</index>\n"
            + "        <value-index>data.myValue</value-index>\n"
            + "        <interval>100</interval>\n"
            + "    </histogram>\n"
            + "</facets>\n";

    query.setFacets(facetDefinition);
    final ContentResultSet result = contentIndexService.query(query);

    final FacetResultSet next = result.getFacetsResultSet().iterator().next();
    assertNotNull(next);
    assertTrue(next instanceof HistogramFacetResultSet);
    HistogramFacetResultSet histogramFacetResultSet = (HistogramFacetResultSet) next;
    assertEquals(6, histogramFacetResultSet.getResultEntries().size());

    String expectedXml =
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
            + "<content>\n"
            + "  <facets>\n"
            + "    <histogram name=\"myHistogramFacet\">\n"
            + "      <interval sum=\"3.0\" hits=\"2\" min=\"1.0\" mean=\"1.5\" max=\"2.0\">100</interval>\n"
            + "      <interval sum=\"7.0\" hits=\"2\" min=\"3.0\" mean=\"3.5\" max=\"4.0\">200</interval>\n"
            + "      <interval sum=\"5.0\" hits=\"1\" min=\"5.0\" mean=\"5.0\" max=\"5.0\">300</interval>\n"
            + "      <interval sum=\"6.0\" hits=\"1\" min=\"6.0\" mean=\"6.0\" max=\"6.0\">400</interval>\n"
            + "      <interval sum=\"7.0\" hits=\"1\" min=\"7.0\" mean=\"7.0\" max=\"7.0\">500</interval>\n"
            + "      <interval sum=\"8.0\" hits=\"1\" min=\"8.0\" mean=\"8.0\" max=\"8.0\">1000</interval>\n"
            + "    </histogram>\n"
            + "  </facets>\n"
            + "</content>";

    createAndCompareResultAsXml(result, expectedXml);
  }
  @Test
  public void simple_histogram() {
    createAndIndexContent(1, "100", "data.myValue");
    createAndIndexContent(2, "101", "data.myValue");
    createAndIndexContent(3, "200", "data.myValue");
    createAndIndexContent(4, "201", "data.myValue");
    createAndIndexContent(5, "300", "data.myValue");
    createAndIndexContent(6, "401", "data.myValue");
    createAndIndexContent(7, "501", "data.myValue");
    createAndIndexContent(8, "1000", "data.myValue");

    flushIndex();

    ContentIndexQuery query = new ContentIndexQuery("");
    final String facetDefinition =
        "<facets>\n"
            + "    <histogram name=\"myHistogramFacet\">\n"
            + "        <index>data.myValue</index>\n"
            + "        <interval>100</interval>\n"
            + "    </histogram>\n"
            + "</facets>\n";

    query.setFacets(facetDefinition);
    final ContentResultSet result = contentIndexService.query(query);

    final FacetResultSet next = result.getFacetsResultSet().iterator().next();
    assertNotNull(next);
    assertTrue(next instanceof HistogramFacetResultSet);
    HistogramFacetResultSet histogramFacetResultSet = (HistogramFacetResultSet) next;
    assertEquals(6, histogramFacetResultSet.getResultEntries().size());

    String expectedXml =
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
            + "<content>\n"
            + "  <facets>\n"
            + "    <histogram name=\"myHistogramFacet\">\n"
            + "      <interval hits=\"2\">100</interval>\n"
            + "      <interval hits=\"2\">200</interval>\n"
            + "      <interval hits=\"1\">300</interval>\n"
            + "      <interval hits=\"1\">400</interval>\n"
            + "      <interval hits=\"1\">500</interval>\n"
            + "      <interval hits=\"1\">1000</interval>\n"
            + "    </histogram>\n"
            + "  </facets>\n"
            + "</content>";

    createAndCompareResultAsXml(result, expectedXml);
  }