How to use string interpolation for query string - Elasticsearch

I am trying to perform string interpolation to my variable that contains of a string of JSON Objects. Below is my original query:

var query = "{\"size\": 1000,\"query\": {\"bool\": {\"should\":[ {\"match\": { \"level\": \"Information\" } }, {\"match\": { \"level\": \"Error\" } } ], " +
                    "\"filter\": [ { \"range\": { \"@timestamp\": { \"gte\": \"2021-07-26T07:58:45.304-05:00\", \"lt\": \"2021-07-26T08:58:45.305-05:00\" } } } ]," +
                    "\"minimum_should_match\": 1 } } }";

Here I attempted to add string interpolation:

.........

foreach (var y in x.BusinessProcessStep.LogDataSources)
{
       var fieldName = y.LogDataSource.LogFieldsMapping.LevelField.FieldName;
       var fieldValue = y.LogDataSource.LogFieldsMapping.LevelField.FieldValue;
}


var query = $"{{\"size\": 1000,\"query\": {{\"bool\": {{\"should\":[ {{\"match\": {{ \"{fieldName}\": \"{fieldValue}\" }} }}, {{\"match\": {{ \"{fieldName}\": \"{fieldValue}\" }} }} ], " +
                    "\"filter\": [ { \"range\": { \"@timestamp\": { \"gte\": \"2021-07-26T07:58:45.304-05:00\", \"lt\": \"2021-07-26T08:58:45.305-05:00\" } } } ]," +
                    "\"minimum_should_match\": 1 } } }";

Snippet of my Json file that I am grabbing from in the for loop:

"LogFieldsMapping": {

                      "IDField": { "FieldName": "_id" },
                       "LevelField": {
                       "FieldName": "_source.level",
                       "FieldValue": [
                                { "value": "Information" },
                                { "value": "Error" }
                        ]
              }

Model class snippet that contains LevelField and FieldValue:

public class LevelField
{
      public string FieldName { get; set; }
      public List<FieldValue> FieldValue { get; set; }
}

public class FieldValue
{
    public string value { get; set; }
}

But it does not work, it only is setting "information" in the query and not both "information" and "error". Inside of the forloop fieldValue will show up as "information" and "error". Can anyone help me if there is anything wrong in my usage for the variable query?

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.