# Mapping via java api and storing values in index

**URL:** <https://discuss.elastic.co/t/mapping-via-java-api-and-storing-values-in-index/3665>\
**Category:** Elasticsearch\
**Created:** [December 15, 2010, 4:12am UTC](https://discuss.elastic.co/t/mapping-via-java-api-and-storing-values-in-index/3665 "2010-12-15T04:12:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cstrzadala](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cstrzadala/32/8211_2.png) [@cstrzadala](https://discuss.elastic.co/u/cstrzadala)\
**Post date:** [December 15, 2010, 4:12am UTC](https://discuss.elastic.co/t/mapping-via-java-api-and-storing-values-in-index/3665/1 "2010-12-15T04:12:12Z")

</div>

Hi all,

New to Elastic Search and just having some issues getting my head  
around mapping via the Java API. I've got the following unit test  
below in which I'm trying to map a simple media object with 2 fields  
(title and description).

Index

- mediaindex1

type

- object

id

- 1

I'm accessing one instance of ES running locally but outside the JVM.  
I see that I'm able to index the doc and if I query ES via the rest  
API:

curl -XGET '[http://localhost:9200/mediaindex1/object/1](http://localhost:9200/mediaindex1/object/1)'

I get the following JSON response:

{"\_index":"mediaindex1","\_type":"object","\_id":"1", "\_source" :  
{"title":"harry potters songs of wackyness"}}

My test below though tries to get the result and check the title  
field. This is null and the source is also empty.

I'm not sure how to store the values in ES so they can also be  
retrieved. My mapping suggests that I've set store to true but I maybe  
missing something here.

public class ESTest extends Assert {  
private static final String TEST\_INDEX = "mediaindex1";  
private static final String TEST\_TYPE = "object";  
private static final String TEST\_ID1 = "1";  
private static final String MEDIA\_TITLE\_VALUE = "harry potters songs  
of wackyness";  
private static final String MEDIA\_TITLE\_FIELD = "title";  
private static final String MEDIA\_DESCRIPTION\_FIELD = "description";

```
@Test
public void test() throws IOException {
	Node node = createNode();
	try {
		Client client = node.client();
		createMapping(client);
		index(client);
		client.admin().indices().refresh(refreshRequest()).actionGet();
		get(client);
	} finally {
		node.close();
	}
}

private Node createNode() {
	// creates a new node
	// setting client to true means no data will be held by this node

```

(only acts as a client)  
Node node = NodeBuilder.nodeBuilder()  
.client(true)  
.local(false)  
.node().start();

```
	return node;
}

private void get(Client client) {
	GetResponse get = null;
	try {
		get =

```

client.get(getRequest(TEST\_INDEX).type(TEST\_TYPE).id(TEST\_ID1).fields(MEDIA\_TITLE\_FIELD)).get();  
} catch (InterruptedException e) {  
e.printStackTrace();  
} catch (ExecutionException e) {  
e.printStackTrace();  
}

```
	assertNotNull(get);

	if(get.isSourceEmpty()) {
		System.out.println("source is empty in get response");
	}

	assertEquals(TEST_ID1, get.getId());
	assertEquals(TEST_TYPE, get.getType());
	assertEquals(MEDIA_TITLE_VALUE,

```

get.field(MEDIA\_TITLE\_FIELD).values().get(0));  
}

```
private void index(Client client) throws IOException {
	XContentBuilder json = jsonBuilder().startObject()
								.field(MEDIA_TITLE_FIELD, MEDIA_TITLE_VALUE)
							.endObject();

	IndexResponse index = client.index(
			indexRequest(TEST_INDEX).type(TEST_TYPE).id(TEST_ID1)
					.create(true)
					.source(json))
					.actionGet();

	assertNotNull(index);
	assertEquals(TEST_ID1, index.id());
	assertEquals(TEST_TYPE, index.type());
	assertEquals(TEST_INDEX, index.index());
}

private void createMapping(Client client) throws IOException {
	// create properties for the mapped type
	XContentBuilder data = jsonBuilder().startObject()
			.startObject("properties")
				.startObject(MEDIA_TITLE_FIELD)
					.field("type", "string")
					.field("index", "not_analyzed")
					.field("store", "yes")
				.endObject()
				.startObject(MEDIA_DESCRIPTION_FIELD)
					.field("type", "string")
					.field("index", "standard")
					.field("store", "yes")
				.endObject()
			.endObject()
		.endObject();

	CreateIndexResponse mapping = client
			.admin()
			.indices()
			.create(createIndexRequest(TEST_INDEX).mapping(TEST_TYPE, data))
			.actionGet();

	assertTrue(mapping.acknowledged());
}

```

}

