I am trying to do a fuzzy search using rest from a c# script and have tested a few different ways without success.
This payload works:
string payload = "{ "name" : "Peter"}";
…which is a normal search after the name “Peter”.
However, when trying to do a fuzzy search using the following payloads:
string payload = "{ "q": { "name": { "$regex": ".*Peter.*" } } }";
Response:
Success response from DB: {"items":[],"hasMore":false,"count":0}
Next:
string payload = @"
{
""q"": {
""name"": { ""$like"": ""%Peter%"" }
}
}";
Result:
Success response from DB: {"items":[],"hasMore":false,"count":0}
Next:
string payload = @"
{
""q"": {
""$textContains"": {
""name"": ""Peter""
}
}
}";
Result:
DB Error: BadRequest
Response: {"type":"http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1","status":400,"title":"The field name $textContains is not a recognized operator.","o:errorCode":"SODA-02002"}
Any help is highly appreciated.
I did solve this by deploying the following code doing a hard coded search for “Pet”:
async Task<string> FuzzyReadDb(string _accessToken, string _playerName)
{
try
{
client.DefaultRequestHeaders.Clear(); // Clear headers to ensure no old or unwanted headers are included
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _accessToken);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// Use the $like operator to search for the specific name
string payload = "{ "name" : { "$like" : "%Pet%" }}";
Debug.Log($"Sending payload: {payload}");
var content = new StringContent(payload, Encoding.UTF8, "application/json");
var response = await client.PostAsync($"https://{url}/ords/xxxxxxx/soda/latest/NameTest?action=query", content);
var responseBody = await response.Content.ReadAsStringAsync();
if (response.IsSuccessStatusCode)
{
Debug.Log($"Success response from DB: {responseBody}");
// Deserialize the JSON response into a C# object
var result = JsonConvert.DeserializeObject<ResponseResult>(responseBody);
// Process the data to extract and display all names
StringBuilder output = new StringBuilder();
if (result != null && result.items.Count > 0)
{
foreach (var item in result.items)
{
// Display each name found in the response
output.AppendLine($"{{"name":"{item.value.name}"}}");
}
// Update UI with all names
txt_Msg.text += output.ToString();
return output.ToString(); // Return all names as output
}
return "No matching items found.";
}
else
{
Debug.LogError($"DB Error: {response.StatusCode}nResponse: {responseBody}");
return $"DB Error: {response.StatusCode}n{responseBody}";
}
}
catch (HttpRequestException e)
{
Debug.LogError($"Request Error: {e.Message}");
return $"Request Error: {e.Message}";
}
catch (Exception e)
{
Debug.LogError($"Unexpected Error: {e.Message}");
return $"Unexpected Error: {e.Message}";
}
}
Response was:
{"name":"Peter"}
{"name":"Petter"}
{"name":"Petra"}
{"name":"Peter2"}