Spring-data-elasticsearch“嵌套查询抛出[嵌套]无法在路径下找到嵌套对象”异常

时间:2019-03-23 08:18:55

标签: java spring spring-boot elasticsearch

我有2个POJO(人员和汽车),其中一个由另一个人引用

@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
}

这是Car对象,被称为嵌套在Person对象中

public class Car {

    private String name;

    private String model;

//getter and setter
}

这是我的REST终点。在这里,我试图返回具有给定汽车模型的人。我将汽车模型作为路径变量发送,并且正在创建QueryBuilder对象

@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;
    }

final SearchResponse response = searchRequestBuilder.get();发生了一个异常,说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]: 

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

请设置“ ignore_unmapped”:正确,很可能会解决您的问题