Searching ElasticSearch using NEST C# Client

keisar picture keisar · May 14, 2012 · Viewed 17.6k times · Source

I started looking around for a search engine and after some reading I decided going with ElasticSearch (which is quite amazing :)), my project is in C# so I looked around for a client and started using NEST, everything is quite straightforward but I am a bit confused on the searching part.

I want to search all fields on a specific type what I came up with is the following code:

elasticClient.Search<NewType>(s => s.Query(q => q.QueryString(d => d.Query(queryString))));

I saw that much of the string query search is deprecated and wanted to make sure the above is the correct way of doing this (the above is not marked as deprecated...) also it is a bit long for a simple task so maybe anyone knows another way of doing this.

Thanks

Answer

driushkin picture driushkin · May 14, 2012

I just use the string query version: create my query object using C# anonymous type and serialize it to JSON.

That way, I can have straightforward mapping from all the JSON query examples out there, no need translating into this "query DSL".

Elasticsearch by itself evolves quite rapidly, and this query DSL thus is bound to lack some features.

Edit: Example:

var query = "blabla";
var q = new
        {
            query = new
            {
                text = new
                {
                    _all= query
                }
            }, 
            from = (page-1)*pageSize, 
            size=pageSize
        };
        var qJson = JsonConvert.SerializeObject(q);
        var hits = _elasticClient.Search<SearchItem>(qJson);