Any help would be great.

Christian

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [December 16, 2010, 3:59pm UTC](https://discuss.elastic.co/t/mapping-via-java-api-and-storing-values-in-index/3665/2 "2010-12-16T15:59:52Z")

</div>

Try and wrap the mappings with the type itself as an object.

"type1" : {  
"properties" : {  
.....  
}  
}

On Wed, Dec 15, 2010 at 6:12 AM, ausWolf [cstrzadala@gmail.com](mailto:cstrzadala@gmail.com) wrote:

> Hi all,
> 
> New to Elastic Search and just having some issues getting my head  
> around mapping via the Java API. I've got the following unit test  
> below in which I'm trying to map a simple media object with 2 fields  
> (title and description).
> 
> Index
> 
> - mediaindex1
> 
> type
> 
> - object
> 
> id
> 
> - 1
> 
> I'm accessing one instance of ES running locally but outside the JVM.  
> I see that I'm able to index the doc and if I query ES via the rest  
> API:
> 
> curl -XGET '[http://localhost:9200/mediaindex1/object/1](http://localhost:9200/mediaindex1/object/1)'
> 
> I get the following JSON response:
> 
> {"\_index":"mediaindex1","\_type":"object","\_id":"1", "\_source" :  
> {"title":"harry potters songs of wackyness"}}
> 
> My test below though tries to get the result and check the title  
> field. This is null and the source is also empty.
> 
> I'm not sure how to store the values in ES so they can also be  
> retrieved. My mapping suggests that I've set store to true but I maybe  
> missing something here.
> 
> public class ESTest extends Assert {  
> private static final String TEST\_INDEX = "mediaindex1";  
> private static final String TEST\_TYPE = "object";  
> private static final String TEST\_ID1 = "1";  
> private static final String MEDIA\_TITLE\_VALUE = "harry potters songs  
> of wackyness";  
> private static final String MEDIA\_TITLE\_FIELD = "title";  
> private static final String MEDIA\_DESCRIPTION\_FIELD = "description";
> 
> ```
> @Test
> public void test() throws IOException {
> Node node = createNode();
> try {
> Client client = node.client();
> createMapping(client);
> index(client);
> 
> ```
> 
> client.admin().indices().refresh(refreshRequest()).actionGet();  
> get(client);  
> } finally {  
> node.close();  
> }  
> }
> 
> ```
> private Node createNode() {
> // creates a new node
> // setting client to true means no data will be held by this
> 
> ```
> 
> node  
> (only acts as a client)  
> Node node = NodeBuilder.nodeBuilder()  
> .client(true)  
> .local(false)  
> .node().start();
> 
> ```
> return node;
> }
> 
> private void get(Client client) {
> GetResponse get = null;
> try {
> get =
> 
> ```
> 
> client.get(getRequest(TEST\_INDEX).type(TEST\_TYPE).id(TEST\_ID1).fields(MEDIA\_TITLE\_FIELD)).get();  
> } catch (InterruptedException e) {  
> e.printStackTrace();  
> } catch (ExecutionException e) {  
> e.printStackTrace();  
> }
> 
> ```
> assertNotNull(get);
> 
> if(get.isSourceEmpty()) {
> System.out.println("source is empty in get
> 
> ```
> 
> response");  
> }
> 
> ```
> assertEquals(TEST_ID1, get.getId());
> assertEquals(TEST_TYPE, get.getType());
> assertEquals(MEDIA_TITLE_VALUE,
> 
> ```
> 
> get.field(MEDIA\_TITLE\_FIELD).values().get(0));  
> }
> 
> ```
> private void index(Client client) throws IOException {
> XContentBuilder json = jsonBuilder().startObject()
> 
> ```
> 
> .field(MEDIA\_TITLE\_FIELD, MEDIA\_TITLE\_VALUE)
> 
> .endObject();
> 
> ```
> IndexResponse index = client.index(
> 
> ```
> 
> indexRequest(TEST\_INDEX).type(TEST\_TYPE).id(TEST\_ID1)  
> .create(true)  
> .source(json))  
> .actionGet();
> 
> ```
> assertNotNull(index);
> assertEquals(TEST_ID1, index.id());
> assertEquals(TEST_TYPE, index.type());
> assertEquals(TEST_INDEX, index.index());
> }
> 
> private void createMapping(Client client) throws IOException {
> // create properties for the mapped type
> XContentBuilder data = jsonBuilder().startObject()
> .startObject("properties")
> .startObject(MEDIA_TITLE_FIELD)
> .field("type", "string")
> .field("index",
> 
> ```
> 
> "not\_analyzed")  
> .field("store", "yes")  
> .endObject()
> 
> .startObject(MEDIA\_DESCRIPTION\_FIELD)  
> .field("type", "string")  
> .field("index", "standard")  
> .field("store", "yes")  
> .endObject()  
> .endObject()  
> .endObject();
> 
> ```
> CreateIndexResponse mapping = client
> .admin()
> .indices()
> 
> ```
> 
> .create(createIndexRequest(TEST\_INDEX).mapping(TEST\_TYPE, data))  
> .actionGet();
> 
> ```
> assertTrue(mapping.acknowledged());
> }
> 
> ```
> 
> }
> 
> Any help would be great.
> 
> Christian

