i am to declare Body as JsonElement and inspect its properties to parse the contained value as string or string[], depending on the token type. but I am stuck. My body response from elasticsearch can be string or string[] but my data model only set as string[]. below are my snippet
Model:
public class ElasticsearchData
{
[JsonPropertyName("title")]
public string Title { get; set; }
[JsonPropertyName("url")]
public string Url { get; set; }
[JsonIgnore]
public string[] Body { get; set; }
[JsonPropertyName("creation_time")]
public string Creation_Time { get; set; }
[JsonIgnore]
public int NumberOfDoc { get; set; }
public ElasticsearchData()
{
Title = string.Empty;
Url = string.Empty;
Creation_Time = string.Empty;
NumberOfDoc = 0;
}
}
Snippet:
var request = new SearchRequest("search-search-ntlm")
{
Query = new MatchQuery("body")
{
Field = "body",
Query = query,
Fuzziness = new Fuzziness("auto")
},
Highlight = new Elastic.Clients.Elasticsearch.Core.Search.Highlight
{
Type = "plain",
PreTags = new[] { "<tag1>" },
PostTags = new[] { "</tag1>" },
Encoder = Elastic.Clients.Elasticsearch.Core.Search.HighlighterEncoder.Html,
Fields = new Dictionary<Field, HighlightField>
{
{
"body", new HighlightField
{
Type = HighlighterType.Plain,
PreTags = new[] { "<strong>" },
PostTags = new[] { "</strong>" },
FragmentSize = 150,
Fragmenter = HighlighterFragmenter.Span,
NumberOfFragments = 3,
NoMatchSize = 150,
HighlightQuery = new MatchQuery("body")
{
Query = query
}
}
}
}
}
};
var response = client.SearchAsync<ElasticsearchData>(request);
sample response:
{
"_index": "search-ocr-poc",
"_id": "5c87196f-eb64-4b2e-8570-5b41ef1f7759",
"_score": 2.591784,
"_source": {
"creation_time": "2024-03-28T07:39:05Z",
"size": 1847715,
"server_relative_url": "/sites/COPS/Documents/1.png",
"id": "5c87196f-eb64-4b2e-8570-5b41ef1f7759",
"type": "File",
"title": "1.png",
"body": "Oops Access denied er Please check with your System Administrator on System Access,, Thank you",
"_timestamp": "2024-04-01T04:11:07Z",
"url": "/sites/COPS/Documents/1.png"
}