@Test
  public void testGeoHashValue() throws Exception {
    String mapping =
        XContentFactory.jsonBuilder()
            .startObject()
            .startObject("type")
            .startObject("properties")
            .startObject("point")
            .field("type", "geo_point")
            .field("lat_lon", true)
            .endObject()
            .endObject()
            .endObject()
            .endObject()
            .string();

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

    ParsedDocument doc =
        defaultMapper.parse(
            "type",
            "1",
            XContentFactory.jsonBuilder()
                .startObject()
                .field("point", GeoHashUtils.encode(1.2, 1.3))
                .endObject()
                .bytes());

    assertThat(doc.rootDoc().getField("point.lat"), notNullValue());
    assertThat(doc.rootDoc().getField("point.lon"), notNullValue());
    assertThat(doc.rootDoc().get("point"), notNullValue());
  }
  @Test
  public void testEnabled() throws Exception {
    String mapping =
        XContentFactory.jsonBuilder()
            .startObject()
            .startObject("type")
            .startObject("_timestamp")
            .field("enabled", "yes")
            .field("store", "yes")
            .endObject()
            .endObject()
            .endObject()
            .string();
    DocumentMapper docMapper = MapperTests.newParser().parse(mapping);
    byte[] source =
        XContentFactory.jsonBuilder()
            .startObject()
            .field("field", "value")
            .endObject()
            .copiedBytes();
    ParsedDocument doc =
        docMapper.parse(SourceToParse.source(source).type("type").id("1").timestamp(1));

    assertThat(doc.rootDoc().getFieldable("_timestamp").isStored(), equalTo(true));
    assertThat(doc.rootDoc().getFieldable("_timestamp").isIndexed(), equalTo(true));
    assertThat(doc.rootDoc().getFieldable("_timestamp").tokenStreamValue(), notNullValue());
  }
  @Test
  public void testLatLonInOneValueStored() throws Exception {
    String mapping =
        XContentFactory.jsonBuilder()
            .startObject()
            .startObject("type")
            .startObject("properties")
            .startObject("point")
            .field("type", "geo_point")
            .field("lat_lon", true)
            .field("store", "yes")
            .endObject()
            .endObject()
            .endObject()
            .endObject()
            .string();

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

    ParsedDocument doc =
        defaultMapper.parse(
            "type",
            "1",
            XContentFactory.jsonBuilder()
                .startObject()
                .field("point", "1.2,1.3")
                .endObject()
                .bytes());

    assertThat(doc.rootDoc().getField("point.lat"), notNullValue());
    assertThat(doc.rootDoc().getField("point.lat").numericValue().doubleValue(), equalTo(1.2));
    assertThat(doc.rootDoc().getField("point.lon"), notNullValue());
    assertThat(doc.rootDoc().getField("point.lon").numericValue().doubleValue(), equalTo(1.3));
    assertThat(doc.rootDoc().get("point"), equalTo("1.2,1.3"));
  }
  public void testIncludeInAll() throws Exception {
    String mapping =
        XContentFactory.jsonBuilder()
            .startObject()
            .startObject("type")
            .startObject("properties")
            .startObject("field")
            .field("type", "ip")
            .field("include_in_all", true)
            .endObject()
            .endObject()
            .endObject()
            .endObject()
            .string();

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

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

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

    IndexableField[] fields = doc.rootDoc().getFields("_all");
    assertEquals(1, fields.length);
    assertEquals("::1", fields[0].stringValue());

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

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

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

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

    fields = doc.rootDoc().getFields("_all");
    assertEquals(0, fields.length);
  }
  public void testPercolatorFieldMapper() throws Exception {
    addQueryMapping();
    QueryBuilder queryBuilder = termQuery("field", "value");
    ParsedDocument doc =
        mapperService
            .documentMapper(typeName)
            .parse(
                "test",
                typeName,
                "1",
                XContentFactory.jsonBuilder()
                    .startObject()
                    .field(fieldName, queryBuilder)
                    .endObject()
                    .bytes());

    assertThat(doc.rootDoc().getFields(fieldType.getExtractedTermsField()).length, equalTo(1));
    assertThat(
        doc.rootDoc().getFields(fieldType.getExtractedTermsField())[0].binaryValue().utf8ToString(),
        equalTo("field\0value"));
    assertThat(doc.rootDoc().getFields(fieldType.getQueryBuilderFieldName()).length, equalTo(1));
    assertThat(
        doc.rootDoc().getFields(fieldType.getExtractionResultFieldName()).length, equalTo(1));
    assertThat(
        doc.rootDoc().getFields(fieldType.getExtractionResultFieldName())[0].stringValue(),
        equalTo(EXTRACTION_COMPLETE));
    BytesRef qbSource =
        doc.rootDoc().getFields(fieldType.getQueryBuilderFieldName())[0].binaryValue();
    assertQueryBuilder(qbSource, queryBuilder);

    // add an query for which we don't extract terms from
    queryBuilder = rangeQuery("field").from("a").to("z");
    doc =
        mapperService
            .documentMapper(typeName)
            .parse(
                "test",
                typeName,
                "1",
                XContentFactory.jsonBuilder()
                    .startObject()
                    .field(fieldName, queryBuilder)
                    .endObject()
                    .bytes());
    assertThat(
        doc.rootDoc().getFields(fieldType.getExtractionResultFieldName()).length, equalTo(1));
    assertThat(
        doc.rootDoc().getFields(fieldType.getExtractionResultFieldName())[0].stringValue(),
        equalTo(EXTRACTION_FAILED));
    assertThat(doc.rootDoc().getFields(fieldType.getExtractedTermsField()).length, equalTo(0));
    assertThat(doc.rootDoc().getFields(fieldType.getQueryBuilderFieldName()).length, equalTo(1));
    qbSource = doc.rootDoc().getFields(fieldType.getQueryBuilderFieldName())[0].binaryValue();
    assertQueryBuilder(qbSource, queryBuilder);
  }
  // multiple percolator fields are allowed in the mapping, but only one field can be used at index
  // time.
  public void testMultiplePercolatorFields() 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("query_field1")
            .field("type", "percolator")
            .endObject()
            .startObject("query_field2")
            .field("type", "percolator")
            .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()
                    .field("query_field1", queryBuilder)
                    .field("query_field2", queryBuilder)
                    .endObject()
                    .bytes());
    assertThat(
        doc.rootDoc().getFields().size(),
        equalTo(11)); // also includes _uid (1), type (2), source (1)
    BytesRef queryBuilderAsBytes =
        doc.rootDoc().getField("query_field1.query_builder_field").binaryValue();
    assertQueryBuilder(queryBuilderAsBytes, queryBuilder);

    queryBuilderAsBytes = doc.rootDoc().getField("query_field2.query_builder_field").binaryValue();
    assertQueryBuilder(queryBuilderAsBytes, queryBuilder);
  }
  public void testJoinFieldSet() throws Exception {
    String parentMapping =
        XContentFactory.jsonBuilder()
            .startObject()
            .startObject("parent_type")
            .endObject()
            .endObject()
            .string();
    String childMapping =
        XContentFactory.jsonBuilder()
            .startObject()
            .startObject("child_type")
            .startObject("_parent")
            .field("type", "parent_type")
            .endObject()
            .endObject()
            .endObject()
            .string();
    IndexService indexService = createIndex("test");
    indexService
        .mapperService()
        .merge(
            "parent_type",
            new CompressedXContent(parentMapping),
            MergeReason.MAPPING_UPDATE,
            false);
    indexService
        .mapperService()
        .merge(
            "child_type", new CompressedXContent(childMapping), MergeReason.MAPPING_UPDATE, false);

    // Indexing parent doc:
    DocumentMapper parentDocMapper = indexService.mapperService().documentMapper("parent_type");
    ParsedDocument doc =
        parentDocMapper.parse(
            SourceToParse.source("test", "parent_type", "1122", new BytesArray("{}")));
    assertEquals(1, getNumberOfFieldWithParentPrefix(doc.rootDoc()));
    assertEquals("1122", doc.rootDoc().getBinaryValue("_parent#parent_type").utf8ToString());

    // Indexing child doc:
    DocumentMapper childDocMapper = indexService.mapperService().documentMapper("child_type");
    doc =
        childDocMapper.parse(
            SourceToParse.source("test", "child_type", "1", new BytesArray("{}")).parent("1122"));

    assertEquals(1, getNumberOfFieldWithParentPrefix(doc.rootDoc()));
    assertEquals("1122", doc.rootDoc().getBinaryValue("_parent#parent_type").utf8ToString());
  }
