I’m a big fan of ElasticSearch. One of the products in the Elastic suite is App Search.
I tried to get some documents through the App Search API using the following code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task<ContentSearchModel> GetDocument(Guid id) | |
{ | |
var requestUri = $"/api/as/v1/engines/{_configuration.Engine}/documents?ids={id}"; | |
var serializeOptions = new JsonSerializerOptions | |
{ | |
PropertyNamingPolicy = new LowercaseNamingPolicy() | |
}; | |
var response = await Client.GetFromJsonAsync< ContentSearchModel>(requestUri, options:serializeOptions); | |
return response; | |
} |
Unfortunately this didn’t work but resulted in the following error message:
JSON must be an array or hash
The reason I got this exception is because the API is expecting a JSON array or object for the id parameter.
Unstructured JSON data is not permitted. You have to encapsulate your object in an array [] or a hash {} (what the error message is referring to).
If you are using query parameters, ensure that ids is followed with %5D%5B - this is an array, but escaped: ?ids%5D%5B=:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task<ContentSearchModel> GetDocument(Guid id) | |
{ | |
var requestUri = $"/api/as/v1/engines/{_configuration.Engine}/documents?ids%5D%5B={id}"; | |
var serializeOptions = new JsonSerializerOptions | |
{ | |
PropertyNamingPolicy = new LowercaseNamingPolicy() | |
}; | |
var response = await Client.GetFromJsonAsync< ContentSearchModel>(requestUri, options:serializeOptions); | |
return response; | |
} |