コード例 #1
0
 public void testMissingType() throws Exception {
   String source =
       JsonXContent.contentBuilder()
           .startObject()
           .startObject("by_date")
           .startObject("date_histogram")
           .field("field", "timestamp")
           .field("interval", "month")
           .endObject()
           .startObject("aggs")
           .startObject("tag_count")
           // the aggregation type is missing
           // .startObject("cardinality")
           .field("field", "tag")
           // .endObject()
           .endObject()
           .endObject()
           .endObject()
           .endObject()
           .string();
   try {
     XContentParser parser = XContentFactory.xContent(source).createParser(source);
     QueryParseContext parseContext =
         new QueryParseContext(queriesRegistry, parser, parseFieldMatcher);
     assertSame(XContentParser.Token.START_OBJECT, parser.nextToken());
     aggParsers.parseAggregators(parseContext);
     fail();
   } catch (ParsingException e) {
     // All Good
   }
 }
コード例 #2
0
 public void testSameAggregationName() throws Exception {
   final String name = randomAsciiOfLengthBetween(1, 10);
   String source =
       JsonXContent.contentBuilder()
           .startObject()
           .startObject(name)
           .startObject("terms")
           .field("field", "a")
           .endObject()
           .endObject()
           .startObject(name)
           .startObject("terms")
           .field("field", "b")
           .endObject()
           .endObject()
           .endObject()
           .string();
   try {
     XContentParser parser = XContentFactory.xContent(source).createParser(source);
     QueryParseContext parseContext =
         new QueryParseContext(queriesRegistry, parser, parseFieldMatcher);
     assertSame(XContentParser.Token.START_OBJECT, parser.nextToken());
     aggParsers.parseAggregators(parseContext);
     fail();
   } catch (IllegalArgumentException e) {
     assertThat(
         e.toString(),
         containsString("Two sibling aggregations cannot have the same name: [" + name + "]"));
   }
 }
コード例 #3
0
 public void testTwoTypes() throws Exception {
   String source =
       JsonXContent.contentBuilder()
           .startObject()
           .startObject("in_stock")
           .startObject("filter")
           .startObject("range")
           .startObject("stock")
           .field("gt", 0)
           .endObject()
           .endObject()
           .endObject()
           .startObject("terms")
           .field("field", "stock")
           .endObject()
           .endObject()
           .endObject()
           .string();
   try {
     XContentParser parser = XContentFactory.xContent(source).createParser(source);
     QueryParseContext parseContext =
         new QueryParseContext(queriesRegistry, parser, parseFieldMatcher);
     assertSame(XContentParser.Token.START_OBJECT, parser.nextToken());
     aggParsers.parseAggregators(parseContext);
     fail();
   } catch (ParsingException e) {
     assertThat(
         e.toString(),
         containsString(
             "Found two aggregation type definitions in [in_stock]: [filter] and [terms]"));
   }
 }
コード例 #4
0
  public void testInvalidAggregationName() throws Exception {
    Matcher matcher = Pattern.compile("[^\\[\\]>]+").matcher("");
    String name;
    Random rand = random();
    int len = randomIntBetween(1, 5);
    char[] word = new char[len];
    while (true) {
      for (int i = 0; i < word.length; i++) {
        word[i] = (char) rand.nextInt(127);
      }
      name = String.valueOf(word);
      if (!matcher.reset(name).matches()) {
        break;
      }
    }

    String source =
        JsonXContent.contentBuilder()
            .startObject()
            .startObject(name)
            .startObject("filter")
            .startObject("range")
            .startObject("stock")
            .field("gt", 0)
            .endObject()
            .endObject()
            .endObject()
            .endObject()
            .endObject()
            .string();
    try {
      XContentParser parser = XContentFactory.xContent(source).createParser(source);
      QueryParseContext parseContext =
          new QueryParseContext(queriesRegistry, parser, parseFieldMatcher);
      assertSame(XContentParser.Token.START_OBJECT, parser.nextToken());
      aggParsers.parseAggregators(parseContext);
      fail();
    } catch (ParsingException e) {
      assertThat(e.toString(), containsString("Invalid aggregation name [" + name + "]"));
    }
  }
コード例 #5
0
 public void testTwoAggs() throws Exception {
   String source =
       JsonXContent.contentBuilder()
           .startObject()
           .startObject("by_date")
           .startObject("date_histogram")
           .field("field", "timestamp")
           .field("interval", "month")
           .endObject()
           .startObject("aggs")
           .startObject("tag_count")
           .startObject("cardinality")
           .field("field", "tag")
           .endObject()
           .endObject()
           .endObject()
           .startObject("aggs") // 2nd "aggs": illegal
           .startObject("tag_count2")
           .startObject("cardinality")
           .field("field", "tag")
           .endObject()
           .endObject()
           .endObject()
           .endObject()
           .endObject()
           .string();
   try {
     XContentParser parser = XContentFactory.xContent(source).createParser(source);
     QueryParseContext parseContext =
         new QueryParseContext(queriesRegistry, parser, parseFieldMatcher);
     assertSame(XContentParser.Token.START_OBJECT, parser.nextToken());
     aggParsers.parseAggregators(parseContext);
     fail();
   } catch (ParsingException e) {
     assertThat(
         e.toString(), containsString("Found two sub aggregation definitions under [by_date]"));
   }
 }