CorribView
CorribView

Reputation: 741

Lowercase Document Type during Nest search of Elasticsearch cluster

I'm searching a v5.5 elasticsearch cluster that's hosted using AWS's managed solution. I'm using a client to send search requests to the cluster but it's not finding any hits. I switched on the cluster lever logging and can see that the problem is that the type being searched is in lowercase (when the document types in the index are uppercase) so it can't match on any documents.

I'm passing a search descriptor object into the Nest client:

GetSearchDescriptor(SearchDescriptor<T> descriptor)
{
      descriptor.Index(index)
                    .Type(documentType)
                    .Query(q => (q
                                .Bool(bq => bq
                                 ...

}

client.Search<T>(s => GetSearchDescriptor(s))

Where documentType is of type T e.g. Invoice.

When I look at the CloudWatch logs I can see that the request that's hitting the cluster is lowercase invoice (instead of uppercase Invoice). This doesn't match the document type hence doesn't show any results. When I do a kibana search using the exact json in the logs I get the correct results when using uppercase Invoice, but no results when using lowercase invoice.

Any ideas what could be going on here?

Upvotes: 0

Views: 382

Answers (1)

Russ Cam
Russ Cam

Reputation: 125518

NEST's default inference for a type name from a POCO of type T, is to lowercase the typeof(T).Name value. You can easily change this behaviour for T or for all POCOs on ConnectionSettings

For all POCOs

var settings = new ConnectionSettings(new Uri("http://localhost:9200"))
    .DefaultTypeNameInferrer(type => type.Name.ToUpperInvariant());

var client = new ElasticClient(settings);

For only T e.g. Invoice

var settings = new ConnectionSettings(new Uri("http://localhost:9200"))
    .InferMappingFor<Invoice>(m => m
        .TypeName("INVOICE")
    );

var client = new ElasticClient(settings);

With the latter, you can also use InferMappingFor<T> to specify a default index name for T, and tell the client which property should be used to infer the id for the document.

Upvotes: 1

Related Questions