# Does Mapping apply on source too?

**URL:** <https://discuss.elastic.co/t/does-mapping-apply-on-source-too/3761>\
**Category:** Elasticsearch\
**Created:** [January 10, 2011, 8:17pm UTC](https://discuss.elastic.co/t/does-mapping-apply-on-source-too/3761 "2011-01-10T20:17:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Karussell1](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@Karussell1](https://discuss.elastic.co/u/Karussell1)\
**Post date:** [January 10, 2011, 8:17pm UTC](https://discuss.elastic.co/t/does-mapping-apply-on-source-too/3761/1 "2011-01-10T20:17:56Z")

</div>

When I'm feeding a document with a long-value (Java API) and  
retrieving the _source_ of this doc back I'm getting an integer value.  
What am I doing wrong? (See append code)

Kind Regards,  
Peter.

Node node = nodeBuilder().  
local(true).  
settings(ImmutableSettings.settingsBuilder().  
put("number\_of\_shards", 3).  
put("number\_of\_replicas", 1).  
put("gateway.type", "none").  
build()).  
build().  
start();

```
    String indexName = "tweetindex";
    String indexType = "tweet";

```

// load json from string  
String fileAsString = "{"  
+ ""tweet" : {"  
+ " "properties" : {"  
+ " "longval" : { "type" : "long",  
"null\_value" : -1}"  
+ "}}}";

```
    Client client = node.client();

    // create index
    CreateIndexResponse rsp = client.admin().indices().
            create(new

```

CreateIndexRequest(indexName).mapping(indexType, fileAsString)).  
actionGet();  
client.admin().cluster().health(new  
ClusterHealthRequest(indexName).waitForYellowStatus()).actionGet();

```
    XContentBuilder docBuilder =

```

XContentFactory.jsonBuilder().startObject();  
docBuilder.field("longval", 124L);  
docBuilder.endObject();

```
    // feed previously created doc
    IndexRequestBuilder irb = client.prepareIndex(indexName,

```

indexType, "1").  
setConsistencyLevel(WriteConsistencyLevel.DEFAULT).  
setSource(docBuilder);  
irb.execute().actionGet();

```
    // make doc available for sure
    client.admin().indices().refresh(new

```

RefreshRequest(indexName)).actionGet();

```
    // query for this doc
    XContentQueryBuilder qb = QueryBuilders.matchAllQuery();
    TermFilterBuilder fb = FilterBuilders.termFilter("longval",

```

124L);  
SearchRequestBuilder srb = client.prepareSearch(indexName).  
setSearchType(SearchType.QUERY\_AND\_FETCH).  
setQuery(QueryBuilders.filteredQuery(qb, fb));  
SearchResponse response = srb.execute().actionGet();  
System.out.println("failed shards:" +  
response.getFailedShards());//failed shards:0  
Object num = response.getHits().hits()  
[0].getSource().get("longval");  
System.out.println("longval:" + num);//longval:124  
System.out.println("longval.getClass:" + num.getClass());//  
longval.getClass:class java.lang.Integer  
node.stop();

---

<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:** [January 10, 2011, 8:31pm UTC](https://discuss.elastic.co/t/does-mapping-apply-on-source-too/3761/2 "2011-01-10T20:31:02Z")

</div>

Its basically the parser, since it can't tell from the json parsed if its long or not, it allocates the relevant data type depending on the value. It has nothing to do with the mapping itself (ES uses jackson to parse the json into a Map of Maps. I suggest you simply treat it as a Number, and call longValue.  
On Monday, January 10, 2011 at 10:17 PM, Ptr wrote:

> When I'm feeding a document with a long-value (Java API) and  
> retrieving the _source_ of this doc back I'm getting an integer value.  
> What am I doing wrong? (See append code)
> 
> Kind Regards,  
> Peter.
> 
> Node node = nodeBuilder().  
> local(true).  
> settings(ImmutableSettings.settingsBuilder().  
> put("number\_of\_shards", 3).  
> put("number\_of\_replicas", 1).  
> put("gateway.type", "none").  
> build()).  
> build().  
> start();
> 
> String indexName = "tweetindex";  
> String indexType = "tweet";  
> // load json from string  
> String fileAsString = "{"
> 
> - ""tweet" : {"
> - " "properties" : {"
> - " "longval" : { "type" : "long",  
> "null\_value" : -1}"
> - "}}}";
> 
> Client client = node.client();
> 
> // create index  
> CreateIndexResponse rsp = client.admin().indices().  
> create(new  
> CreateIndexRequest(indexName).mapping(indexType, fileAsString)).  
> actionGet();  
> client.admin().cluster().health(new  
> ClusterHealthRequest(indexName).waitForYellowStatus()).actionGet();
> 
> XContentBuilder docBuilder =  
> XContentFactory.jsonBuilder().startObject();  
> docBuilder.field("longval", 124L);  
> docBuilder.endObject();
> 
> // feed previously created doc  
> IndexRequestBuilder irb = client.prepareIndex(indexName,  
> indexType, "1").  
> setConsistencyLevel(WriteConsistencyLevel.DEFAULT).  
> setSource(docBuilder);  
> irb.execute().actionGet();
> 
> // make doc available for sure  
> client.admin().indices().refresh(new  
> RefreshRequest(indexName)).actionGet();
> 
> // query for this doc  
> XContentQueryBuilder qb = QueryBuilders.matchAllQuery();  
> TermFilterBuilder fb = FilterBuilders.termFilter("longval",  
> 124L);  
> SearchRequestBuilder srb = client.prepareSearch(indexName).  
> setSearchType(SearchType.QUERY\_AND\_FETCH).  
> setQuery(QueryBuilders.filteredQuery(qb, fb));  
> SearchResponse response = srb.execute().actionGet();  
> System.out.println("failed shards:" +  
> response.getFailedShards());//failed shards:0  
> Object num = response.getHits().hits()  
> [0].getSource().get("longval");  
> System.out.println("longval:" + num);//longval:124  
> System.out.println("longval.getClass:" + num.getClass());//  
> longval.getClass:class java.lang.Integer  
> node.stop();

