Spring Boot with Native Query does not return Nested Objects

Hi There,

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

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:

    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:

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

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

    {
            "_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:

    [
    {
        "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!

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!

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