Retrieving nested objects using `SearchRequestBuilder`


(Arinto Murdopo) #1

Hi all,

Here's our sample document:

{
  .... we have other fields in this document
  "urlEntities": [
    {
      "url": "https://x.xx/xxxxxxx",
      "expandedURL": "https://xxxxxxx.xxxx/p/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/",
      "end": 92,
      "start": 69,
      "displayURL": "xxxxxxxxxxxxxx.xxx/p/xxxxxxx..."
    }
  ],
  "createAtMilis": 1438757536000,
  "createdAt": "Aug 5, 2015 2:52:16 PM",
  .... we have other fields in this document
}

We tried to scan and filter the indices with many of those documents using this SearchRequestBuilder:

      SearchRequestBuilder srb = client.prepareSearch(index)
                .setTypes(type)
                .setSearchType(SearchType.SCAN)
                .addFields("createdAt", "createAtMilis", "urlEntities")
                .setQuery(QueryBuilders.matchAllQuery())
                .setPostFilter(FilterBuilders.andFilter(
                        FilterBuilders.rangeFilter( "createAtMilis").from(1398902400000L).to(1438757537000L),
                        FilterBuilders.existsFilter("urlEntities"))) 
                .setSize(100);

However, we don't get any SearchHit.

If we change the addFields line to addFields("createdAt", "createAtMilis", "urlEntities.expandedUrl"), we are able to get valid SearchHit, but the SearchHit doesn't contain field urlEntities.expandedUrl.

Is it the expected behavior of addFields and search with Scan in Elasticsearch? We would like to limit the number of resulting fields and to not retrieve the whole _source field.

How can we handle this case?

Thanks!


(Arinto Murdopo) #2

Nevermind, apparently I use wrong field name. Problem solved! yay!


(system) #3