# Java Client ingesting Object Fields

**URL:** <https://discuss.elastic.co/t/java-client-ingesting-object-fields/45461>\
**Category:** Elasticsearch\
**Created:** [March 25, 2016, 4:15pm UTC](https://discuss.elastic.co/t/java-client-ingesting-object-fields/45461 "2016-03-25T16:15:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tom\_Mahy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tom_mahy/32/8695_2.png) [@Tom\_Mahy](https://discuss.elastic.co/u/Tom_Mahy)\
**Post date:** [March 25, 2016, 4:15pm UTC](https://discuss.elastic.co/t/java-client-ingesting-object-fields/45461/1 "2016-03-25T16:15:13Z")

</div>

Hello,

I am unable to ingest content in an object field with the java client 2.2.0.  
Please note that i have the same problem for nested objects but for String/long/arrays etc, it all works

Field creation

> ```
> mappingSource
> .startObject(ElasticLibrary.TYPE_CONTENT)
> .startObject("properties")
> .startObject("part")
> .field(TYPE, TYPE_STRING)
> .endObject()
> .startObject("name")
> .field(TYPE, TYPE_STRING)
> .endObject()
> .endObject()
> .endObject();
> 
> ```

The mapping is created fine.

This is how i try to ingest :

> contentBuilder = XContentFactory.jsonBuilder();  
> contentBuilder.startObject().prettyPrint();

> contentBuilder.field(ElasticLibrary.TYPE\_CONTENT, new Content(text));

> contentBuilder.endObject().prettyPrint().humanReadable(true);

> indexRequestBuilder.setSource(contentBuilder);  
> indexRequestBuilder.execute().actionGet();

My content object :

> public class Content implements Serializable{

> private String part;  
> private String name;

If i run this i will get the error

> org.elasticsearch.index.mapper.MapperParsingException: object mapping for [CONTENT] tried to parse field [CONTENT] as object, but found a concrete value

Does anyone know why i am getting this error ?

Thank you.

---

<div class="post-metadata">

**Author:** ![Glen\_Smith](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/glen_smith/32/111656_2.png) [@Glen\_Smith](https://discuss.elastic.co/u/Glen_Smith)\
**Post date:** [March 26, 2016, 8:28pm UTC](https://discuss.elastic.co/t/java-client-ingesting-object-fields/45461/2 "2016-03-26T20:28:40Z")

</div>

Looks to me like you're mapping a type, "content", with string properties "part" and "name", but then constructing an instance of Content by passing a single parameter called "text". Don't know what the single arg c'tor of your Content class does. Also don't know what the expected values for content.part and [content.name](http://content.name) would be, if all you have is "text".

I think what you want to do is something like

```auto
contentBuilder = XContentFactory.jsonBuilder()
  .startObject().prettyPrint()
    .startObject("content").field("part", part).field("name", name).endObject()
  .endObject().prettyPrint().humanReadable(true);
indexRequestBuilder.setSource(contentBuilder);
indexRequestBuilder.execute().actionGet();

```

but, again, I can't tell from where the values for "part" and "name" should come.

---

<div class="post-metadata">

**Author:** ![Tom\_Mahy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tom_mahy/32/8695_2.png) [@Tom\_Mahy](https://discuss.elastic.co/u/Tom_Mahy)\
**Post date:** [March 29, 2016, 8:00am UTC](https://discuss.elastic.co/t/java-client-ingesting-object-fields/45461/3 "2016-03-29T08:00:28Z")

</div>

If i remove the type specification i get the following error

> Caused by: MapperParsingException[No type specified for field [name]]

Here is a sample application that will reproduce the problem

```
private static String INDICE = "test";
private static String DOCUMENT_TYPE = "doc";
private static String FIELD_NAME = "content";
		
public static void main(String[] args) throws IOException {

	Builder builder = Settings.builder().put("cluster.name", "elastic");
	TransportClient transportClient = TransportClient.builder().settings(builder).build();
	transportClient.addTransportAddress(new InetSocketTransportAddress(Inet4Address.getByName("localhost"), 9300));
	Client client = (Client) transportClient;

	IndicesAdminClient indicesAdminClient = client.admin().indices();
	IndicesExistsRequest indicesExistsRequest = new IndicesExistsRequest(INDICE);
	IndicesExistsResponse indicesExistsResponse = indicesAdminClient.exists(indicesExistsRequest).actionGet();

	if (!indicesExistsResponse.isExists()) {

		CreateIndexRequestBuilder createIndexRequestBuilder = client.admin().indices()
				.prepareCreate(INDICE).addMapping(DOCUMENT_TYPE, getDefaultMapping());

		createIndexRequestBuilder.execute().actionGet();

	}

	IndexRequestBuilder indexRequestBuilder = client.prepareIndex(INDICE, DOCUMENT_TYPE, UUID.randomUUID().toString());
	XContentBuilder contentBuilder = null;

	contentBuilder = XContentFactory.jsonBuilder();
	contentBuilder.startObject().prettyPrint();

	contentBuilder.field(FIELD_NAME, new Content("This is my name", "This is the part"));

	contentBuilder.endObject().prettyPrint().humanReadable(true);

	indexRequestBuilder.setSource(contentBuilder);
	indexRequestBuilder.execute().actionGet();
}

private static XContentBuilder getDefaultMapping() throws IOException {

	XContentBuilder mappingSource = XContentFactory.jsonBuilder();

    mappingSource.startObject().startObject("properties");
    
    mappingSource
    .startObject(FIELD_NAME)
        .startObject("properties")
            .startObject("part")
                .field("type", "String")
            .endObject()
            .startObject("name")
                .field("type", "String")
            .endObject()
        .endObject()
    .endObject();

    mappingSource.endObject().endObject().prettyPrint();

    return mappingSource;

}

```

And the content object

```
public class Content {

String name;
String part;

public Content(String name, String part) {
	this.name = name;
	this.part = part;
}

public String getName() {
	return name;
}

public void setName(String name) {
	this.name = name;
}

public String getPart() {
	return part;
}

public void setPart(String part) {
	this.part = part;
}

}

```

---

<div class="post-metadata">

**Author:** ![Tom\_Mahy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tom_mahy/32/8695_2.png) [@Tom\_Mahy](https://discuss.elastic.co/u/Tom_Mahy)\
**Post date:** [March 29, 2016, 8:11am UTC](https://discuss.elastic.co/t/java-client-ingesting-object-fields/45461/4 "2016-03-29T08:11:26Z")

</div>

A quick look at the request shows that the content is

> index {[test][doc][e04cc5f2-9b62-4543-94e5-25d0e6dc54b8], source[{  
> "content" : "org.elastic.elasticnested.Content@191cb49e"  
> }]}

So i decided to try to convert to JSON before passing it to the builder, this is the request

> index {[test][doc][b6931a80-045f-4527-a0ca-b0bcf7d5925f], source[{  
> "content" : "{"name":"This is my name","part":"This is the part"}"  
> }]}

It still considered the content as a String.

---

<div class="post-metadata">

**Author:** ![Tom\_Mahy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tom_mahy/32/8695_2.png) [@Tom\_Mahy](https://discuss.elastic.co/u/Tom_Mahy)\
**Post date:** [March 29, 2016, 8:19am UTC](https://discuss.elastic.co/t/java-client-ingesting-object-fields/45461/5 "2016-03-29T08:19:42Z")

</div>

Found it by creating a map

```
Map<String, Object> map = new HashMap<>();
	
map.put("name", "This is my name");
map.put("part", "This is the part");
	
contentBuilder.field(FIELD_NAME, map);

```

Result

```
{
  "took": 10,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "test",
        "_type": "doc",
        "_id": "4b96664b-146a-45ad-99b6-e8e229d8de7c",
        "_score": 1,
        "_source": {
          "content": {
            "name": "This is my name",
            "part": "This is the part"
          }
        }
      }
    ]
  }
} 

```

Is there not a way to do this by passing the object directly ?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 29, 2016, 8:35am UTC](https://discuss.elastic.co/t/java-client-ingesting-object-fields/45461/6 "2016-03-29T08:35:50Z")

</div>

I personally use Jackson for this.

[https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-docs-index.html#java-docs-index-generate-beans](https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-docs-index.html#java-docs-index-generate-beans)

---

<div class="post-metadata">

**Author:** ![Glen\_Smith](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/glen_smith/32/111656_2.png) [@Glen\_Smith](https://discuss.elastic.co/u/Glen_Smith)\
**Post date:** [March 30, 2016, 3:04pm UTC](https://discuss.elastic.co/t/java-client-ingesting-object-fields/45461/7 "2016-03-30T15:04:36Z")

</div>

I don't understand why you can't just use the startObject helper as I showed.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 11:04pm UTC](https://discuss.elastic.co/t/java-client-ingesting-object-fields/45461/8 "2017-07-05T23:04:00Z")

</div>


