'How to get the results of a Spring data elasticsearch query
I have made an elasticsearch query to find a page of members by county.
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface MemberSearchRepository extends ElasticsearchRepository<Member, Long> {
@Query("{\"match\": {\"county\": \"?0\"}}")
Page<Member> findByCounty(String county);
}
The overall goal is to get all the memberId's returned from the query and to use them as a search criteria for another query. (The entity 'member' has a field called 'memberId')
I tried getting the page content (in order to get the memberId field from the page) using .getContent..
Page<Member> countyResults = memberSearchRepository.findByCounty(query);
System.out.println("county results: " + countyResults.getContent().stream().collect(Collectors.groupingBy(Member::getMemberId)));
But I don't know what I'm really doing using .collect and it returns a bunch more than just the memberId
Here is what it shows in the sysout..
Member{id=1173, memberId='123e4567-e89b-12d3-a456-426614174000', ... }
I just need to get the memberId by itself.
Solution 1:[1]
This is what worked for me
for(int i=0; i<countyResults.getContent().toArray().length; i++) {
System.out.println("key: " + countyResults.getContent().get(i).getMemberId());
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | cluis92 |
