5

I have a basic Elasticsearch query that looks like this

POST /fruit/_search
{"query":{"term":{"Name":"banana"}}}

I get result back, no problems when I run in sense.

So I try to do this in elasticsearch.net

var requestBody = new { query = new { term = new { Name = "banana" } } };
                var result = client.Search<string>("fruit", requestBody);

And I get no results back. If I just have a search body with new {} then I get hits, but not filtered.

What am I doing wrong?

3
  • that is strange worked for me Commented Oct 18, 2014 at 0:36
  • I think Nest might lowercasing Name. You can serialize and log the search to debug. Commented Oct 18, 2014 at 0:37
  • What about going step by step through your code and trying to find where the problem comes from ? Commented Oct 18, 2014 at 7:36

1 Answer 1

8

If you use the low level client (elasticsearch.net) directly it will not do any normalisation and serialise the object verbatim:

var query = new { query = new { term = new { Name = "banana" } } };
var json = new ElasticsearchClient().Serializer.Serialize(query).Utf8String();

this will result to the following json:

{
  "query": {
    "term": {
      "Name": "banana"
    }
  }
}

If you use NEST the default behaviour is to camelCase property names (NEST is opinionated):

{
  "query": {
    "term": {
      "name": "banana"
    }
  }
}

If you use the low level client through the high level client (client.Raw) it will use the exact same serialisation settings as the high level client.

You can control this behaviour on the high level client through:

var connectionSettings = new ConnectionSettings()
    .SetDefaultPropertyNameInferrer(p=>p);
var client = new ElasticClient(connectionSettings);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer. I still think casing should be left alone if not the user implements it them selves.
I agree, NEST is something i wrote mostly for my own use while i was integrating elasticsearch in my applications back in 2010. The convention was baked in since and even though NEST has been completely rewritten internally leading to its 1.0 release changing it now would cause to many problems for existing users. When splitting out the low level client we did deliberately chose for it NOT to have any magic like this baked in though.

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.