---

<div class="post-metadata">

**Author:** ![Karussell1](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@Karussell1](https://discuss.elastic.co/u/Karussell1)\
**Post date:** [January 10, 2011, 8:42pm UTC](https://discuss.elastic.co/t/does-mapping-apply-on-source-too/3761/3 "2011-01-10T20:42:04Z")

</div>

ah, ok. thought it gets into the source via long + then it gets out  
via long.

Thanks for the clarification!

On 10 Jan., 21:31, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> Its basically the parser, since it can't tell from the json parsed if its long or not, it allocates the relevant data type depending on the value. It has nothing to do with the mapping itself (ES uses jackson to parse the json into a Map of Maps. I suggest you simply treat it as a Number, and call longValue.
> 
> On Monday, January 10, 2011 at 10:17 PM, Ptr wrote:
> 
> > When I'm feeding a document with a long-value (Java API) and  
> > retrieving the _source_ of this doc back I'm getting an integer value.  
> > What am I doing wrong? (See append code)
> 
> > Kind Regards,  
> > Peter.
> 
> > Node node = nodeBuilder().  
> > local(true).  
> > settings(ImmutableSettings.settingsBuilder().  
> > put("number\_of\_shards", 3).  
> > put("number\_of\_replicas", 1).  
> > put("gateway.type", "none").  
> > build()).  
> > build().  
> > start();
> 
> > String indexName = "tweetindex";  
> > String indexType = "tweet";  
> > // load json from string  
> > String fileAsString = "{"
> > 
> > - ""tweet" : {"
> > - " "properties" : {"
> > - " "longval" : { "type" : "long",  
> > "null\_value" : -1}"
> > - "}}}";
> 
> > Client client = node.client();
> 
> > // create index  
> > CreateIndexResponse rsp = client.admin().indices().  
> > create(new  
> > CreateIndexRequest(indexName).mapping(indexType, fileAsString)).  
> > actionGet();  
> > client.admin().cluster().health(new  
> > ClusterHealthRequest(indexName).waitForYellowStatus()).actionGet();
> 
> > XContentBuilder docBuilder =  
> > XContentFactory.jsonBuilder().startObject();  
> > docBuilder.field("longval", 124L);  
> > docBuilder.endObject();
> 
> > // feed previously created doc  
> > IndexRequestBuilder irb = client.prepareIndex(indexName,  
> > indexType, "1").  
> > setConsistencyLevel(WriteConsistencyLevel.DEFAULT).  
> > setSource(docBuilder);  
> > irb.execute().actionGet();
> 
> > // make doc available for sure  
> > client.admin().indices().refresh(new  
> > RefreshRequest(indexName)).actionGet();
> 
> > // query for this doc  
> > XContentQueryBuilder qb = QueryBuilders.matchAllQuery();  
> > TermFilterBuilder fb = FilterBuilders.termFilter("longval",  
> > 124L);  
> > SearchRequestBuilder srb = client.prepareSearch(indexName).  
> > setSearchType(SearchType.QUERY\_AND\_FETCH).  
> > setQuery(QueryBuilders.filteredQuery(qb, fb));  
> > SearchResponse response = srb.execute().actionGet();  
> > System.out.println("failed shards:" +  
> > response.getFailedShards());//failed shards:0  
> > Object num = response.getHits().hits()  
> > [0].getSource().get("longval");  
> > System.out.println("longval:" + num);//longval:124  
> > System.out.println("longval.getClass:" + num.getClass());//  
> > longval.getClass:class java.lang.Integer  
> > node.stop();

---

<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:14am UTC](https://discuss.elastic.co/t/does-mapping-apply-on-source-too/3761/4 "2017-07-06T04:14:04Z")

</div>


