Search code examples
c#.netelasticsearchnest

Set Index name by SearchRequest class in Nest


I use Nest client to use ElasticSearch .I want to search in ElasticSearch :

SearchRequest countRequest = new SearchRequest
{
  //Somthing
};

client.Search<Post>(countRequest);

On other hand :

client.Search<Post>(s=>s.Index("IndexName").Query(...))

How i can set index name by SearchRequest class search ?


Solution

  • SearchRequest contains an Indices property, so that you can specify multiple indices to search across. In your case, you could just pass the single index like so:

    var request = new SearchRequest
    {
        Indices = new IndexNameMarker[] { "IndexName" }
    };
    

    Another option would be to map your Post type to the index it belongs to, and use the typed SearchRequest<T> to let NEST infer the index name.