English 中文(简体)
无法使用 Python SDK 进行 Azure AI 搜索将数据添加到复杂字段
原标题:Cannot add data to a ComplexField using Python SDK for Azure AI Search
I want to upload a payload with a nested dictionary to Azure AI Search index. I am using a ComplexField in the index for a nested dictionary in my payload. The nested dictionary is not being recognized by the index and I get a null-error. Here is my code: ComplexField, CorsOptions, SearchIndex, ScoringProfile, SearchFieldDataType, SimpleField, SearchableField, ) request_example = { "id": "1", "text": "bla", "payload": { "processId": "00665", "color": "green" } } # Create a search index index_client = SearchIndexClient(endpoint=service_endpoint, credential=credential) fields = [ SimpleField(name="id", type=SearchFieldDataType.String, key=True, sortable=True, filterable=True, facetable=True), SearchableField(name="text", type=SearchFieldDataType.String), ComplexField( name="payload", fields=[ SimpleField(name="processId", type=SearchFieldDataType.String), SimpleField(name="color", type=SearchFieldDataType.String), ], collection=True, ) ] # Configure the vector search configuration vector_search = VectorSearch( algorithms=[ HnswAlgorithmConfiguration( name="myHnsw", kind=VectorSearchAlgorithmKind.HNSW, parameters=HnswParameters( m=4, ef_construction=400, ef_search=500, metric=VectorSearchAlgorithmMetric.COSINE, ) ), ], profiles=[ VectorSearchProfile( name="myHnswProfile", algorithm_configuration_name="myHnsw", ), ], ) semantic_config = SemanticConfiguration( name="my-semantic-config", prioritized_fields=SemanticPrioritizedFields( # title_field=SemanticField(field_name="title"), # keywords_fields=[SemanticField(field_name="color")], content_fields=[SemanticField(field_name="text")] ) ) # Create the semantic settings with the configuration semantic_search = SemanticSearch(configurations=[semantic_config]) cors_options = CorsOptions(allowed_origins=["*"], max_age_in_seconds=60) from typing import List scoring_profiles: List[ScoringProfile] = [] # Create the search index with the semantic settings index = SearchIndex(name="complex_field_test", fields=fields, vector_search=vector_search, semantic_search=semantic_search,scoring_profiles=scoring_profiles, cors_options=cors_options) result = index_client.create_or_update_index(index) print(f {result.name} created ) # Upload some documents to the index search_client = SearchClient(endpoint=service_endpoint, index_name="complex_field_test", credential=credential) result = search_client.upload_documents([request_example]) print(f"Uploaded {len(documents)} documents") And the error I get: The request is invalid. Details: A null value was found for the property named payload , which has the expected type Collection(search.complex.payload)[Nullable=False] . The expected type Collection(search.complex.payload)[Nullable=False] does not allow null values. So, as I understand the nested dictionary "payload" is not being recognized and appears like empty, although the sub-fields are registered in the index and have no null values. Can you help me with this problem? How to add data to a ComplexField?
问题回答
try with square brackets: "payload": [ { "processId": "00665", "color": "green" } ]
Although this might be late, it could still be helpful for others. To save a payload like {"processId": "00665", "color": "green"} in a search index: Step 1: Determine whether you need payload as a single dictionary or as a list of dictionaries. Set collection=True in the ComplexField definition if you want a list of dictionaries. Step 2: Assuming collection=False (meaning you want payload as a single dictionary), you can save and upload documents as follows: payload = {"processId": "00665", "color": "green"} request_example = { "id": "1", "text": "bla", "payload": flatdict(payload) # you can check on this link how to flatdict https://www.freecodecamp.org/news/how-to-flatten-a-dictionary-in-python-in-4-different-ways/ } # Create a search index index_client = SearchIndexClient(endpoint=service_endpoint, credential=credential) fields = [ SimpleField(name="id", type=SearchFieldDataType.String, key=True, sortable=True, filterable=True, facetable=True), SearchableField(name="text", type=SearchFieldDataType.String), ComplexField( name="payload", fields=[ SimpleField(name="processId", type=SearchFieldDataType.String), SimpleField(name="color", type=SearchFieldDataType.String), ], collection=False, # important . Set to True if you want a list of collection of payload ) ] search_client = SearchClient(endpoint=service_endpoint, index_name="complex_field_test", credential=credential) result = search_client.upload_documents([request_example]) print(f"Uploaded {len(result)} documents")




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签