i want to filter the suggestions of cognitive search depending on the permissions. all items in the index have like this object
"permissions": {
"readitem": ";test1@test.com;;test2@test.com;;test3@test.com;",
"readlist": ";test1@test.com;;test2@test.com;;test3@test.com;"
},
If the email of the user is one of the emails in the readitem, then he is allowed to see only the suggestions of the items that have his email.
i'm trying to use search.ismatch("test1@test.com|test2@test.com|test3@test.com",permissions/readitem) like that
SuggestOptions sp = new SuggestOptions()
{
UseFuzzyMatching = query.Fuzzy,
Size = 10,
Filter = "search.ismatch(\"test1@test.com|test2@test.com|test3@test.com\",permissions/readitem)",
};
but search.ismatch is only supported in the Search API and I have tried to use odata-filter-expr but it works only with arrays.
Any way to overcome this?
I solved this issue by creating an output field like this.
cosmosDbIndexer.OutputFieldMappings.Add(new FieldMapping("/permissions/readitem") { TargetFieldName = "hasPermission" });
and I used the rest of the API of cognitive search to select the retrieved items. Everything is described in the Microsoft document. https://learn.microsoft.com/en-us/rest/api/searchservice/autocomplete
here is the Request:
POST https://[service name].search.windows.net/indexes/[index name]/docs/autocomplete?api-version=[api-version]
Content-Type: application/json
api-key: [admin or query key]
Body:
{
"autocompleteMode": "oneTerm" (default) | "twoTerms" | "oneTermWithContext",
"fuzzy": true | false (default),
"highlightPreTag": "pre_tag",
"highlightPostTag": "post_tag",
"minimumCoverage": # (% of index that must be covered to declare query
successful; default 80),
"search": "partial_search_input",
"searchFields": "title, hasPermission, ...",
"suggesterName": "suggester_name",
"top": # (default 5)
}
The response is handled like that.
string[] groups= ["test1@test.com","test2@test.com","test3@test.com"];
IEnumerable<string> suggestions = response.value.Where(x => HasPermission(x.HasPermission, groups)).Select(x => x.Title);