How do I connect my .net core mvc application to Elasticsearch and display query result to my view?

Charlene 1 Reputation point
2022-06-10T03:09:40.957+00:00

I would like to know how I could connect to elasticsearch with .net core mvc (.net 6) utilizing entityframework core, and then get the data to display on my view.

NUGET NEST (7.17.2) is installed.

I have a BuildingViewModel:

public class BuildingViewModel
{
public string? Id { get; set; }
public string? Building { get; set; }
public string? Keeper { get; set; }
}

Connection string setting in appsettings.json (I have a default connection to another database for User Identity

{
"ConnectionStrings": {
"DefaultConnection": "other connection string",
"ElasticConnection": "Server :'my server IP', Port:'9200', Index :'my_index', UserName:'my_username', Password:'my_password'"
}
}

Program.cs

210141-image.png

ElasticDbContext.cs

public class ElasticDbContext : DbContext
{
public ElasticDbContext(DbContextOptions<ElasticDbContext> options)

    : base(options)  
    {  
    }  

    protected override void OnModelCreating(ModelBuilder modelBuilder)  
    {  
        modelBuilder.ApplyConfiguration(new ElasticClientConfiguration());  
    }  

    public DbSet<BuildingViewModel> Buildings { get; set; }  
}  

I am stuck on how I could open the connection in my controller and use the data in my view.
I have did some research but I do not wish to create a search bar in my view but instead I will search in my controller and simply display the data as graphs in my view. Please let me know how this could be done. Thanks!

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,184 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 56,686 Reputation points
    2022-06-10T16:38:45.033+00:00

    you question is not clear. elasticsearch is an index service with an api. while NEST supplies a fluent api, it has nothing to do with entity framework.

    see:

    https://github.com/elastic/elasticsearch-net