Search code examples
azureazure-ai-search

HttpResponseError in Azure Ai search


I am new to cloud and trying create a vector embedding for Azure AI search. Here are the snippet of my code :

# Define the index fields
client = SearchIndexClient(endpoint, credential)
fields = [
  SimpleField(name="id", type=SearchFieldDataType.String, key=True, sortable=True, 
                   filterable=True, facetable=True),
  SimpleField(name="originalbalancedue", type=SearchFieldDataType.Double, 
                   sortable=True, filterable=True, facetable=True),
  SimpleField(name="adjustedbalancedue", type=SearchFieldDataType.Double, 
                   sortable=True, filterable=True, facetable=True),
  SimpleField(name="feeamount", type=SearchFieldDataType.Double, sortable=True, 
                   filterable=True, facetable=True),
  SearchableField(name="result", type=SearchFieldDataType.Double, sortable=True, 
                   filterable=True)
]

index = SearchIndex(name=index_name, fields=fields, vector_search=vector_search)
result = client.create_or_update_index(index)
print(f'{result.name} created')

And the search index was created successfully.

Now trying to insert text and embeddings into vector store as:

with open('worthiness_with_result_small.json', 'r') as f:
    content = f.read().strip()
    if content:
        documents = json.loads(content)
        print(f"loaded {len(documents)} documents")
    else:
        print("The file is empty.")
search_client = SearchClient(endpoint=endpoint, index_name=index_name, 
credential=credential)
result = search_client.upload_documents(documents)
print(f"Uploaded {len(documents)} documents") 

where my data in worthiness_with_result_small.json file looks like :

[{"id":"425001","originalbalancedue":1684269.59,"adjustedbalancedue":1683369.59,"feeamount":6659.1199999999998900,"result":5759.1199999999998900}]

I am getting error:

HttpResponseError: () The request is invalid. Details: Cannot convert the literal '5759.12' to the expected type 'Edm.String'.
Code: 
Message: The request is invalid. Details: Cannot convert the literal '5759.12' to the expected type 'Edm.String'.

Any idea on what I am doing wrong here?


Solution

  • I believe the issue is that you are using SearchableField for a double data type. Please note that only String and String (Collection) data types can be searchable.

    Please try by changing:

    SearchableField(name="result", type=SearchFieldDataType.Double, sortable=True, 
                       filterable=True)
    

    to

    SimpleField(name="result", type=SearchFieldDataType.Double, sortable=True, 
                       filterable=True)
    

    If you look at the source code for SearchableField, you will notice that it does not have a type attribute.

    def SearchableField(
        *,
        name: str,
        collection: bool = False,
        key: bool = False,
        hidden: bool = False,
        searchable: bool = True,
        filterable: bool = False,
        sortable: bool = False,
        facetable: bool = False,
        analyzer_name: Optional[Union[str, LexicalAnalyzerName]] = None,
        search_analyzer_name: Optional[Union[str, LexicalAnalyzerName]] = None,
        index_analyzer_name: Optional[Union[str, LexicalAnalyzerName]] = None,
        synonym_map_names: Optional[List[str]] = None,
        **kw  # pylint:disable=unused-argument
    ) -> SearchField:
    

    So your result field is created with String data type and when you try to insert a numeric value in that field, the operation fails.