MetaCoder
MetaCoder

Reputation: 478

Spring Boot & Elastic Search | Multi-index search functionality

I have a spring boot API which has three models (User, Projects, Skills) currently I can search any of the indexes without issue as below:

  log.info("Search with query {}", query);
        Pageable pageable = PageRequest.of(page, size);

        QueryBuilder queryBuilder =
                QueryBuilders
                        .multiMatchQuery(query, "projectDescription", "projectName", "projectTechStack")
                        .fuzziness(Fuzziness.ONE);

        Query searchQuery = new NativeSearchQueryBuilder()
                .withFilter(queryBuilder)
                .withPageable(pageable)
                .build();

        SearchHits<UserProjects> projectHits =
                elasticsearchOperations
                        .search(searchQuery, UserProjects.class,
                                IndexCoordinates.of(elasticProjectsIndex));

However I would like to search all three indices and map them to the correct POJO. I'm rather stuck on this. Furthermore and this is where I get VERY stuck is, I would like to have a response paginated which would contain lets say the following:

User results: 3 of 100 
Project Results 4 of 10
etc etc

Basically so on my front end I can know if I should show a "click here for more User results" link which would run a search just on the user index.

If anyone can please help me that would be amazing.

Thanks

Upvotes: 1

Views: 978

Answers (1)

Sahil Gupta
Sahil Gupta

Reputation: 2166

If a user wants to search across multiple dimensions/indexes then MultiSearch will be very useful.

Example Usage: User can search for 'java' in person, article & job indices (in a LinkedIn type application) and would be able to configure pagination per request.

Upvotes: 2

Related Questions