2

Am learning to use ElasticSearch with Nest as the .Net client. While indexing the data, I don't have a defined model (type mapping) to write the index to, I rely on ElasticSearch to create that for me

Creating the client

var settings = new ConnectionSettings(_config.Uri);
settings.DefaultIndex(_config.defaultIndexName);

_client = new ElasticClient(settings);

Indexing data by Type info

public void Index(object data)
{
    var response = _client.Index(data, d => d.Type(data.GetType().Name));
    if (!response.IsValid)
    {
        throw new InvalidOperationException(response.DebugInformation);
    }
}

Say if the data type was Project class with some properties

class Project
{
    int Id {get; set;}
    string Name {get; set;}
}

How do I query the above indexed data. Please bear in mind that the indexing part and the search/query part are completely isolated parts of the system and they are not aware of each other, so at search time, maximum I can get is the name of type 'Project' being queried but not its internal fields details. So, How can I run the search query in ElasticSearch as its fluent API would need to be something like this

_client.Search<Project>(...)

but I want something generic like

_client.Search('project', ...)

Thanks

sppc42
  • 2,994
  • 2
  • 31
  • 49

1 Answers1

2

I'm not completely sure what you mean when you say completely isolated, but searching with NEST is built around using SearchDescriptors, where you can specify quite a lot like the types, indices, query type and so on.

The SearchResponse type which is project in your case telling nest which object type to map the response to.

So:

_client.Search<Project>(...); // Response.Hits should be a list of IHit<Projects>

Whereas you could specify searching for projects in the search descriptor like this:

_client.Search<SomeResponseType>(searchDescriptor => searchDescriptor
    .Type("project")
    .Query(q => q.Term("name", "fooBar")));
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
A.Game
  • 459
  • 4
  • 16