├── .gitignore
├── AspNet5SearchWithElasticsearchCrud.sln
├── README.md
├── global.json
└── src
└── AspNet5SearchWithElasticsearchCrud
├── AspNet5SearchWithElasticsearchCrud.xproj
├── Controllers
└── SearchController.cs
├── Properties
├── AssemblyInfo.cs
└── launchSettings.json
├── Search
├── ElasticSearchProvider.cs
├── ISearchProvider.cs
└── Skill.cs
├── Startup.cs
├── project.json
├── web.config
└── wwwroot
├── index.html
└── web.config
/.gitignore:
--------------------------------------------------------------------------------
1 | .vs/AspNet5SearchWithElasticsearchCrud/v14/.suo
2 | src/AspNet5SearchWithElasticsearchCrud/AspNet5SearchWithElasticsearchCrud.xproj.user
3 | src/AspNet5SearchWithElasticsearchCrud/obj
4 | src/AspNet5SearchWithElasticsearchCrud/bin
5 | src/AspNet5SearchWithElasticsearchCrud/project.lock.json
6 | .vs/config/applicationhost.config
7 | .vs/restore.dg
8 |
--------------------------------------------------------------------------------
/AspNet5SearchWithElasticsearchCrud.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 14
4 | VisualStudioVersion = 14.0.25123.0
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{BAA2072E-07B2-4B8F-8D89-51134DB5E7C0}"
7 | EndProject
8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{15032822-9029-4E46-8DB1-D9B5D3DF6134}"
9 | ProjectSection(SolutionItems) = preProject
10 | global.json = global.json
11 | EndProjectSection
12 | EndProject
13 | Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "AspNet5SearchWithElasticsearchCrud", "src\AspNet5SearchWithElasticsearchCrud\AspNet5SearchWithElasticsearchCrud.xproj", "{9C1F7B21-1613-4054-AFBD-61D7A4C6EF4A}"
14 | EndProject
15 | Global
16 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
17 | Debug|Any CPU = Debug|Any CPU
18 | Release|Any CPU = Release|Any CPU
19 | EndGlobalSection
20 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
21 | {9C1F7B21-1613-4054-AFBD-61D7A4C6EF4A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
22 | {9C1F7B21-1613-4054-AFBD-61D7A4C6EF4A}.Debug|Any CPU.Build.0 = Debug|Any CPU
23 | {9C1F7B21-1613-4054-AFBD-61D7A4C6EF4A}.Release|Any CPU.ActiveCfg = Release|Any CPU
24 | {9C1F7B21-1613-4054-AFBD-61D7A4C6EF4A}.Release|Any CPU.Build.0 = Release|Any CPU
25 | EndGlobalSection
26 | GlobalSection(SolutionProperties) = preSolution
27 | HideSolutionNode = FALSE
28 | EndGlobalSection
29 | GlobalSection(NestedProjects) = preSolution
30 | {9C1F7B21-1613-4054-AFBD-61D7A4C6EF4A} = {BAA2072E-07B2-4B8F-8D89-51134DB5E7C0}
31 | EndGlobalSection
32 | EndGlobal
33 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # ASP.NET Core Search with ElasticsearchCrud dnxcore50 Elasticsearch 2.0.0
2 |
3 | http://damienbod.com/2015/11/17/using-elasticsearch-with-asp-net-5-dnxcore50/
4 |
--------------------------------------------------------------------------------
/global.json:
--------------------------------------------------------------------------------
1 | {
2 | "projects": [ "src", "test" ],
3 | "sdk": {
4 | "version": "1.0.0-preview2-003121"
5 | }
6 | }
7 |
--------------------------------------------------------------------------------
/src/AspNet5SearchWithElasticsearchCrud/AspNet5SearchWithElasticsearchCrud.xproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 14.0
5 | $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)
6 |
7 |
8 |
9 | 9c1f7b21-1613-4054-afbd-61d7a4c6ef4a
10 | AspNet5SearchWithElasticsearchCrud
11 | ..\..\artifacts\obj\$(MSBuildProjectName)
12 | .\bin\
13 |
14 |
15 | 2.0
16 | 10371
17 |
18 |
19 |
--------------------------------------------------------------------------------
/src/AspNet5SearchWithElasticsearchCrud/Controllers/SearchController.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using Microsoft.AspNetCore.Mvc;
4 | using AspNet5SearchWithElasticsearchCrud.Search;
5 |
6 | namespace AspNet5SearchWithElasticsearchCrud.Controllers
7 | {
8 | [Route("api/[controller]")]
9 | public class SearchController : Controller
10 | {
11 | readonly ISearchProvider _searchProvider;
12 |
13 | public SearchController(ISearchProvider searchProvider)
14 | {
15 | _searchProvider = searchProvider;
16 | }
17 |
18 | [HttpGet("{term}")]
19 | public IEnumerable Search(string term)
20 | {
21 | return _searchProvider.QueryString(term);
22 | }
23 |
24 | [HttpPost("{id}/{name}/{description}")]
25 | public IActionResult Post(long id, string name, string description)
26 | {
27 | _searchProvider.AddUpdateEntity(
28 | new Skill
29 | {
30 | Created = DateTimeOffset.UtcNow,
31 | Description = description,
32 | Name = name,
33 | Id = id
34 | });
35 |
36 | string url = $"api/search/{id}/{name}/{description}";
37 |
38 | return Created(url, id);
39 | }
40 |
41 | [Microsoft.AspNetCore.Mvc.HttpPut("{id}/{updateName}/{updateDescription}")]
42 | public IActionResult Put(long id, string updateName, string updateDescription)
43 | {
44 | _searchProvider.UpdateSkill(id, updateName, updateDescription);
45 |
46 | return Ok();
47 | }
48 |
49 |
50 | // DELETE api/values/5
51 | [Microsoft.AspNetCore.Mvc.HttpDelete("{id}")]
52 | public IActionResult Delete(int id)
53 | {
54 | _searchProvider.DeleteSkill(id);
55 |
56 | return new NoContentResult();
57 | }
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/src/AspNet5SearchWithElasticsearchCrud/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("AspNet5SearchWithElasticsearchCrud")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("AspNet5SearchWithElasticsearchCrud")]
13 | [assembly: AssemblyCopyright("Copyright © 2015")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("9c1f7b21-1613-4054-afbd-61d7a4c6ef4a")]
24 |
--------------------------------------------------------------------------------
/src/AspNet5SearchWithElasticsearchCrud/Properties/launchSettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "iisSettings": {
3 | "windowsAuthentication": false,
4 | "anonymousAuthentication": true,
5 | "iisExpress": {
6 | "applicationUrl": "http://localhost:3795/",
7 | "sslPort": 0
8 | }
9 | },
10 | "profiles": {
11 | "IIS Express": {
12 | "commandName": "IISExpress",
13 | "launchBrowser": true,
14 | "launchUrl": "index.html",
15 | "environmentVariables": {
16 | "ASPNET_ENV": "Development"
17 | }
18 | },
19 | "web": {
20 | "commandName": "web",
21 | "environmentVariables": {
22 | "Hosting:Environment": "Development"
23 | },
24 | "sdkVersion": "dnx-coreclr-win-x86.1.0.0-beta8"
25 | }
26 | }
27 | }
--------------------------------------------------------------------------------
/src/AspNet5SearchWithElasticsearchCrud/Search/ElasticSearchProvider.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 |
5 | using ElasticsearchCRUD;
6 | using ElasticsearchCRUD.Model.SearchModel;
7 | using ElasticsearchCRUD.Model.SearchModel.Queries;
8 |
9 | namespace AspNet5SearchWithElasticsearchCrud.Search
10 | {
11 | public class ElasticSearchProvider : ISearchProvider, IDisposable
12 | {
13 | public ElasticSearchProvider()
14 | {
15 | _context = new ElasticsearchContext(ConnectionString, _elasticSearchMappingResolver);
16 | }
17 |
18 | private const string ConnectionString = "http://localhost:9200/";
19 | private readonly IElasticsearchMappingResolver _elasticSearchMappingResolver = new ElasticsearchMappingResolver();
20 | private readonly ElasticsearchContext _context;
21 |
22 | public IEnumerable QueryString(string term)
23 | {
24 | var results = _context.Search(BuildQueryStringSearch(term));
25 | return results.PayloadResult.Hits.HitsResult.Select(t => t.Source);
26 | }
27 |
28 | /*
29 | {
30 | "query": {
31 | "query_string": {
32 | "query": "*"
33 |
34 | }
35 | }
36 | }
37 | */
38 | private ElasticsearchCRUD.Model.SearchModel.Search BuildQueryStringSearch(string term)
39 | {
40 | var names = "";
41 | if (term != null)
42 | {
43 | names = term.Replace("+", " OR *");
44 | }
45 |
46 | var search = new ElasticsearchCRUD.Model.SearchModel.Search
47 | {
48 | Query = new Query(new QueryStringQuery(names + "*"))
49 | };
50 |
51 | return search;
52 | }
53 |
54 | public void AddUpdateEntity(Skill skill)
55 | {
56 | _context.AddUpdateDocument(skill, skill.Id);
57 | _context.SaveChanges();
58 | }
59 |
60 | public void UpdateSkill(long updateId, string updateName, string updateDescription)
61 | {
62 | var skill = _context.GetDocument(updateId);
63 | skill.Updated = DateTime.UtcNow;
64 | skill.Name = updateName;
65 | skill.Description = updateDescription;
66 | _context.AddUpdateDocument(skill, skill.Id);
67 | _context.SaveChanges();
68 | }
69 |
70 | public void DeleteSkill(long deleteId)
71 | {
72 | _context.DeleteDocument(deleteId);
73 | _context.SaveChanges();
74 | }
75 |
76 | private bool isDisposed;
77 | public void Dispose()
78 | {
79 | if (isDisposed)
80 | {
81 | isDisposed = true;
82 | _context.Dispose();
83 | }
84 | }
85 | }
86 | }
--------------------------------------------------------------------------------
/src/AspNet5SearchWithElasticsearchCrud/Search/ISearchProvider.cs:
--------------------------------------------------------------------------------
1 | namespace AspNet5SearchWithElasticsearchCrud.Search
2 | {
3 | using System.Collections.Generic;
4 |
5 | public interface ISearchProvider
6 | {
7 | IEnumerable QueryString(string term);
8 |
9 | void AddUpdateEntity(Skill skill);
10 | void UpdateSkill(long updateId, string updateName, string updateDescription);
11 | void DeleteSkill(long updateId);
12 | }
13 | }
--------------------------------------------------------------------------------
/src/AspNet5SearchWithElasticsearchCrud/Search/Skill.cs:
--------------------------------------------------------------------------------
1 | namespace AspNet5SearchWithElasticsearchCrud.Search
2 | {
3 | using System;
4 | using System.ComponentModel.DataAnnotations;
5 |
6 | public class Skill
7 | {
8 | [Required]
9 | [Range(1, long.MaxValue)]
10 | public long Id { get; set; }
11 | [Required]
12 | public string Name { get; set; }
13 | [Required]
14 | public string Description { get; set; }
15 | public DateTimeOffset Created { get; set; }
16 | public DateTimeOffset Updated { get; set; }
17 | }
18 | }
--------------------------------------------------------------------------------
/src/AspNet5SearchWithElasticsearchCrud/Startup.cs:
--------------------------------------------------------------------------------
1 | using System.IO;
2 | using Microsoft.AspNetCore.Builder;
3 | using Microsoft.AspNetCore.Hosting;
4 | using Microsoft.Extensions.Configuration;
5 | using Microsoft.Extensions.DependencyInjection;
6 | using Microsoft.Extensions.Logging;
7 | using Microsoft.AspNetCore.Http;
8 | using AspNet5SearchWithElasticsearchCrud.Search;
9 |
10 | namespace AngularPlotlyAspNetCore
11 | {
12 | public class Startup
13 | {
14 | public Startup(IHostingEnvironment env)
15 | {
16 | var builder = new ConfigurationBuilder()
17 | .SetBasePath(env.ContentRootPath)
18 | .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
19 | .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
20 | .AddEnvironmentVariables();
21 | Configuration = builder.Build();
22 | }
23 |
24 | public IConfigurationRoot Configuration { get; set; }
25 |
26 | // This method gets called by the runtime. Use this method to add services to the container.
27 | public void ConfigureServices(IServiceCollection services)
28 | {
29 | //Add Cors support to the service
30 | services.AddCors();
31 |
32 | var policy = new Microsoft.AspNetCore.Cors.Infrastructure.CorsPolicy();
33 |
34 | policy.Headers.Add("*");
35 | policy.Methods.Add("*");
36 | policy.Origins.Add("*");
37 | policy.SupportsCredentials = true;
38 |
39 | services.AddCors(x => x.AddPolicy("corsGlobalPolicy", policy));
40 |
41 | services.AddMvc();
42 |
43 | services.AddScoped();
44 | }
45 |
46 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
47 | public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
48 | {
49 | app.UseCors("corsGlobalPolicy");
50 |
51 | loggerFactory.AddConsole();
52 | loggerFactory.AddDebug();
53 |
54 | app.UseDefaultFiles();
55 |
56 | app.UseStaticFiles();
57 | app.UseMvc();
58 | }
59 |
60 | public static void Main(string[] args)
61 | {
62 | var host = new WebHostBuilder()
63 | .UseKestrel()
64 | .UseContentRoot(Directory.GetCurrentDirectory())
65 | .UseIISIntegration()
66 | .UseStartup()
67 | .Build();
68 |
69 | host.Run();
70 | }
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/src/AspNet5SearchWithElasticsearchCrud/project.json:
--------------------------------------------------------------------------------
1 | {
2 | "dependencies": {
3 | "Microsoft.NETCore.App": {
4 | "version": "1.0.0",
5 | "type": "platform"
6 | },
7 | "Microsoft.AspNetCore.Diagnostics": "1.0.0",
8 | "Microsoft.AspNetCore.Mvc": "1.0.0",
9 | "Microsoft.AspNetCore.Razor.Tools": {
10 | "version": "1.0.0-preview2-final",
11 | "type": "build"
12 | },
13 | "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
14 | "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
15 | "Microsoft.AspNetCore.StaticFiles": "1.0.0",
16 | "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
17 | "Microsoft.Extensions.Configuration.Json": "1.0.0",
18 | "Microsoft.Extensions.Logging": "1.0.0",
19 | "Microsoft.Extensions.Logging.Console": "1.0.0",
20 | "Microsoft.Extensions.Logging.Debug": "1.0.0",
21 | "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0",
22 | "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
23 | "ElasticsearchCrud": "2.3.3.1"
24 | },
25 |
26 | "tools": {
27 | "Microsoft.AspNetCore.Razor.Tools": {
28 | "version": "1.0.0-preview2-final",
29 | "imports": "portable-net45+win8+dnxcore50"
30 | },
31 | "Microsoft.AspNetCore.Server.IISIntegration.Tools": {
32 | "version": "1.0.0-preview2-final",
33 | "imports": "portable-net45+win8+dnxcore50"
34 | }
35 | },
36 |
37 | "frameworks": {
38 | "netcoreapp1.0": {
39 | "imports": [
40 | "dotnet5.6",
41 | "dnxcore50",
42 | "portable-net45+win8"
43 | ]
44 | }
45 | },
46 |
47 | "buildOptions": {
48 | "emitEntryPoint": true,
49 | "preserveCompilationContext": true
50 | },
51 |
52 | "runtimeOptions": {
53 | "gcServer": true
54 | },
55 |
56 | "publishOptions": {
57 | "include": [
58 | "wwwroot",
59 | "Views",
60 | "appsettings.json",
61 | "web.config"
62 | ]
63 | },
64 |
65 | "scripts": {
66 | "prepublish": [ "npm install", "bower install", "gulp clean", "gulp min" ],
67 | "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/src/AspNet5SearchWithElasticsearchCrud/web.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/src/AspNet5SearchWithElasticsearchCrud/wwwroot/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 | App start, use fiddler to test
9 |
10 |
11 | POST: http://localhost:3795/api/search/1/hi/cool
12 |
13 |
14 | POST: http://localhost:3795/api/search/2/hi/cooltwo
15 |
16 |
17 | GET: http://localhost:3795/api/search/hi/
18 |
19 |
--------------------------------------------------------------------------------
/src/AspNet5SearchWithElasticsearchCrud/wwwroot/web.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------