Search code examples
pythonjsondjangoazureazure-cognitive-search

Azure search - Create Index - JSON decode error


I'm using the django framework. I'm trying to create an index in the Azure portal using the REST api tutorial they have provided. I'm getting the following error when I send my post request.

JSONDecodeError at /createIndex

This is my method.

@csrf_exempt
def createIndex(request):
    endpoint = 'https://service.search.windows.net/'
    api_version = '2020-06-30'
    url = endpoint + "indexes" + api_version

    index_schema = {
        "name": "hotels-quickstar11t",  
        "fields": [
            {"name": "HotelId", "type": "Edm.String", "key": "true", "filterable": "true"},
            {"name": "HotelName", "type": "Edm.String", "searchable": "true", "filterable": "false", "sortable": "true", "facetable": "false"},
            {"name": "Description", "type": "Edm.String", "searchable": "true", "filterable": "false", "sortable": "false", "facetable": "false", "analyzer": "en.lucene"},
            {"name": "Description_fr", "type": "Edm.String", "searchable": "true", "filterable": "false", "sortable": "false", "facetable": "false", "analyzer": "fr.lucene"},
            {"name": "Category", "type": "Edm.String", "searchable": "true", "filterable": "true", "sortable": "true", "facetable": "true"},
            {"name": "Tags", "type": "Collection(Edm.String)", "searchable": "true", "filterable": "true", "sortable": "false", "facetable": "true"},
            {"name": "ParkingIncluded", "type": "Edm.Boolean", "filterable": "true", "sortable": "true", "facetable": "true"},
            {"name": "LastRenovationDate", "type": "Edm.DateTimeOffset", "filterable": "true", "sortable": "true", "facetable": "true"},
            {"name": "Rating", "type": "Edm.Double", "filterable": "true", "sortable": "true", "facetable": "true"},
            {"name": "Address", "type": "Edm.ComplexType", 
                "fields": [
                    {"name": "StreetAddress", "type": "Edm.String", "filterable": "false", "sortable": "false", "facetable": "false", "searchable": "true"},
                    {"name": "City", "type": "Edm.String", "searchable": "true", "filterable": "true", "sortable": "true", "facetable": "true"},
                    {"name": "StateProvince", "type": "Edm.String", "searchable": "true", "filterable": "true", "sortable": "true", "facetable": "true"},
                    {"name": "PostalCode", "type": "Edm.String", "searchable": "true", "filterable": "true", "sortable": "true", "facetable": "true"},
                    {"name": "Country", "type": "Edm.String", "searchable": "true", "filterable": "true", "sortable": "true", "facetable": "true"}
                ]
            }
        ]
    }
    
    headers = {'Content-Type': 'application/json', 'api-key': '******'}

    response = requests.post(url, headers=headers, json=index_schema)
    index = response.json()

    return HttpResponse(response)

Any idea whats wrong with my method?

fyi; I have masked my api key, and service name. They are valid, as they work for other methods.


Solution

  • I believe the issue is with the following line of your code:

    url = endpoint + "indexes" + api_version
    

    If you notice, you're missing a ? and api-version= query parameter (you're just including the api version value).

    Please change the above line of code to

    url = endpoint + "indexes?api-version=" + api_version
    

    and that should fix the problem.