Joseph Katzman
Joseph Katzman

Reputation: 2085

ElasticSearch query doesn't work

I have the following document:

{
        "_index": "taskmanager",
        "_type": "tasks",
        "_id": "AVn4vhIKiS68kYrc2Xp0",
        "_score": 0.8784157,
        "_source": {
          "Id": 2,
          "Title": "Hello World",
          "Description": "Description example",
          "PublishDate": "2017-01-29T15:06:04",
          "IsCompleted": true
        }

I can got the list of documents executing the query:

var hits = elasticClient.Search<Task>(s => s);

But I got nothing trying to get documents which contains 'world'.

var hits = elasticClient.Search<DalTask>(s => s
                .Type("tasks")
                .Query(q => q
                    .Match(m => m
                        .Field(p => p.Title).Query("world")
                    )
                )
            ).Hits;

Where is my mistake? P.s. My index set to default "taskmanager".

Upvotes: 0

Views: 85

Answers (1)

Russ Cam
Russ Cam

Reputation: 125488

By default, NEST camel cases C# POCO property names when serializing them to Elasticsearch document field names in the request, so

.Field(p => p.Title)

becomes

"title"

in the request. Looking at the _source in the response you have posted, it looks like your field names are Pascal-cased, hence the casing of field name in the search request is different and will not match.

You can change how NEST serializes C# POCO property names by changing .DefaultFieldNameInferrer(Func<string, string>) on ConnectionSettings

var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var connectionSettings = new ConnectionSettings(pool)
    // serialize POCO property names verbatim
    .DefaultFieldNameInferrer(s => s);

var client = new ElasticClient(connectionSettings);

Upvotes: 1

Related Questions