# How to use string interpolation for query string - Elasticsearch

**URL:** <https://discuss.elastic.co/t/how-to-use-string-interpolation-for-query-string-elasticsearch/280001>\
**Category:** Elasticsearch\
**Created:** [July 29, 2021, 2:58pm UTC](https://discuss.elastic.co/t/how-to-use-string-interpolation-for-query-string-elasticsearch/280001 "2021-07-29T14:58:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![MarkCo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/markco/32/86746_2.png) [@MarkCo](https://discuss.elastic.co/u/MarkCo)\
**Post date:** [July 29, 2021, 2:58pm UTC](https://discuss.elastic.co/t/how-to-use-string-interpolation-for-query-string-elasticsearch/280001/1 "2021-07-29T14:58:45Z")

</div>

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

```auto
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:

```auto
.........

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:

```auto
"LogFieldsMapping": {

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

```

Model class snippet that contains `LevelField` and `FieldValue`:

```auto
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?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 26, 2021, 2:59pm UTC](https://discuss.elastic.co/t/how-to-use-string-interpolation-for-query-string-elasticsearch/280001/2 "2021-08-26T14:59:36Z")

</div>

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