I am trying to query nested object in elasticsearch using spring-boot
I have 2 POJOs (Person and Car) where one is referred by the other
@Document(indexName = "person", type = "user")
public class Person {
@Id
private String id;
private String name;
@Field(type = FieldType.Nested)
private Car car;
//getter and setter
}
public class Car {
private String name;
private String model;
//getter and setter
}
This is my REST end point. Here I am trying to return the person who has the given car model. I am sending the car model as a path variable and I am creating a QueryBuilder
object
@RequestMapping(value = "/api/{carModel}")
public List<Map<String,Object>> search(@PathVariable final String carModel) {
QueryBuilder queryBuilder = QueryBuilders.nestedQuery(
"car",
QueryBuilders.boolQuery().must(QueryBuilders.matchQuery("car.model", carModel)),
ScoreMode.None);
final SearchRequestBuilder searchRequestBuilder = client.prepareSearch("person")
.setTypes("user")
.setSearchType(SearchType.QUERY_THEN_FETCH)
.setQuery(queryBuilder);
final SearchResponse response = searchRequestBuilder.get();
List<Map<String,Object>> resultList = new ArrayList<>();
List<SearchHit> searchHits = Arrays.asList(response.getHits().getHits());
for (SearchHit hit : searchHits) {
resultList.add(hit.getSourceAsMap());
}
return resultList;
}
There is an exception occurred at final SearchResponse response = searchRequestBuilder.get();
saying
java.lang.IllegalStateException: [nested] failed to find nested object under path [cars]
What is wrong this that I am doing here.