public void testUnsupportedFeatures() throws IOException {
    XContentBuilder mapping =
        XContentBuilder.builder(JsonXContent.jsonXContent)
            .startObject()
            .startObject("type")
            .startObject(FieldNamesFieldMapper.NAME)
            // by setting randomly index to no we also test the pre-1.3 behavior
            .field("index", randomFrom("no", "not_analyzed"))
            .field("store", randomFrom("no", "yes"))
            .endObject()
            .endObject()
            .endObject();

    try {
      assertAcked(
          prepareCreate("test")
              .setSettings(
                  Settings.builder()
                      .put(
                          "index.routing.allocation.exclude._name",
                          backwardsCluster().newNodePattern())
                      .put(indexSettings()))
              .addMapping("type", mapping));
    } catch (MapperParsingException ex) {
      assertThat(ex.getCause(), instanceOf(IllegalArgumentException.class));
      assertThat(
          ExceptionsHelper.detailedMessage(ex)
              .contains(
                  "type=_field_names is not supported on indices created before version 1.3.0"),
          equalTo(true));
    }
  }
Пример #2
0
  public void testIgnoreMalformed() throws Exception {
    String mapping =
        XContentFactory.jsonBuilder()
            .startObject()
            .startObject("type")
            .startObject("properties")
            .startObject("field")
            .field("type", "ip")
            .endObject()
            .endObject()
            .endObject()
            .endObject()
            .string();

    DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping));

    assertEquals(mapping, mapper.mappingSource().toString());

    ThrowingRunnable runnable =
        () ->
            mapper.parse(
                "test",
                "type",
                "1",
                XContentFactory.jsonBuilder()
                    .startObject()
                    .field("field", ":1")
                    .endObject()
                    .bytes());
    MapperParsingException e = expectThrows(MapperParsingException.class, runnable);
    assertThat(e.getCause().getMessage(), containsString("':1' is not an IP string literal"));

    mapping =
        XContentFactory.jsonBuilder()
            .startObject()
            .startObject("type")
            .startObject("properties")
            .startObject("field")
            .field("type", "ip")
            .field("ignore_malformed", true)
            .endObject()
            .endObject()
            .endObject()
            .endObject()
            .string();

    DocumentMapper mapper2 = parser.parse("type", new CompressedXContent(mapping));

    ParsedDocument doc =
        mapper2.parse(
            "test",
            "type",
            "1",
            XContentFactory.jsonBuilder().startObject().field("field", ":1").endObject().bytes());

    IndexableField[] fields = doc.rootDoc().getFields("field");
    assertEquals(0, fields.length);
  }
 public void testPercolatorFieldMapperUnMappedField() throws Exception {
   addQueryMapping();
   MapperParsingException exception =
       expectThrows(
           MapperParsingException.class,
           () -> {
             mapperService
                 .documentMapper(typeName)
                 .parse(
                     "test",
                     typeName,
                     "1",
                     XContentFactory.jsonBuilder()
                         .startObject()
                         .field(fieldName, termQuery("unmapped_field", "value"))
                         .endObject()
                         .bytes());
           });
   assertThat(exception.getCause(), instanceOf(QueryShardException.class));
   assertThat(
       exception.getCause().getMessage(),
       equalTo("No field mapping can be found for the field with name [unmapped_field]"));
 }
  @Test
  public void testPointsOnly() throws Exception {
    String mapping =
        XContentFactory.jsonBuilder()
            .startObject()
            .startObject("type1")
            .startObject("properties")
            .startObject("location")
            .field("type", "geo_shape")
            .field("tree", randomBoolean() ? "quadtree" : "geohash")
            .field("tree_levels", "6")
            .field("distance_error_pct", "0.01")
            .field("points_only", true)
            .endObject()
            .endObject()
            .endObject()
            .endObject()
            .string();

    assertAcked(prepareCreate("geo_points_only").addMapping("type1", mapping));
    ensureGreen();

    ShapeBuilder shape = RandomShapeGenerator.createShape(random());
    try {
      index(
          "geo_points_only",
          "type1",
          "1",
          jsonBuilder().startObject().field("location", shape).endObject());
    } catch (MapperParsingException e) {
      // RandomShapeGenerator created something other than a POINT type, verify the correct
      // exception is thrown
      assertThat(e.getCause().getMessage(), containsString("is configured for points only"));
      return;
    }

    refresh();
    // test that point was inserted
    SearchResponse response =
        client()
            .prepareSearch()
            .setQuery(geoIntersectionQuery("location", shape))
            .execute()
            .actionGet();

    assertEquals(1, response.getHits().getTotalHits());
  }
  @Test
  public void testIgnoreMalformedOption() throws Exception {
    String mapping =
        XContentFactory.jsonBuilder()
            .startObject()
            .startObject("type")
            .startObject("properties")
            .startObject("field1")
            .field("type", "integer")
            .field("ignore_malformed", true)
            .endObject()
            .startObject("field2")
            .field("type", "integer")
            .field("ignore_malformed", false)
            .endObject()
            .startObject("field3")
            .field("type", "integer")
            .endObject()
            .endObject()
            .endObject()
            .endObject()
            .string();

    DocumentMapper defaultMapper = MapperTestUtils.newParser().parse(mapping);

    ParsedDocument doc =
        defaultMapper.parse(
            "type",
            "1",
            XContentFactory.jsonBuilder()
                .startObject()
                .field("field1", "a")
                .field("field2", "1")
                .endObject()
                .bytes());
    assertThat(doc.rootDoc().getField("field1"), nullValue());
    assertThat(doc.rootDoc().getField("field2"), notNullValue());

    try {
      defaultMapper.parse(
          "type",
          "1",
          XContentFactory.jsonBuilder().startObject().field("field2", "a").endObject().bytes());
    } catch (MapperParsingException e) {
      assertThat(e.getCause(), instanceOf(NumberFormatException.class));
    }

    // Verify that the default is false
    try {
      defaultMapper.parse(
          "type",
          "1",
          XContentFactory.jsonBuilder().startObject().field("field3", "a").endObject().bytes());
    } catch (MapperParsingException e) {
      assertThat(e.getCause(), instanceOf(NumberFormatException.class));
    }

    // Unless the global ignore_malformed option is set to true
    Settings indexSettings = settingsBuilder().put("index.mapping.ignore_malformed", true).build();
    defaultMapper = MapperTestUtils.newParser(indexSettings).parse(mapping);
    doc =
        defaultMapper.parse(
            "type",
            "1",
            XContentFactory.jsonBuilder().startObject().field("field3", "a").endObject().bytes());
    assertThat(doc.rootDoc().getField("field3"), nullValue());

    // This should still throw an exception, since field2 is specifically set to
    // ignore_malformed=false
    try {
      defaultMapper.parse(
          "type",
          "1",
          XContentFactory.jsonBuilder().startObject().field("field2", "a").endObject().bytes());
    } catch (MapperParsingException e) {
      assertThat(e.getCause(), instanceOf(NumberFormatException.class));
    }
  }
  // percolator field can be nested under an object field, but only one query can be specified per
  // document
  public void testNestedPercolatorField() throws Exception {
    String typeName = "another_type";
    String percolatorMapper =
        XContentFactory.jsonBuilder()
            .startObject()
            .startObject(typeName)
            .startObject("_field_names")
            .field("enabled", false)
            .endObject() // makes testing easier
            .startObject("properties")
            .startObject("object_field")
            .field("type", "object")
            .startObject("properties")
            .startObject("query_field")
            .field("type", "percolator")
            .endObject()
            .endObject()
            .endObject()
            .endObject()
            .endObject()
            .endObject()
            .string();
    mapperService.merge(
        typeName,
        new CompressedXContent(percolatorMapper),
        MapperService.MergeReason.MAPPING_UPDATE,
        true);

    QueryBuilder queryBuilder = matchQuery("field", "value");
    ParsedDocument doc =
        mapperService
            .documentMapper(typeName)
            .parse(
                "test",
                typeName,
                "1",
                jsonBuilder()
                    .startObject()
                    .startObject("object_field")
                    .field("query_field", queryBuilder)
                    .endObject()
                    .endObject()
                    .bytes());
    assertThat(
        doc.rootDoc().getFields().size(),
        equalTo(8)); // also includes _uid (1), type (2), source (1)
    BytesRef queryBuilderAsBytes =
        doc.rootDoc().getField("object_field.query_field.query_builder_field").binaryValue();
    assertQueryBuilder(queryBuilderAsBytes, queryBuilder);

    doc =
        mapperService
            .documentMapper(typeName)
            .parse(
                "test",
                typeName,
                "1",
                jsonBuilder()
                    .startObject()
                    .startArray("object_field")
                    .startObject()
                    .field("query_field", queryBuilder)
                    .endObject()
                    .endArray()
                    .endObject()
                    .bytes());
    assertThat(
        doc.rootDoc().getFields().size(),
        equalTo(8)); // also includes _uid (1), type (2), source (1)
    queryBuilderAsBytes =
        doc.rootDoc().getField("object_field.query_field.query_builder_field").binaryValue();
    assertQueryBuilder(queryBuilderAsBytes, queryBuilder);

    MapperParsingException e =
        expectThrows(
            MapperParsingException.class,
            () -> {
              mapperService
                  .documentMapper(typeName)
                  .parse(
                      "test",
                      typeName,
                      "1",
                      jsonBuilder()
                          .startObject()
                          .startArray("object_field")
                          .startObject()
                          .field("query_field", queryBuilder)
                          .endObject()
                          .startObject()
                          .field("query_field", queryBuilder)
                          .endObject()
                          .endArray()
                          .endObject()
                          .bytes());
            });
    assertThat(e.getCause(), instanceOf(IllegalArgumentException.class));
    assertThat(
        e.getCause().getMessage(), equalTo("a document can only contain one percolator query"));
  }