Example #8
0
  @Test
  public void testDynamicFalse() throws IOException {
    String mapping =
        XContentFactory.jsonBuilder()
            .startObject()
            .startObject("type")
            .field("dynamic", "false")
            .startObject("properties")
            .startObject("field1")
            .field("type", "string")
            .endObject()
            .endObject()
            .endObject()
            .endObject()
            .string();

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

    ParsedDocument doc =
        defaultMapper.parse(
            "type",
            "1",
            XContentFactory.jsonBuilder()
                .startObject()
                .field("field1", "value1")
                .field("field2", "value2")
                .bytes());

    assertThat(doc.rootDoc().get("field1"), equalTo("value1"));
    assertThat(doc.rootDoc().get("field2"), nullValue());
  }
  public void testNoDocValues() throws Exception {
    String mapping =
        XContentFactory.jsonBuilder()
            .startObject()
            .startObject("type")
            .startObject("properties")
            .startObject("field")
            .field("type", "ip")
            .field("doc_values", false)
            .endObject()
            .endObject()
            .endObject()
            .endObject()
            .string();

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

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

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

    IndexableField[] fields = doc.rootDoc().getFields("field");
    assertEquals(1, fields.length);
    IndexableField pointField = fields[0];
    assertEquals(1, pointField.fieldType().pointDimensionCount());
    assertEquals(
        new BytesRef(InetAddressPoint.encode(InetAddresses.forString("::1"))),
        pointField.binaryValue());
  }
  public void testDefaultDisabledIndexMapper() throws Exception {
    String mapping =
        XContentFactory.jsonBuilder()
            .startObject()
            .startObject("type")
            .endObject()
            .endObject()
            .string();
    DocumentMapper docMapper =
        createIndex("test").mapperService().documentMapperParser().parse(mapping);
    IndexFieldMapper indexMapper = docMapper.rootMapper(IndexFieldMapper.class);
    assertThat(indexMapper.enabled(), equalTo(false));

    ParsedDocument doc =
        docMapper.parse(
            "type",
            "1",
            XContentFactory.jsonBuilder()
                .startObject()
                .field("field", "value")
                .endObject()
                .bytes());

    assertThat(doc.rootDoc().get("_index"), nullValue());
    assertThat(doc.rootDoc().get("field"), equalTo("value"));
  }
  private Fields generateTermVectorsFromDoc(TermVectorRequest request, boolean doAllFields)
      throws IOException {
    // parse the document, at the moment we do update the mapping, just like percolate
    ParsedDocument parsedDocument =
        parseDocument(indexShard.shardId().getIndex(), request.type(), request.doc());

    // select the right fields and generate term vectors
    ParseContext.Document doc = parsedDocument.rootDoc();
    Collection<String> seenFields = new HashSet<>();
    Collection<GetField> getFields = new HashSet<>();
    for (IndexableField field : doc.getFields()) {
      FieldMapper fieldMapper = indexShard.mapperService().smartNameFieldMapper(field.name());
      if (seenFields.contains(field.name())) {
        continue;
      } else {
        seenFields.add(field.name());
      }
      if (!isValidField(fieldMapper)) {
        continue;
      }
      if (request.selectedFields() == null
          && !doAllFields
          && !fieldMapper.fieldType().storeTermVectors()) {
        continue;
      }
      if (request.selectedFields() != null && !request.selectedFields().contains(field.name())) {
        continue;
      }
      String[] values = doc.getValues(field.name());
      getFields.add(new GetField(field.name(), Arrays.asList((Object[]) values)));
    }
    return generateTermVectors(getFields, request.offsets(), request.perFieldAnalyzer());
  }
  @Override
  public void doTestNoDocValues(String type) throws Exception {
    String mapping =
        XContentFactory.jsonBuilder()
            .startObject()
            .startObject("type")
            .startObject("properties")
            .startObject("field")
            .field("type", type)
            .field("doc_values", false)
            .endObject()
            .endObject()
            .endObject()
            .endObject()
            .string();

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

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

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

    IndexableField[] fields = doc.rootDoc().getFields("field");
    assertEquals(1, fields.length);
    IndexableField pointField = fields[0];
    assertEquals(1, pointField.fieldType().pointDimensionCount());
    assertEquals(123, pointField.numericValue().doubleValue(), 0d);
  }
 public void testJoinFieldNotSet() throws Exception {
   String mapping =
       XContentFactory.jsonBuilder()
           .startObject()
           .startObject("type")
           .endObject()
           .endObject()
           .string();
   DocumentMapper docMapper =
       createIndex("test")
           .mapperService()
           .documentMapperParser()
           .parse("type", new CompressedXContent(mapping));
   ParsedDocument doc =
       docMapper.parse(
           SourceToParse.source(
               "test",
               "type",
               "1",
               XContentFactory.jsonBuilder()
                   .startObject()
                   .field("x_field", "x_value")
                   .endObject()
                   .bytes()));
   assertEquals(0, getNumberOfFieldWithParentPrefix(doc.rootDoc()));
 }
  public void testDisablingBackcompat() throws Exception {
    // before 1.5, disabling happened by setting index:no
    String mapping =
        XContentFactory.jsonBuilder()
            .startObject()
            .startObject("type")
            .startObject("_field_names")
            .field("index", "no")
            .endObject()
            .endObject()
            .endObject()
            .string();

    Settings indexSettings =
        ImmutableSettings.builder()
            .put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_1_4_2.id)
            .build();
    DocumentMapper docMapper =
        createIndex("test", indexSettings).mapperService().documentMapperParser().parse(mapping);
    FieldNamesFieldMapper fieldNamesMapper = docMapper.rootMapper(FieldNamesFieldMapper.class);
    assertFalse(fieldNamesMapper.enabled());

    ParsedDocument doc =
        docMapper.parse(
            "type",
            "1",
            XContentFactory.jsonBuilder()
                .startObject()
                .field("field", "value")
                .endObject()
                .bytes());

    assertNull(doc.rootDoc().get("_field_names"));
  }
  @Test
  public void testDynamicTrue() throws IOException {
    String mapping =
        XContentFactory.jsonBuilder()
            .startObject()
            .startObject("type")
            .field("dynamic", "true")
            .startObject("properties")
            .startObject("field1")
            .field("type", "string")
            .endObject()
            .endObject()
            .endObject()
            .endObject()
            .string();

    DocumentMapper defaultMapper =
        createIndex("test").mapperService().documentMapperParser().parse(mapping);

    ParsedDocument doc =
        defaultMapper.parse(
            "type",
            "1",
            XContentFactory.jsonBuilder()
                .startObject()
                .field("field1", "value1")
                .field("field2", "value2")
                .bytes());

    assertThat(doc.rootDoc().get("field1"), equalTo("value1"));
    assertThat(doc.rootDoc().get("field2"), equalTo("value2"));
  }
  public void testPercolatorFieldMapper_noQuery() throws Exception {
    addQueryMapping();
    ParsedDocument doc =
        mapperService
            .documentMapper(typeName)
            .parse(
                "test",
                typeName,
                "1",
                XContentFactory.jsonBuilder().startObject().endObject().bytes());
    assertThat(doc.rootDoc().getFields(fieldType.getQueryBuilderFieldName()).length, equalTo(0));

    try {
      mapperService
          .documentMapper(typeName)
          .parse(
              "test",
              typeName,
              "1",
              XContentFactory.jsonBuilder().startObject().nullField(fieldName).endObject().bytes());
    } catch (MapperParsingException e) {
      assertThat(
          e.getDetailedMessage(), containsString("query malformed, must start with start_object"));
    }
  }
  public void testDynamicFalse() throws IOException {
    String mapping =
        jsonBuilder()
            .startObject()
            .startObject("type")
            .field("dynamic", "false")
            .startObject("properties")
            .startObject("field1")
            .field("type", "text")
            .endObject()
            .endObject()
            .endObject()
            .endObject()
            .string();

    DocumentMapper defaultMapper =
        createIndex("test")
            .mapperService()
            .documentMapperParser()
            .parse("type", new CompressedXContent(mapping));

    ParsedDocument doc =
        defaultMapper.parse(
            "test",
            "type",
            "1",
            jsonBuilder()
                .startObject()
                .field("field1", "value1")
                .field("field2", "value2")
                .bytes());

    assertThat(doc.rootDoc().get("field1"), equalTo("value1"));
    assertThat(doc.rootDoc().get("field2"), nullValue());
  }
  public void testCompressEnabled() throws Exception {
    String mapping =
        XContentFactory.jsonBuilder()
            .startObject()
            .startObject("type")
            .startObject("_source")
            .field("compress", true)
            .endObject()
            .endObject()
            .endObject()
            .string();

    DocumentMapper documentMapper =
        createIndex("test", settings).mapperService().documentMapperParser().parse(mapping);

    ParsedDocument doc =
        documentMapper.parse(
            "test",
            "type",
            "1",
            XContentFactory.jsonBuilder()
                .startObject()
                .field("field1", "value1")
                .field("field2", "value2")
                .endObject()
                .bytes());

    BytesRef bytes = doc.rootDoc().getBinaryValue("_source");
    assertThat(CompressorFactory.isCompressed(new BytesArray(bytes)), equalTo(true));
  }
  public void testDisabled() throws Exception {
    String mapping =
        XContentFactory.jsonBuilder()
            .startObject()
            .startObject("type")
            .startObject("_field_names")
            .field("enabled", false)
            .endObject()
            .endObject()
            .endObject()
            .string();
    DocumentMapper docMapper =
        createIndex("test").mapperService().documentMapperParser().parse(mapping);
    FieldNamesFieldMapper fieldNamesMapper = docMapper.rootMapper(FieldNamesFieldMapper.class);
    assertFalse(fieldNamesMapper.enabled());

    ParsedDocument doc =
        docMapper.parse(
            "type",
            "1",
            XContentFactory.jsonBuilder()
                .startObject()
                .field("field", "value")
                .endObject()
                .bytes());

    assertNull(doc.rootDoc().get("_field_names"));
  }
  public void testNotIndexed() throws Exception {
    String mapping =
        XContentFactory.jsonBuilder()
            .startObject()
            .startObject("type")
            .startObject("properties")
            .startObject("field")
            .field("type", "ip")
            .field("index", false)
            .endObject()
            .endObject()
            .endObject()
            .endObject()
            .string();

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

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

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

    IndexableField[] fields = doc.rootDoc().getFields("field");
    assertEquals(1, fields.length);
    IndexableField dvField = fields[0];
    assertEquals(DocValuesType.SORTED_SET, dvField.fieldType().docValuesType());
  }
  public void testCustomSettingsBackcompat() throws Exception {
    String mapping =
        XContentFactory.jsonBuilder()
            .startObject()
            .startObject("type")
            .startObject("_index")
            .field("enabled", true)
            .field("store", "yes")
            .endObject()
            .endObject()
            .endObject()
            .string();
    Settings indexSettings =
        Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_1_4_2.id).build();
    DocumentMapper docMapper =
        createIndex("test", indexSettings).mapperService().documentMapperParser().parse(mapping);
    IndexFieldMapper indexMapper = docMapper.rootMapper(IndexFieldMapper.class);
    assertThat(indexMapper.enabled(), equalTo(true));
    assertThat(indexMapper.fieldType().stored(), equalTo(true));

    ParsedDocument doc =
        docMapper.parse(
            "type",
            "1",
            XContentFactory.jsonBuilder()
                .startObject()
                .field("field", "value")
                .endObject()
                .bytes());

    assertThat(doc.rootDoc().get("_index"), equalTo("test"));
    assertThat(doc.rootDoc().get("field"), equalTo("value"));
  }
  @Test
  public void testLonLatArrayArrayStored() throws Exception {
    String mapping =
        XContentFactory.jsonBuilder()
            .startObject()
            .startObject("type")
            .startObject("properties")
            .startObject("point")
            .field("type", "geo_point")
            .field("lat_lon", true)
            .field("store", "yes")
            .endObject()
            .endObject()
            .endObject()
            .endObject()
            .string();

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

    ParsedDocument doc =
        defaultMapper.parse(
            "type",
            "1",
            XContentFactory.jsonBuilder()
                .startObject()
                .startArray("point")
                .startArray()
                .value(1.3)
                .value(1.2)
                .endArray()
                .startArray()
                .value(1.5)
                .value(1.4)
                .endArray()
                .endArray()
                .endObject()
                .bytes());

    assertThat(doc.rootDoc().getFields("point.lat").length, equalTo(2));
    assertThat(doc.rootDoc().getFields("point.lon").length, equalTo(2));
    assertThat(doc.rootDoc().getFields("point.lat")[0].numericValue().doubleValue(), equalTo(1.2));
    assertThat(doc.rootDoc().getFields("point.lon")[0].numericValue().doubleValue(), equalTo(1.3));
    assertThat(doc.rootDoc().getFields("point")[0].stringValue(), equalTo("1.2,1.3"));
    assertThat(doc.rootDoc().getFields("point.lat")[1].numericValue().doubleValue(), equalTo(1.4));
    assertThat(doc.rootDoc().getFields("point.lon")[1].numericValue().doubleValue(), equalTo(1.5));
    assertThat(doc.rootDoc().getFields("point")[1].stringValue(), equalTo("1.4,1.5"));
  }
  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 testIncludeInObjectBackcompat() throws Exception {
    String mapping =
        jsonBuilder().startObject().startObject("type").endObject().endObject().string();
    Settings settings =
        Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_1_4_2.id).build();
    DocumentMapper docMapper =
        createIndex("test", settings).mapperService().documentMapperParser().parse(mapping);
    ParsedDocument doc =
        docMapper.parse(
            "test",
            "type",
            "1",
            XContentFactory.jsonBuilder().startObject().field("_all", "foo").endObject().bytes());

    assertNull(doc.rootDoc().get("_all"));
    AllField field = (AllField) doc.rootDoc().getField("_all");
    // the backcompat behavior is actually ignoring directly specifying _all
    assertFalse(field.getAllEntries().fields().iterator().hasNext());
  }
  @Test
  public void testSizeEnabled() throws Exception {
    String mapping =
        XContentFactory.jsonBuilder()
            .startObject()
            .startObject("type")
            .startObject("_size")
            .field("enabled", true)
            .endObject()
            .endObject()
            .endObject()
            .string();
    DocumentMapper docMapper = MapperTests.newParser().parse(mapping);

    BytesReference source =
        XContentFactory.jsonBuilder().startObject().field("field", "value").endObject().bytes();
    ParsedDocument doc = docMapper.parse(SourceToParse.source(source).type("type").id("1"));

    assertThat(doc.rootDoc().getField("_size").fieldType().stored(), equalTo(false));
    assertThat(
        doc.rootDoc().getField("_size").tokenStream(docMapper.indexAnalyzer()), notNullValue());
  }
  @Test
  public void testSimpleDisabled() throws Exception {
    String mapping =
        XContentFactory.jsonBuilder().startObject().startObject("type").endObject().string();
    DocumentMapper docMapper = MapperTests.newParser().parse(mapping);
    byte[] source =
        XContentFactory.jsonBuilder()
            .startObject()
            .field("field", "value")
            .endObject()
            .copiedBytes();
    ParsedDocument doc =
        docMapper.parse(SourceToParse.source(source).type("type").id("1").timestamp(1));

    assertThat(doc.rootDoc().getFieldable("_timestamp"), equalTo(null));
  }
  @Test
  public void testSizeNotSet() throws Exception {
    String mapping =
        XContentFactory.jsonBuilder()
            .startObject()
            .startObject("type")
            .endObject()
            .endObject()
            .string();
    DocumentMapper docMapper = MapperTests.newParser().parse(mapping);

    BytesReference source =
        XContentFactory.jsonBuilder().startObject().field("field", "value").endObject().bytes();
    ParsedDocument doc = docMapper.parse(SourceToParse.source(source).type("type").id("1"));

    assertThat(doc.rootDoc().getField("_size"), nullValue());
  }
  public void initialize(Engine.Searcher docSearcher, ParsedDocument parsedDocument) {
    this.docSearcher = docSearcher;

    IndexReader indexReader = docSearcher.reader();
    LeafReaderContext atomicReaderContext = indexReader.leaves().get(0);
    LeafSearchLookup leafLookup = lookup().getLeafSearchLookup(atomicReaderContext);
    leafLookup.setDocument(0);
    leafLookup.source().setSource(parsedDocument.source());

    Map<String, SearchHitField> fields = new HashMap<>();
    for (IndexableField field : parsedDocument.rootDoc().getFields()) {
      fields.put(field.name(), new InternalSearchHitField(field.name(), Collections.emptyList()));
    }
    hitContext()
        .reset(
            new InternalSearchHit(0, "unknown", new StringText(parsedDocument.type()), fields),
            atomicReaderContext,
            0,
            docSearcher.searcher());
  }
  @Test
  public void testIncludeExclude() throws Exception {
    String mapping =
        XContentFactory.jsonBuilder()
            .startObject()
            .startObject("type")
            .startObject("_source")
            .field("includes", new String[] {"path1*"})
            .endObject()
            .endObject()
            .endObject()
            .string();

    DocumentMapper documentMapper = MapperTests.newParser().parse(mapping);

    ParsedDocument doc =
        documentMapper.parse(
            "type",
            "1",
            XContentFactory.jsonBuilder()
                .startObject()
                .startObject("path1")
                .field("field1", "value1")
                .endObject()
                .startObject("path2")
                .field("field2", "value2")
                .endObject()
                .endObject()
                .copiedBytes());

    Fieldable sourceField = doc.rootDoc().getFieldable("_source");
    Map<String, Object> sourceAsMap =
        XContentFactory.xContent(XContentType.JSON)
            .createParser(
                sourceField.getBinaryValue(),
                sourceField.getBinaryOffset(),
                sourceField.getBinaryLength())
            .mapAndClose();
    assertThat(sourceAsMap.containsKey("path1"), equalTo(true));
    assertThat(sourceAsMap.containsKey("path2"), equalTo(false));
  }
 public void testQueryWithRewrite() throws Exception {
   addQueryMapping();
   client().prepareIndex("remote", "type", "1").setSource("field", "value").get();
   QueryBuilder queryBuilder =
       termsLookupQuery("field", new TermsLookup("remote", "type", "1", "field"));
   ParsedDocument doc =
       mapperService
           .documentMapper(typeName)
           .parse(
               "test",
               typeName,
               "1",
               XContentFactory.jsonBuilder()
                   .startObject()
                   .field(fieldName, queryBuilder)
                   .endObject()
                   .bytes());
   BytesRef qbSource =
       doc.rootDoc().getFields(fieldType.getQueryBuilderFieldName())[0].binaryValue();
   assertQueryBuilder(qbSource, queryBuilder.rewrite(indexService.newQueryShardContext()));
 }