# How force index level for raw query

**URL:** <https://discuss.elastic.co/t/how-force-index-level-for-raw-query/73190>\
**Category:** Elasticsearch\
**Created:** [January 30, 2017, 10:39am UTC](https://discuss.elastic.co/t/how-force-index-level-for-raw-query/73190 "2017-01-30T10:39:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rookie](https://avatars.discourse-cdn.com/v4/letter/r/e47c2d/32.png) [@rookie](https://discuss.elastic.co/u/rookie)\
**Post date:** [January 30, 2017, 10:39am UTC](https://discuss.elastic.co/t/how-force-index-level-for-raw-query/73190/1 "2017-01-30T10:39:11Z")

</div>

Hello

I currently have a elasticsearch index like :  
`logs-{ automate timestamp message }`

I have a query like  
`{ ""aggs"": { ""langs"": { ""terms"": {""field"": ""automate""}} } }`

That works fine is the requester or in python

But I can't find a way to do the same thing in .net

I try  
`var searchResults = client.Search < string > (s => s .Profile() .From(0) .Index("logs-") .Size(100) .Query(q => q .Raw(@"{ ""aggs"": { ""langs"": { ""terms"": {""field"": ""automate""} } } }") )`  
But have this error:  
`Invalid NEST response built from a unsuccessful low level call on POST:/logs-/string/_search}	Nest.ISearchResponse<string> {Nest.SearchResponse<string>`

How force POST:/logs-/\_search and not POST:/logs-/string/\_search ?

(Try also like the documentation:  
`.Aggregations(a => a.Terms("automate", st => st .Field(o => o.Content) .Size(10) .ExecutionHint(TermsAggregationExecutionHint.Ordinals))`  
But o.Content do not compile ...)

[Elasticsearch.net](http://Elasticsearch.net) :2.X  
Nest 2.X  
Elasticsearch 2.3.3

Regards

---

<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:** [January 31, 2017, 10:29am UTC](https://discuss.elastic.co/t/how-force-index-level-for-raw-query/73190/2 "2017-01-31T10:29:44Z")

</div>

A few observations

1. You can't use `.Query(q => q.Raw(""))` to send an aggregation string as the json string here is inserted inside the `"query": { }` object on the request. There is [an open issue for supporting raw aggregations](https://github.com/elastic/elasticsearch-net/issues/1016), but it's _tricky_ 😄
2. You can omit the `type` in the URI by specifying `.AllTypes()` on the `SearchDescriptor<T>` (the `s` in your example).

You may prefer to use [Elasticsearch.Net](http://Elasticsearch.Net) to send this request, as you can send a raw string as you have; using the [Elasticsearch.Net](http://Elasticsearch.Net) client exposed through the `.LowLevel` property of the NEST client, you can have the best of both worlds; use NEST for high level calls, and drop down to [Elasticsearch.Net](http://Elasticsearch.Net) when you want to, still taking advantage of strong types on the response. Here's an example

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

var searchResults = client.LowLevel.Search<string>("log-", @"{
    ""aggs"": {
        ""langs"": {
            ""terms"": {""field"": ""automate""}
        }
    }
}");

```

You can also use an anonymous type to represent the query. It's a little bit easier than escaping quotes in a json string

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

var searchResults = client.LowLevel.Search<string>("log-", 
new {
    aggs = new {
        langs = new {
            terms = new { field = "automate" }
        }
    }
});

```

The `string` generic type in `Search<T>` is the type of the response. You can use the NEST `SearchResponse<T>` and expose documents in a strongly typed fashion e.g. using `SearchResponse<JObject>`.

---

<div class="post-metadata">

**Author:** ![rookie](https://avatars.discourse-cdn.com/v4/letter/r/e47c2d/32.png) [@rookie](https://discuss.elastic.co/u/rookie)\
**Post date:** [January 31, 2017, 12:22pm UTC](https://discuss.elastic.co/t/how-force-index-level-for-raw-query/73190/3 "2017-01-31T12:22:20Z")

</div>

Really thank you for you time

The solution with `client.LowLevel` works fine for me 🙂  
Just for my knowledge, I see that you add the index, should I also add document type?

(The second code did not compile on `new {` for me, but the first works 😉 )

Regards

---

<div class="post-metadata">

**Author:** ![rookie](https://avatars.discourse-cdn.com/v4/letter/r/e47c2d/32.png) [@rookie](https://discuss.elastic.co/u/rookie)\
**Post date:** [January 31, 2017, 1:13pm UTC](https://discuss.elastic.co/t/how-force-index-level-for-raw-query/73190/4 "2017-01-31T13:13:04Z")

</div>

There is just one thing, that I do not understang on this forum.  
It is how can we correctly format code ... 😓  
If I copy paste your code, it is good, but when I try to do it myself that won't work  
Really space 😥

---

<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:** [January 31, 2017, 10:39pm UTC](https://discuss.elastic.co/t/how-force-index-level-for-raw-query/73190/5 "2017-01-31T22:39:49Z")

</div>

> [@rookie](#):
>
> I see that you add the index, should I also add document type?

Add the type if you want to search for a specific document type within an index/indices, otherwise leave it out.

> [@rookie](#):
>
> It is how can we correctly format code

Once you have pasted your code, highlight it and press the `</>` button in the editor. Alternatively, you can enclose a block of code inside ``` and ```, optionally adding the language name after the opening ``` e.g.

```c#  
Code here  
```

---

<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:** [February 28, 2017, 10:40pm UTC](https://discuss.elastic.co/t/how-force-index-level-for-raw-query/73190/6 "2017-02-28T22:40:17Z")

</div>

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