假设我有一个包含以下数据的 cosmosdb
{id: 1, name : "$abc"}, {id: 2, name : "cdf>>"}, {id : 3,name : "xyz"}
我正在使用 azure search api 使用 Node JS 搜索此集合的索引。
api post body : {
filter : ,
search: ,
top: ,
suggesterName:,
}
在搜索参数的请求正文中。 如果我直接给出特殊字符或通过转义特殊字符,我会收到一条错误消息,提示无效搜索语法 (search : '$' or search : '$') 。
当我对搜索字符串进行 URL 编码并将其发送到 api 时,不会发生错误。但是使用特殊字符搜索不起作用(比如搜索:'$'($ 将被编码)-> 不会返回 {id: 1, name: "$abc"},但使用搜索搜索:'x' -> 返回{id : 3, name : "xyz"}).
任何人都可以帮助我理解,即使在使用特殊字符进行搜索时,我需要做什么才能从 azure api 获得响应,因为我对 azure 非常陌生。
答案 0 :(得分:0)
我这边已经测试过了,可以看到如下截图,可以得到结果。与使用sdk相同。您能否尝试我的查询或提供有关如何创建索引或代码的更多详细信息?
通过rest api post请求:
const options = {
headers: {
'content-type': 'application/json',
'api-key': 'B556BCC1715872292DB1878EA4FB7EE9'
}
};
axios.post('https://search_service_name.search.windows.net/indexes/index_name/docs/search?api-version=2020-06-30-Preview',
{ "search": "$abc" }, options)
.then(response => {
console.log(response.data);
});
使用 SDK:
const indexClient = new SearchIndexClient(endpoint, new AzureKeyCredential(apiKey));
const searchClient = indexClient.getSearchClient(indexName);
searchOptions = {
searchFields: ["name"]
};
searchResults = await searchClient.search("cdf>>", searchOptions);
for await (const result of searchResults.results) {
console.log(`${JSON.stringify(result.document)}`);
}
这是我在集合中的数据:
{
"id": "3",
"category": "fun",
"name": "$abc",
"description": "Complete Cosmos DB Node.js Quickstart ⚡",
"isComplete": true,
"_rid": "zJlUAP-8snQCAAAAAAAAAA==",
"_self": "dbs/zJlUAA==/colls/zJlUAP-8snQ=/docs/zJlUAP-8snQCAAAAAAAAAA==/",
"_etag": "\"9e0123dd-0000-1800-0000-608661760000\"",
"_attachments": "attachments/",
"_ts": 1619419510
},
{
"id": "4",
"category": "fun",
"name": "cdf>>",
"description": "aasssddd",
"isComplete": true,
"_rid": "zJlUAP-8snQDAAAAAAAAAA==",
"_self": "dbs/zJlUAA==/colls/zJlUAP-8snQ=/docs/zJlUAP-8snQDAAAAAAAAAA==/",
"_etag": "\"9e0128dd-0000-1800-0000-608661970000\"",
"_attachments": "attachments/",
"_ts": 1619419543
}