This article shows how to use Elasticsearch with ASP.NET Core 1.0, dnxcore or dnx451. ElasticsearchCrud ASP.NET Core 1.0 rc1 version is used for the Elasticsearch data access. ElasticsearchCrud supports dnxcore50, dnx451 and .NET with Elasticsearch 2.0.0. By supporting dnxcore50, the Elasticsearch API can now be run on windows, linux or a mac.
2015.11.19: Updated to ASP.NET Core 1.0 rc1
Code: https://github.com/damienbod/AspNet5SearchWithElasticsearchCrud
To use Elasticsearch, add ElasticsearchCrud to the project.json file in the dependencies; “ElasticsearchCrud”: “2.0.0-beta8”
{ "webroot": "wwwroot", "version": "1.0.0-*", "dependencies": { "ElasticsearchCrud": "2.0.1-rc1", "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final", "Microsoft.AspNet.Mvc": "6.0.0-rc1-final", "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final", "Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final", "Microsoft.Extensions.Logging": "1.0.0-rc1-final", "Microsoft.Extensions.Logging.Console": "1.0.0-rc1-final", "Microsoft.Extensions.Logging.Debug": "1.0.0-rc1-final" }, "commands": { "web": "Microsoft.AspNet.Server.Kestrel" }, "frameworks": { "dnx451": { }, "dnxcore50": { } }, "exclude": [ "wwwroot", "node_modules" ], "publishExclude": [ "**.user", "**.vspscc" ] }
Here is a simple example of a search provider which implements a query_string query.
namespace AspNet5SearchWithElasticsearchCrud.Search { using System; using System.Collections.Generic; using System.Linq; using ElasticsearchCRUD; using ElasticsearchCRUD.Model.SearchModel; using ElasticsearchCRUD.Model.SearchModel.Queries; public class ElasticSearchProvider : ISearchProvider, IDisposable { public ElasticSearchProvider() { _context = new ElasticsearchContext(ConnectionString, _elasticSearchMappingResolver); } private const string ConnectionString = "http://localhost:9200/"; private readonly IElasticsearchMappingResolver _elasticSearchMappingResolver = new ElasticsearchMappingResolver(); private readonly ElasticsearchContext _context; public IEnumerable<Skill> QueryString(string term) { var results = _context.Search<Skill>(BuildQueryStringSearch(term)); return results.PayloadResult.Hits.HitsResult.Select(t => t.Source); } /* { "query": { "query_string": { "query": "*" } } } */ private ElasticsearchCRUD.Model.SearchModel.Search BuildQueryStringSearch(string term) { var names = ""; if (term != null) { names = term.Replace("+", " OR *"); } var search = new ElasticsearchCRUD.Model.SearchModel.Search { Query = new Query(new QueryStringQuery(names + "*")) }; return search; } public void AddUpdateEntity(Skill skill) { _context.AddUpdateDocument(skill, skill.Id); _context.SaveChanges(); } public void UpdateSkill(long updateId, string updateName, string updateDescription) { var skill = _context.GetDocument<Skill>(updateId); skill.Updated = DateTime.UtcNow; skill.Name = updateName; skill.Description = updateDescription; _context.AddUpdateDocument(skill, skill.Id); _context.SaveChanges(); } public void DeleteSkill(long deleteId) { _context.DeleteDocument<Skill>(deleteId); _context.SaveChanges(); } private bool isDisposed; public void Dispose() { if (isDisposed) { isDisposed = true; _context.Dispose(); } } } }
The MVC 6 controller implements basic CRUD action methods using URL parameters.
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNet.Mvc; namespace AspNet5SearchWithElasticsearchCrud.Controllers { using AspNet5SearchWithElasticsearchCrud.Search; [Route("api/[controller]")] public class SearchController : Controller { readonly ISearchProvider _searchProvider; public SearchController(ISearchProvider searchProvider) { _searchProvider = searchProvider; } [HttpGet("{term}")] public IEnumerable<Skill> Search(string term) { return _searchProvider.QueryString(term); } [HttpPost("{id}/{name}/{description}")] public IActionResult Post(long id, string name, string description) { _searchProvider.AddUpdateEntity( new Skill { Created = DateTimeOffset.UtcNow, Description = description, Name = name, Id = id }); string url = $"api/search/{id}/{name}/{description}"; return Created(url, id); } [Microsoft.AspNet.Mvc.HttpPut("{id}/{updateName}/{updateDescription}")] public IActionResult Put(long id, string updateName, string updateDescription) { _searchProvider.UpdateSkill(id, updateName, updateDescription); return Ok(); } // DELETE api/values/5 [Microsoft.AspNet.Mvc.HttpDelete("{id}")] public IActionResult Delete(int id) { _searchProvider.DeleteSkill(id); return new NoContentResult(); } } }
A new Skill document can be added using Fiddler.
And can also be viewed using the search with a query term as a parameter in the URL.
TODO ElasticsearchCrud:
– Clean up the dnx dependencies
– Port the Tests to XUnit
Links:
https://www.elastic.co/downloads/elasticsearch
https://www.nuget.org/packages/ElasticsearchCRUD/2.0.0-beta8
https://github.com/damienbod/ElasticsearchCRUD
Elasticsearch CRUD .NET provider
