简体   繁体   中英

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

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 ?

请设置 "ignore_unmapped" : true,很可能它会解决您的问题

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM