ElasticSearch using NEST C # client

I started looking for a search engine, and after some reading I decided to go with ElasticSearch (which is pretty surprising :)), my project is in C #, so I looked at the client and started using NEST , everything is pretty simple, but I got a little confused in the search part .

I want to find all the fields in a particular type that I came across is the following code:

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

I saw that most of the search in the string query is out of date and wants to make sure the above is the correct way to do this (the above is not marked as deprecated ...) and it is a little longer for a simple task so maybe someone knows another way to do it.

thank

+5
source share
4 answers

I just use the version of the string request: create your request object using the anonymous C # type and serialize it in JSON.

That way, I can have a direct mapping to all examples of JSON requests, no need to translate this “DSL request”.

Elasticsearch itself is developing quite quickly, and in this matter DSL lacks 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);
+14
source

Just to confirm

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

It is the preferred way to search and make it feel a little long, because there are many options that you can play with that are not used here. I always open offers to make it shorter!

, NEST. , .

+5

If the anonymous types above are not your thing, you can just use JObjects from json.net and build the query this way. Then you can run it in the same way above.

JObject query = new JObject();
query["query"] = new JObject();
query["query"]["text"] = new JObject();
query["query"]["text"]["_all"] = searchTerm;
query["from"] = start;
query["size"] = maxResults;
string stringQuery = JsonConvert.SerializeObject(query);
var results = connectedClient.SearchRaw<SearchItem>(stringQuery);

I like this way better because DSL in ES uses reserved keywords in C #, like bool, so I don't need to make any screens.

+2
source

With ElasticSearch 2.0, I have to use SearchResponse <NewType> in the search method, for example:

var json = JsonConvert.SerializeObject(searchQuery);
var body = new PostData<object>(json);
var res = _elasticClient.Search<SearchResponse<NewType>>(body);
IEnumerable<NewType> result = res.Body.Hits.Select(h => h.Source).ToList();

Hope this helps.

Note. I found very little documentation about the PostData class and its type parameter

+1
source