Azure / azure-sdk-for-java

This repository is for active development of the Azure SDK for Java. For consumers of the SDK we recommend visiting our public developer docs at https://docs.microsoft.com/java/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-java.
MIT License
2.33k stars 1.97k forks source link

FIrst snippet for index creation does not work #42161

Open carloniluca91 opened 3 hours ago

carloniluca91 commented 3 hours ago

Type of issue

Code doesn't work

Description

I copied both the Hotel java model

public static class Hotel {
    @SimpleField(isKey = true, isFilterable = true, isSortable = true)
    private String id;
    @SearchableField(isFilterable = true, isSortable = true)
    private String name;

    public String getId() {
        return id;
    }

    public Hotel setId(String id) {
        this.id = id;
        return this;
    }

    public String getName() {
        return name;
    }

    public Hotel setName(String name) {
        this.name = name;
        return this;
    }
}

and the snippet for index creation

List<SearchField> searchFields = SearchIndexClient.buildSearchFields(Hotel.class, null);
SEARCH_INDEX_CLIENT.createIndex(new SearchIndex("index", searchFields));

However, when creating the index using the snippet, I got the following exception

Status code 400, "{"error":{"code":"OperationNotAllowed","message":"The request is invalid. Details: index : Found 0 key fields in index 'hotels'. Each index must have exactly one key field.","details":[{"code":"MissingKeyField","message":"Found 0 key fields in index 'hotels'. Each index must have exactly one key field. Parameters: index"}]}}"

Indeed, the 'id' field, although if annotated properly, was not enabled as key field. In addition, the 'static' qualifier should be removed from the Hotel class definition

Java version: 1.8 Azure Search SDK version: 11.6.0 OS: Windows

Page URL

https://learn.microsoft.com/en-us/java/api/overview/azure/search-documents-readme?view=azure-java-stable

Content source URL

https://github.com/Azure/azure-docs-sdk-java/blob/master/docs-ref-services/latest/search-documents-readme.md

Document Version Independent Id

91939d9d-2088-c3a5-dcdf-79d89da3a927

Article author

@azure-sdk

Metadata

github-actions[bot] commented 3 hours ago

@alzimmermsft @jairmyree

github-actions[bot] commented 3 hours ago

Thank you for your feedback. Tagging and routing to the team member best able to assist.