1

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
}

This is the Car object which is referred as a nested in the Person object

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 [car]

"nested" : {
    "query" : {
      "bool" : {
        "must" : [
          {
            "match" : {
              "car.model" : {
                "query" : "gt200",
                "operator" : "OR",
                "prefix_length" : 0,
                "max_expansions" : 50,
                "fuzzy_transpositions" : true,
                "lenient" : false,
                "zero_terms_query" : "NONE",
                "auto_generate_synonyms_phrase_query" : true,
                "boost" : 1.0
              }
            }
          }
        ],
        "adjust_pure_negative" : true,
        "boost" : 1.0
      }
    },
    "path" : "car",
    "ignore_unmapped" : false,
    "score_mode" : "none",
    "boost" : 1.0
  }
}]; nested: IllegalStateException[[nested] failed to find nested object under path [car]]; }{[5uefqk2YT0ahmj3s-S1cvw][person][1]: 

How Could I solve this problem ?

9
  • The field is named car but you're using cars as nested field in your query. Commented Mar 23, 2019 at 9:41
  • @Val, I have mistakenly posted it here. I have corrected that in the post. Still the exception is there Commented Mar 23, 2019 at 10:08
  • I'm still seeing car as the field name. Commented Mar 23, 2019 at 10:42
  • The object Car is referred as "car" everywhere now. It was a posting mistake and I have corrected it now Commented Mar 23, 2019 at 10:54
  • Please add mapping of your index. Commented Mar 23, 2019 at 11:06

1 Answer 1

1

Please set "ignore_unmapped" : true, most probably it will solve your problem

Sign up to request clarification or add additional context in comments.

1 Comment

Will help if you can whip up a few lines on how to set ignore_unmapped to true. This way OP can try to follow your instructions and see if it solves the problem (and also newbies to ES in the future that chanced upon this answer).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.