Does Mapping apply on source too?


(Karussell) #1

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();


(Shay Banon) #2

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();


(Karussell) #3

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 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();


(system) #4