Now I will try to create few examples with SearchRequestBuilder
after I created the index in the previous example :
Java ElasticSearch 5 examples with node index and mapping - running local.
A new dependency was added to maven pom to convert Java objects to JSON and JSON to Java Object.
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.3</version>
</dependency>
Now Is time to add few documents to the index:
ObjectMapper objectMapper = new ObjectMapper();
ExampleDocument exampleDocument;
exampleDocument = new ExampleDocument("first document","Not Me",10);
client.prepareIndex(indexName,documentType,"1").setSource(objectMapper.writeValueAsString(exampleDocument)).get();
exampleDocument = new ExampleDocument("A nice book","Unknown Again",20);
client.prepareIndex(indexName,documentType,"2").setSource(objectMapper.writeValueAsString(exampleDocument)).get();
exampleDocument = new ExampleDocument("Old dusty","Author Again",50);
client.prepareIndex(indexName,documentType,"3").setSource(objectMapper.writeValueAsString(exampleDocument)).get();
exampleDocument = new ExampleDocument("New Car","Car Lover",55);
client.prepareIndex(indexName,documentType,"4").setSource(objectMapper.writeValueAsString(exampleDocument)).get();
Now we have documents in index and we can start searching for them.
First example is a QueryBuilders.matchAllQuery()
private static void searchRequestBuilderMatchAllQuery(String indexName, Client client) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.readerFor(ExampleDocument.class);
SearchRequestBuilder builder = client.prepareSearch(indexName);
builder.setQuery(QueryBuilders.matchAllQuery());
builder.addSort("pages", SortOrder.DESC);
SearchResponse response = builder.get();
for (SearchHit searchHit : response.getHits()) {
ExampleDocument document= objectMapper.readValue(searchHit.getSourceAsString(), ExampleDocument.class);
System.out.println(document);
}
}
Because I added a sort by pages DESC , the output is :
ExampleDocument{title='New Car', author='Car Lover', pages=55}
ExampleDocument{title='Old dusty', author='Author Again', pages=50}
ExampleDocument{title='A nice book', author='Unknown Again', pages=20}
ExampleDocument{title='first document', author='Not Me', pages=10}
Now we will create a simple BoolQuery:
private static void searchRequestBuilderBoolQuery(String indexName, Client client) throws IOException {
System.out.println("SearchRequestBuilderExample.searchRequestBuilderBoolQuery");
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.readerFor(ExampleDocument.class);
SearchRequestBuilder builder = client.prepareSearch(indexName);
builder.setQuery(
boolQuery()
.must(termQuery("title","first"))
.mustNot(termQuery("title", "book"))
);
builder.addSort("pages", SortOrder.DESC);
SearchResponse response = builder.get();
for (SearchHit searchHit : response.getHits()) {
ExampleDocument document= objectMapper.readValue(searchHit.getSourceAsString(), ExampleDocument.class);
System.out.println(document);
}
}
The title must contain "first" and must not contain "book", the result is :
SearchRequestBuilderExample.searchRequestBuilderBoolQuery
ExampleDocument{title='first document', author='Not Me', pages=10}
To build a multi level BoolQuery like a tree we have to combine must/should .... with boolQuery
SearchRequestBuilder builder = client.prepareSearch(indexName);
builder.setQuery(
boolQuery()
.must(termQuery("title", "first"))
.must(
boolQuery()
.must(termQuery("title", "document"))
.must(termQuery("pages", 10)))
);
builder.addSort("pages", SortOrder.DESC);
SearchResponse response = builder.get();
The output will be the same.
Enjoy !
the code can be found on GitHub SearchRequestBuilder Example source code
Category: Java Tutorials