1

I am able to successfully create an index using java api and search the source field. My Problem is not able to get the nested key field using search.

For ex my sample json stucture:

{
    "name":         "John Smith",
    "age":          42,
    "confirmed":    true,
    "join_date":    "2014-06-01",
    "timestamp": "14331222299",
    "home": {
        "lat":      51.5,
        "lon":      0.1
    },
    "accounts": [
        {
            "type": "facebook",
            "id":   "johnsmith"
        },
        {
            "type": "twitter",
            "id":   "johnsmith"
        }
    ]
}

i can able to index this json as source through java client api:-

 IndexResponse response = client.prepareIndex(mohan+"-"+14-10-2014, "mohanelastic")
                    .setSource(jsonoutput.toString())
                    .execute()
                    .actionGet();

My java client search api:

   QueryBuilder en = QueryBuilders.matchQuery("name", "John Smith");

FilterBuilder flb = FilterBuilders.andFilter(
                            FilterBuilders
                        .              .rangeFilter("timestamp").from(starttimeinmilli).to(endtimeinmilli),
                                                            FilterBuilders.queryFilter(QueryBuilders.matchQuery("confirmed", "true"))
                            );
            SearchResponse response = client.prepareSearch(mohan+"-"+14-10-2014)        
            .setTypes("mohanelastic")
            .setSearchType(SearchType.QUERY_AND_FETCH)
            .setPostFilter(flb)
            .setQuery(en)
            .setFrom(0).setSize(60)
            .execute().actionGet();

In this i can able to get the total hits, key field values(name,age,join_date). But not able to get the key value for (home.lat) it shows null value. Nested values for any json shows null.

I am retrieving the source field json keys and it shows respective value:-

System.out.println("event type"+response.getHits().getAt(0).getSource().get("name"));
System.out.println("event type"+response.getHits().getAt(0).getSource().get("timestamp"));

But when i try home.lat it shows null value:

System.out.println("event type"+response.getHits().getAt(0).getSource().get("home.lat"));

1 Answer 1

3

You can't access home.lat value using dot notation in the Java API. Think of nested objects as maps (home) or a list containing maps (accounts). To get the lat value you would need to do the following:

Map<String, Object> source = response.getHits().getAt(0).getSource();
Map<String, Object> home = source.get('home');
Double latValue = (Double) home.get('lat');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.