Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch.net client can't do basic search

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?

like image 286
Emil C Avatar asked Oct 17 '14 22:10

Emil C


1 Answers

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);
like image 92
Martijn Laarman Avatar answered Sep 24 '22 05:09

Martijn Laarman