Spring-data-elasticsearch “nested query throws [nested] failed to find nested object under path” Exception

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.

What is the mapping?

I have not defined any specific mappings between the DTOs other than defining Car object within the Person object. Is that what I am doing wrong here ?
What type of mapping is required ?

Can you run this: https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-mapping.html

This is the result that I am getting. No nested type mapping information here. Does this mean that I am doing wrong when persisting data ?

    "person":{  
          "mappings":{  
             "user":{  
                "properties":{  
                   "car":{  
                      "properties":{  
                         "model":{  
                            "type":"text",
                            "fields":{  
                               "keyword":{  
                                  "type":"keyword",
                                  "ignore_above":256
                               }
                            }
                         },
                         "name":{  
                            "type":"text",
                            "fields":{  
                               "keyword":{  
                                  "type":"keyword",
                                  "ignore_above":256
                               }
                            }
                         }
                      }
                   },
                   "name":{  
                      "type":"text",
                      "fields":{  
                         "keyword":{  
                            "type":"keyword",
                            "ignore_above":256
                         }
                      }
                   }
                }
             }
          }
       }

Don't use nested query then.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.