When retrieving data from ElasticSearch, one approach to do this is via the following code.
var response = await elasticClient.SearchAsync<Document>(request);
Should the document structure be something similar to the following, upon running the above search request the response would include all data persisted and map it to all the provided properties.
{
public int Id { get; set; }
public string Name { get; set; }
public List<Tag> Tags { get; set; }
}
Optimizing retrieval by excluding unwanted properties
There might be instances where we would need only specific data within this structure. On can just remove the undesired properties from the Document class. Though this would present the user with only the set of wanted properties, performance wise it wouldn't have any benefits. This is because ElasticSearch would still return all the persisted data. The exclusion of the data would be just happening during the process when mapping the result to our structure.
To improve the performance by avoiding bulk and useless network traffic from the ElasticSearch server, we can optimize the search request to exclude unwanted properties. This can be achieved via the following code.
{
Source = new SourceFilter { Excludes = "tags" }
};
Using the above search request, we would be instructing ElasticSearch to exclude the tags properties from the response. Hence, the result sent by ElasticSearch would consist of the properties Id and Name.
Optimizing retrieval by including wanted properties
{
Source = new SourceFilter { Includes = "Name" }
};
Using the above search request, we would be instructing ElasticSearch to include only the Name property. All other properties would be set as null.
No comments:
Post a Comment