简体   繁体   中英

Azure Cognitive Search int to Edm.String field issues

I'm having trouble trying to added data to my Azure Cognitive Search index. The data is being read from SQL Server tables with a python script. The script sends it to the index using the SearchIndexClient from the azure search sdk.

The problem is when sending Python "int" values into a search index field of type Edm.String. The link below seems to indicate that this should be possible. Any number type is allowed to go into a Edm.String.

https://learn.microsoft.com/en-us/rest/api/searchservice/data-type-map-for-indexers-in-azure-search#bkmk_sql_search

However I get this error:

Cannot convert the literal '0' to the expected type 'Edm.String'.

Am I misunderstanding the docs? Is the python int different than the SQL Server int through the Azure Search SDK?

I'm using pyodbc to connect to an Azure Synapse db. Retrieving the rows with cursor loop. This is basically what I'm doing...

search_client = SearchIndexClient(env.search_endpoint,
  env.search_index, 
  SearchApiKeyCredential(env.search_api_key),
  logging_enable=True)
conn = pyodbc.connect(env.sqlconnstr_synapse_connstr, autocommit=True)
query = f"SELECT * FROM [{env.source_schema}].[{source_table}]"
cursor = conn.cursor()
cursor.execute(query)

source_table_columns = [source_table_column[0] for source_table_column in cursor.description]
rows = []
for source_table_values in cursor.fetchmany(MAX_ROWS_TO_FETCH):
   source_table_row = dict(zip(source_table_columns,
                               source_table_values))
   rows.append(source_table_row)

upload = search_client.upload_documents(documents=rows)

If the row contains a row with an int value and the search index table field is Edm.String, we get the error.

Cannot convert the literal '0' to the expected type 'Edm.String'.

Thank you for providing the code snippet. The data type mapping link is applicable when using an Indexer to populate an Index.

Indexers provide a convenient mechanism to load documents into an Index from a source datasource. They perform the mapping outlined here by default or can take in an optional fieldMappings .

In the case of the code snippet where an index is being updated manually, when there is a type mismatch between source & target, that would be handled by casting/converting etc. by the user. In the code snippet after you have the dictionary, you can convert the int into a string using str() before uploading the batch in to the Index

source_table_row[column_name] = str(source_table_row[column_name])

This is a python sample that creates an indexer to update an index

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM