I want to Search My Index in all Types and all fields for the query. I know how to do it using REST way. I want to know how to do it using JAVA API and more specifically in Spring Data Elasticsearch.
This is some code i have written but seams to be pretty messed in terms of return Type.
@Override
public Page searchInAll(String searchQuery) {
MatchQueryBuilder matchQueryBuilder =QueryBuilders.matchQuery("_all", "trump");
SearchQuery searchQueryFinal = new NativeSearchQueryBuilder().withQuery(matchQueryBuilder).withIndices("booksearchserver").withTypes("authors","books").build();
return elasticsearchTemplate.queryForPage(searchQueryFinal, Author.class, new SearchResultMapper(){
@SuppressWarnings("unchecked")
public <T> FacetedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
long totalHits = response.getHits().getTotalHits();
List<Book> content = new ArrayList<Book>();
for (SearchHit searchHit : response.getHits()) {
if (response.getHits().getHits().length <= 0) {
return null;
}else if(searchHit.getType().equals("books")){
logger.info("Creating Book,Mapping Book,Adding Book to Page");
}
else if(searchHit.getType().equals("author")){
logger.info("Creating Author,Mapping Author,Adding Author to Page");
}
logger.info(searchHit.getType());
}
List<FacetResult> facets = new ArrayList<FacetResult>();
if (response.getFacets() != null) {
for (Facet facet : response.getFacets()) {
FacetResult facetResult = DefaultFacetMapper.parse(facet);
if (facetResult != null) {
facets.add(facetResult);
}
}
}
return new FacetedPageImpl<T>((List<T>)content, pageable, totalHits, facets);
}
});