Skip to content

Instantly share code, notes, and snippets.

@sebnmuller
Last active August 29, 2015 14:17
Show Gist options
  • Save sebnmuller/92772945f5281df54c3b to your computer and use it in GitHub Desktop.
Save sebnmuller/92772945f5281df54c3b to your computer and use it in GitHub Desktop.
Example Elasticsearch SearchRequestBuilder
// Use our autowired searchService to instantiate an Elasticsearch client
Client client = searchService.elasticsearchClient(host, 9300);
// Initialise our SRB
SearchRequestBuilder srb = searchService.elasticsearchSearchRequestBuilder(client, index);
// Add aggregations to the srb
srb.addAggregation(AggregationBuilders.terms("shop").field("shop").size(20));
// Create our filtered query and add it to the srb
FilterBuilder fb = elasticsearchResultFunctions.gteDateRangeFilter("valid_to", DateTime.now());
QueryBuilder qb = QueryBuilders.matchAllQuery();
FilteredQueryBuilder fqb = QueryBuilders.filteredQuery(qb, fb);
srb = srb.setQuery(fqb);
// Get our response
response = srb.setTypes("products").setSize(30).execute().actionGet();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment