我想将带有嵌套字典的有效负载上传到 Azure AI 搜索索引。我在索引中使用 ComplexField 作为负载中的嵌套字典。索引无法识别嵌套字典,并且出现空错误。这是我的代码:
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")
我得到的错误:
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.
因此,据我了解,嵌套字典“有效负载”未被识别并且显示为空,尽管子字段已在索引中注册并且没有空值。你能帮我解决这个问题吗?如何向 ComplexField 添加数据?
在使用 Azure AI 搜索的 Python SDK 时遇到的错误信息表明,的索引定义预期“payload”字段是一个复杂类型数组 (Collection(search.complex.payload)),但它收到的值是单个对象。
为了解决这个问题,需要将“payload”的值包装在一个列表中,即使它只有一个元素:
request_example = {
"id": "1",
"text": "bla",
"payload": [ # Wrap the payload object in a list
{
"processId": "00665",
"color": "green"
}
]
}
通过将“payload”的值包装在一个列表中,告诉 Azure AI 搜索这是一个复杂类型数组,这与的索引定义相匹配。
另外,请确保的
ComplexField
定义中
collection=True
已经设置。这将确保该字段可以接受多个复杂对象。
以下是更正后的代码片段:
# ... other code ...
# 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]) # "payload" is now a list
print(f"Uploaded {len(result)} documents")
通过进行此更改,应该能够成功将数据上传到包含
ComplexField
的 Azure AI 搜索索引。