Azure 认知搜索转义特殊字符

问题描述 投票:0回答:1

我正在尝试查询具有特殊字符的天蓝色搜索索引。我没有通过以下查询获得预期结果,

{
  "search": "*",
  "filter": "search.ismatch('*\\ca-\\*', 'Text')",
  "count": true,
  "top": 10
}

结果集,

[{
    "Id": "11109",    
    "Author": null,
    "Text": "ca-1000"
},
{
    "Id": "11109",    
    "Author": null,
    "Text": "da-20000"
}]

预计,

[{
    "Id": "11109",    
    "Author": null,
    "Text": "ca-1000"
}]

基本上,我想查询任何具有值“ca-”的记录的“文本”字段。我在这里缺少什么?

odata azure-cognitive-search azure-search-.net-sdk
1个回答
0
投票

根据提供的信息,问题可能是因为两个文档使用相同的

id value
,并且后面的值正在更新为相同的 id。

要解决此问题,请尝试为每个条目提供不同的 id。

您可以查看以下代码片段:



# Define the index schema
index_schema = {
    "name": index_name,
    "fields": [
        {"name": "id", "type": "Edm.String", "key": True},
        {"name": "Author", "type": "Edm.String", "searchable": True, "filterable": True},
        {"name": "Text", "type": "Edm.String", "searchable": True, "filterable": True},
    ],
}

# Create the index
headers = {
    "Content-Type": "application/json",
    "api-key": api_key,
}
url = f"https://{search_service_name}.search.windows.net/indexes?api-version={api_version}"
response = requests.post(url, headers=headers, data=json.dumps(index_schema))
response.raise_for_status()

# Upload sample data
documents =[{
    "Id": "11108",    
    "Author": None,
    "Text": "ca-1000"
},
{
    "Id": "11109",    
    "Author": None,
    "Text": "da-20000"
}]

upload_data = {"value": documents}
url = f"https://{search_service_name}.search.windows.net/indexes/{index_name}/docs/index?api-version={api_version}"
response = requests.post(url, headers=headers, data=json.dumps(upload_data))
response.raise_for_status()

通过上述设置,我能够获得所需的结果: enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.