Ejemplo n.º 1
0
  @Test
  public void testNumericDetectionDefault() throws Exception {
    String mapping =
        XContentFactory.jsonBuilder()
            .startObject()
            .startObject("type")
            .endObject()
            .endObject()
            .string();

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

    ParsedDocument doc =
        defaultMapper.parse(
            "type",
            "1",
            XContentFactory.jsonBuilder()
                .startObject()
                .field("s_long", "100")
                .field("s_double", "100.0")
                .endObject()
                .bytes());

    FieldMapper mapper = defaultMapper.mappers().smartNameFieldMapper("s_long");
    assertThat(mapper, instanceOf(StringFieldMapper.class));

    mapper = defaultMapper.mappers().smartNameFieldMapper("s_double");
    assertThat(mapper, instanceOf(StringFieldMapper.class));
  }
Ejemplo n.º 2
0
  @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));
    }
  }