# Spring Boot with Native Query does not return Nested Objects

**URL:** <https://discuss.elastic.co/t/spring-boot-with-native-query-does-not-return-nested-objects/267565>\
**Category:** Elasticsearch\
**Created:** [March 17, 2021, 7:16pm UTC](https://discuss.elastic.co/t/spring-boot-with-native-query-does-not-return-nested-objects/267565 "2021-03-17T19:16:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Philip2020](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/philip2020/32/79564_2.png) [@Philip2020](https://discuss.elastic.co/u/Philip2020)\
**Post date:** [March 17, 2021, 7:16pm UTC](https://discuss.elastic.co/t/spring-boot-with-native-query-does-not-return-nested-objects/267565/1 "2021-03-17T19:16:44Z")

</div>

Hi There,

Fairly new to ES and I'm hoping someone can help me and stop me from pulling my hair out 🙂

I have a fairly basic Rest API which allows users to store college projects details, once created I store this info in ES. I also provide an endpoint which allows users to search college projects and this is where i'm having my issue.....

I'm using the Java high level rest client for querying my elastic search instance. I've fairly basic info in my index. Now the data in the index contains a nested object which when I query using the native query build does not get returned ie. its always null. Can anyone spot what i'm doing wrong here:

```auto
    public List<UserProjects> fetchUserProjectsSearchSuggestions(final String query, Integer offset, Integer limit) {
        log.info("Search with query {}", query);

        QueryBuilder queryBuilder =
                QueryBuilders
                        .multiMatchQuery(query, "projectDescription", "projectName", "projectTechStack")
                        .fuzziness(Fuzziness.AUTO);

        Query searchQuery = new NativeSearchQueryBuilder()
                .withFilter(queryBuilder)
                .withPageable(PageRequest.of(offset, limit))
                .build();

        SearchHits<UserProjects> projectHits =
                elasticsearchOperations
                        .search(searchQuery, UserProjects.class,
                                IndexCoordinates.of(elasticProjectsIndex));

        List<UserProjects> projectMatches = new ArrayList<>();
        projectHits.forEach(srchHit -> {
            projectMatches.add(srchHit.getContent());
        });
        return projectMatches;
    }

```

Here is my Pojo:

```auto
    @Getter
    @Setter
    @Document(indexName = "test.userprojects")
    public class UserProjects {

    @Field(type = FieldType.Text, name = "userId")
    private String userId;

    @Field(type = FieldType.Text, name = "projectDescription")
    private String projectDescription;

    @Field(type = FieldType.Nested)
    private ProjectCreator projectCreator;

    @Field(type = FieldType.Text, name = "projectName")
    private String projectName;

    @Field(type = FieldType.Text, name = "projectImgURI")
    private String projectImgURI;

    @Field(type = FieldType.Text, name = "name")
    private String name;

    @Field(type = FieldType.Auto, name = "projectTechStack")
    private List<String> projectTechStack;

    @Field(type = FieldType.Boolean, name = "isOpenSource")
    private Boolean isOpenSource;

    @Field(type = FieldType.Boolean, name = "isActive")
    private Boolean isActive;

    @Field(type = FieldType.Boolean, name = "lookingForCollabs")
    private Boolean lookingForCollabs;

    }

```

Here is my nested Object Pojo:

```auto
    @Getter
    @Setter
    public class ProjectCreator {

        @Field(type = FieldType.Text)
        private String userCurrentTitle;
        @Field(type = FieldType.Text)
        private String userDisplayName;
        @Field(type = FieldType.Text)
        private String userProfilePicURL;
    }

```

So when I run the method fetchUserProjectsSearchSuggestions, the project is correctly returned BUT the projectCreator nested object is null even though in ES it has data:

```auto
    {
            "_score": 2.476096,
            "_type": "_doc",
            "_id": "60512d612c8bf32f2035f878",
            "_source": {
              "projectDescription": "this is a desc",
              "lookingForCollabs": true,
              "projectName": "Whoop Test 2",
              "userId": "4",
              "teamId": "",
              "projectCreator": {
                "userProfilePicURL": "somelink",
                "userCurrentTitle": "sometitle",
                "userDisplayName": "someDisplayName"
              },
              "projectImgURI": "somelink",
              "projectTechStack": [
                "ruby",
                "cassandra",
                "python"
              ],
              "isOpenSource": false,
              "isActive": true
            },
            "_index": "test.userprojects"

```

The result from my controller when searching returns:

```auto
    [
    {
        "userId": "4",
        "projectDescription": "this is a desc",
        "projectCreator": null,
        "projectName": "whoop test 2",
        "projectImgURI": "some link",
        "name": null,
        "projectTechStack": [
            "ruby",
            "cassandra",
            "python"
        ],
        "isOpenSource": false,
        "isActive": true,
        "lookingForCollabs": true
    }
    ]

```

Thank you so much for reading!

---

<div class="post-metadata">

**Author:** ![Philip2020](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/philip2020/32/79564_2.png) [@Philip2020](https://discuss.elastic.co/u/Philip2020)\
**Post date:** [March 17, 2021, 7:17pm UTC](https://discuss.elastic.co/t/spring-boot-with-native-query-does-not-return-nested-objects/267565/2 "2021-03-17T19:17:47Z")

</div>

I believe and I could be wrong but I think nested objects are not stored in the same source document? If that is true....I still dont know how to get my search query above working.

Once again thank you for reading this!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 14, 2021, 7:18pm UTC](https://discuss.elastic.co/t/spring-boot-with-native-query-does-not-return-nested-objects/267565/3 "2021-04-14T19:18:04Z")

</div>

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