Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching ElasticSearch using NEST C# Client

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

like image 924
keisar Avatar asked May 14 '12 10:05

keisar


People also ask

How do you search in Elasticsearch?

You can use the search API to search and aggregate data stored in Elasticsearch data streams or indices. The API's query request body parameter accepts queries written in Query DSL. The following request searches my-index-000001 using a match query. This query matches documents with a user.id value of kimchy .

What is NEST Elasticsearch?

NEST is an official high-level client created by Elasticsearch that maps all requests and responses as types and provides an interface to execute queries. Let's add the NEST Client from NuGet via package manager or by running the following command from the directory where the .csproj file lives: dotnet add package NEST.

What is elastic search .NET core?

Elasticsearch is a free, open-source search database based on the Lucene search library. Some key features include: Distributed and scalable, including the ability for sharding and replicas. Documents stored as JSON.


4 Answers

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);
like image 66
driushkin Avatar answered Sep 28 '22 06:09

driushkin


Just to confirm

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

Is the preferred way to search and the fact it feels a bit long is because there are alot of options you can play with that are not used here. I'm always open on suggestions to make it shorter!

The string overload is deprecated but wont be removed from NEST. I'll update the obsolete message to explicitly mention this.

like image 30
Martijn Laarman Avatar answered Sep 28 '22 07:09

Martijn Laarman


If the anonymous types above aren't your thing, you can just use JObjects from json.net and build your query that way. Then you can run it 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 the DSL in ES uses reserved keywords in C#, like bool, so I don't have to do any escaping.

like image 37
daviddeath Avatar answered Sep 28 '22 05:09

daviddeath


With ElasticSearch 2.0, I have to use a SearchResponse< NewType > in the Search method like this :

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 it help.

Note: I found very few documentation about PostData class and its generic type parameter

like image 22
Alexandre B Avatar answered Sep 28 '22 06:09

Alexandre B