6

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

4 Answers 4

13

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Any chance for an little code example (one line, nothing too big :) ) ? I didn't fully understand what you meant...
There is no overload for Search accepting a string parameter.
5

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.

5 Comments

Hi, updating the message would be really helpful but if you're not planning on removing it why deprecate it ? I feel that it should be more convenient because many people use NEST for free-form search (textbox input) maybe just elasticClient.Search<NewType>(s => s.Query("query string")) (which I believe is the deprecated one :) ) anyway great client, thank you for providing it!
elasticClient.Search<NewType>(s => s.Query("query string")) as a shortcut too elasticClient.Search<NewType>(s => s.Query(q => q.QueryString(d => d.Query(queryString)))); might be a very good idea, I'll probably rename it to elasticClient.Search<NewType>(s => s.QueryString("query string")) though. Thanks keisar :)
Also i deprecated it because if you are using the raw json overload i'm very keen to hear why. That's why i am using deprecate() with a link to the github, i might go back on this decision before the 1.0 release though if its causing issues or confusing though.
That was quick :) i will check it out in the upcoming days, I may register to the project to contribute, again thanks for the project and responses
2

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.

1 Comment

what version are you using? I get error for SearchRaw
1

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

1 Comment

Documentation for PostData<T> can be found here - elastic.co/guide/en/elasticsearch/client/net-api/2.x/…. I'll see if we can add some example uses too

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.