# How Can I convert raw query with “aggs” to Nest query?

**URL:** <https://discuss.elastic.co/t/how-can-i-convert-raw-query-with-aggs-to-nest-query/105803>\
**Category:** Elasticsearch\
**Created:** [October 30, 2017, 7:54pm UTC](https://discuss.elastic.co/t/how-can-i-convert-raw-query-with-aggs-to-nest-query/105803 "2017-10-30T19:54:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nehad\_Hazem](https://avatars.discourse-cdn.com/v4/letter/n/db5fbb/32.png) [@Nehad\_Hazem](https://discuss.elastic.co/u/Nehad_Hazem)\
**Post date:** [October 30, 2017, 7:54pm UTC](https://discuss.elastic.co/t/how-can-i-convert-raw-query-with-aggs-to-nest-query/105803/1 "2017-10-30T19:54:34Z")

</div>

I have this raw query that is being used in Kibana to get and show some data from ElasticSearch.  
{  
"query": {  
"bool": {  
"must": [  
{  
"query\_string": {  
"query": "_",  
"analyze\_wildcard": true  
}  
},  
{  
"match": {  
"type": {  
"query": "Error",  
"type": "phrase"  
}  
}  
},  
{  
"match": {  
"area": {  
"query": "usa",  
"type": "phrase"  
}  
}  
},  
{  
"query\_string": {  
"analyze\_wildcard": true,  
"query": "_"  
}  
},  
{  
"range": {  
"requestTimestamp": {  
"gte": 1509369446307,  
"lte": 1509383846307,  
"format": "epoch\_millis"  
}  
}  
}  
],  
"must\_not": []  
}  
},  
"size": 0,  
"\_source": {  
"excludes": []  
},  
"aggs": {  
"2": {  
"date\_histogram": {  
"field": "requestTimestamp",  
"interval": "5m",  
"time\_zone": "America/Los\_Angeles",  
"min\_doc\_count": 1  
},  
"aggs": {  
"4": {  
"filters": {  
"filters": {  
"Readable Title 1": {  
"query\_string": {  
"query": "FirstField:("MyMachine-Prod-_") AND SecondField:("Message1")",  
"analyze\_wildcard": true  
}  
},  
"Readable Title 2": {  
"query\_string": {  
"query": "FirstField:("MyMachine-Prod-_") AND SecondField:("Message2")",  
"analyze\_wildcard": true  
}  
},  
"Readable Title 3": {  
"query\_string": {  
"query": "FirstField:("MyMachine-Prod-_") AND SecondField:("Message3")",  
"analyze\_wildcard": true  
}  
},  
"Readable Title 4": {  
"query\_string": {  
"query": "SecondField:("Message4;") AND FirstField:("MyMachine-Prod-_")",  
"analyze\_wildcard": true  
}  
},  
"Readable Title 5": {  
"query\_string": {  
"query": "SecondField:("Message5") AND FirstField:("MyMachine-Prod-_")",  
"analyze\_wildcard": true  
}  
},  
"Readable Title 6": {  
"query\_string": {  
"query": "SecondField:("Message6") AND FirstField:("MyMachine-Prod-_")",  
"analyze\_wildcard": true  
}  
},  
"Readable Title 7": {  
"query\_string": {  
"query": "SecondField:("Message7") AND FirstField:("MyMachine-Prod-_")",  
"analyze\_wildcard": true  
}  
},  
"Readable Title 8": {  
"query\_string": {  
"query": "SecondField:("Message8") AND FirstField:("MyMachine-Prod-_")",  
"analyze\_wildcard": true  
}  
},  
"Readable Title 9": {  
"query\_string": {  
"query": "FirstField:("MyMachine-Prod-_") AND SecondField:("Message9")",  
"analyze\_wildcard": true  
}  
},  
"Readable Title 10": {  
"query\_string": {  
"query": "FirstField:("MyMachine-Prod-_") AND SecondField:("Message10")",  
"analyze\_wildcard": true  
}  
},  
"Readable Title 11": {  
"query\_string": {  
"query": "FirstField:("MyMachine-Prod-_") AND SecondField:("Message11")",  
"analyze\_wildcard": true  
}  
}  
}  
},  
"aggs": {  
"1": {  
"cardinality": {  
"field": "Guid"  
}  
}  
}  
}  
}  
}  
},  
"highlight": {  
"pre\_tags": [  
"@kibana-highlighted-field@"  
],  
"post\_tags": [  
"@/kibana-highlighted-field@"  
],  
"fields": {  
"_": {  
"highlight\_query": {  
"bool": {  
"must": [  
{  
"query\_string": {  
"query": "_",  
"analyze\_wildcard": true,  
"all\_fields": true  
}  
},  
{  
"match": {  
"type": {  
"query": "Error",  
"type": "phrase"  
}  
}  
},  
{  
"match": {  
"area": {  
"query": "usa",  
"type": "phrase"  
}  
}  
},  
{  
"query\_string": {  
"analyze\_wildcard": true,  
"query": "_",  
"all\_fields": true  
}  
},  
{  
"range": {  
"requestTimestamp": {  
"gte": 1509369446307,  
"lte": 1509383846307,  
"format": "epoch\_millis"  
}  
}  
}  
],  
"must\_not": []  
}  
}  
}  
},  
"fragment\_size": 2147483647  
}  
}

I need to get convert this query to Nest query and run it from inside C# console application to get the same data in my application, then I will do some processing on the returned data. Can anyone please help me in converting this query to Nest query?

Note: I have class in my C# code that matches has all the fields that are in my ElasticSearch Index and I'm able to do simple queries on it.

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [November 5, 2017, 11:11pm UTC](https://discuss.elastic.co/t/how-can-i-convert-raw-query-with-aggs-to-nest-query/105803/2 "2017-11-05T23:11:26Z")

</div>

What have you tried so far?

If you'd just like to run the query using a JSON string, you can do so using the low level client. If you use the low level client exposed on the high level client, you can also get back a strongly typed search response

```auto
var client = new ElasticClient();

// substitute for your JSON string
var query = @"
{
  ""query"": {
    ""match"": {
      ""_all"": {
        ""query"": ""some query""
      }
    }
  }
}";

var searchResult = client.LowLevel.Search<SearchResponse<MyDocument>>(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:** [December 3, 2017, 11:11pm UTC](https://discuss.elastic.co/t/how-can-i-convert-raw-query-with-aggs-to-nest-query/105803/3 "2017-12-03T23:11:38Z")

</div>

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