---

<div class="post-metadata">

**Author:** ![cstrzadala](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cstrzadala/32/8211_2.png) [@cstrzadala](https://discuss.elastic.co/u/cstrzadala)\
**Post date:** [January 14, 2011, 5:37am UTC](https://discuss.elastic.co/t/mapping-via-java-api-and-storing-values-in-index/3665/3 "2011-01-14T05:37:24Z")

</div>

Awesome,

Thanks for the help. No doubt I'll have more questions to come.

Christian

On Dec 17 2010, 2:59 am, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com)  
wrote:

> Try and wrap the mappings with the type itself as an object.
> 
> "type1" : {  
> "properties" : {  
> .....  
> }
> 
> }  
> On Wed, Dec 15, 2010 at 6:12 AM, ausWolf [cstrzad...@gmail.com](mailto:cstrzad...@gmail.com) wrote:
> 
> > Hi all,
> 
> > New to Elastic Search and just having some issues getting my head  
> > around mapping via the Java API. I've got the following unit test  
> > below in which I'm trying to map a simple media object with 2 fields  
> > (title and description).
> 
> > Index
> > 
> > - mediaindex1
> 
> > type
> > 
> > - object
> 
> > id
> > 
> > - 1
> 
> > I'm accessing one instance of ES running locally but outside the JVM.  
> > I see that I'm able to index the doc and if I query ES via the rest  
> > API:
> 
> > curl -XGET '[http://localhost:9200/mediaindex1/object/1](http://localhost:9200/mediaindex1/object/1)'
> 
> > I get the following JSON response:
> 
> > {"\_index":"mediaindex1","\_type":"object","\_id":"1", "\_source" :  
> > {"title":"harry potters songs of wackyness"}}
> 
> > My test below though tries to get the result and check the title  
> > field. This is null and the source is also empty.
> 
> > I'm not sure how to store the values in ES so they can also be  
> > retrieved. My mapping suggests that I've set store to true but I maybe  
> > missing something here.
> 
> > public class ESTest extends Assert {  
> > private static final String TEST\_INDEX = "mediaindex1";  
> > private static final String TEST\_TYPE = "object";  
> > private static final String TEST\_ID1 = "1";  
> > private static final String MEDIA\_TITLE\_VALUE = "harry potters songs  
> > of wackyness";  
> > private static final String MEDIA\_TITLE\_FIELD = "title";  
> > private static final String MEDIA\_DESCRIPTION\_FIELD = "description";
> 
> > ```
> > @Test
> > public void test() throws IOException {
> > Node node = createNode();
> > try {
> > Client client = node.client();
> > createMapping(client);
> > index(client);
> > 
> > ```
> 
> > client.admin().indices().refresh(refreshRequest()).actionGet();  
> > get(client);  
> > } finally {  
> > node.close();  
> > }  
> > }
> 
> > ```
> > private Node createNode() {
> > // creates a new node
> > // setting client to true means no data will be held by this
> > 
> > ```
> > 
> > node  
> > (only acts as a client)  
> > Node node = NodeBuilder.nodeBuilder()  
> > .client(true)  
> > .local(false)  
> > .node().start();
> 
> > ```
> > return node;
> > }
> > 
> > ```
> 
> > ```
> > private void get(Client client) {
> > GetResponse get = null;
> > try {
> > get =
> > 
> > ```
> 
> > client.get(getRequest(TEST\_INDEX).type(TEST\_TYPE).id(TEST\_ID1).fields(MEDIA \_TITLE\_FIELD)).get();  
> > } catch (InterruptedException e) {  
> > e.printStackTrace();  
> > } catch (ExecutionException e) {  
> > e.printStackTrace();  
> > }
> 
> > ```
> > assertNotNull(get);
> > 
> > ```
> 
> > ```
> > if(get.isSourceEmpty()) {
> > System.out.println("source is empty in get
> > 
> > ```
> > 
> > response");  
> > }
> 
> > ```
> > assertEquals(TEST_ID1, get.getId());
> > assertEquals(TEST_TYPE, get.getType());
> > assertEquals(MEDIA_TITLE_VALUE,
> > 
> > ```
> > 
> > get.field(MEDIA\_TITLE\_FIELD).values().get(0));  
> > }
> 
> > ```
> > private void index(Client client) throws IOException {
> > XContentBuilder json = jsonBuilder().startObject()
> > 
> > ```
> 
> > .field(MEDIA\_TITLE\_FIELD, MEDIA\_TITLE\_VALUE)
> 
> > .endObject();
> 
> > ```
> > IndexResponse index = client.index(
> > 
> > ```
> 
> > indexRequest(TEST\_INDEX).type(TEST\_TYPE).id(TEST\_ID1)  
> > .create(true)  
> > .source(json))  
> > .actionGet();
> 
> > ```
> > assertNotNull(index);
> > assertEquals(TEST_ID1, index.id());
> > assertEquals(TEST_TYPE, index.type());
> > assertEquals(TEST_INDEX, index.index());
> > }
> > 
> > ```
> 
> > ```
> > private void createMapping(Client client) throws IOException {
> > // create properties for the mapped type
> > XContentBuilder data = jsonBuilder().startObject()
> > .startObject("properties")
> > .startObject(MEDIA_TITLE_FIELD)
> > .field("type", "string")
> > .field("index",
> > 
> > ```
> > 
> > "not\_analyzed")  
> > .field("store", "yes")  
> > .endObject()
> 
> > .startObject(MEDIA\_DESCRIPTION\_FIELD)  
> > .field("type", "string")  
> > .field("index", "standard")  
> > .field("store", "yes")  
> > .endObject()  
> > .endObject()  
> > .endObject();
> 
> > ```
> > CreateIndexResponse mapping = client
> > .admin()
> > .indices()
> > 
> > ```
> 
> > .create(createIndexRequest(TEST\_INDEX).mapping(TEST\_TYPE, data))  
> > .actionGet();
> 
> > ```
> > assertTrue(mapping.acknowledged());
> > }
> > 
> > ```
> > 
> > }
> 
> > Any help would be great.
> 
> > Christian

---

<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 6, 2017, 4:13am UTC](https://discuss.elastic.co/t/mapping-via-java-api-and-storing-values-in-index/3665/4 "2017-07-06T04:13:56Z")

</div>


