File tree Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change 1+ ---
2+ template : layout.jade
3+ title : Tips & Tricks
4+ menusection :
5+ menuitem : tips-tricks
6+ ---
7+
8+ # Tips & Tricks
9+
10+ This page lists some general tips and tricks provided by the community
11+
12+ ## Posting Elasticsearch queries from javascript
13+
14+ Consider a scenario where you are using client side libraries like [ elasticjs] ( https://github.com/fullscale/elastic.js )
15+ but want security to be provided by server side business logic. Consider this example using ` WebAPI `
16+
17+ ** NOTE** make sure dynamic scripting is turned off if you decide to open the full query DSL to the client!
18+
19+ ``` cs
20+ [RoutePrefix (" api/Search" )]
21+ public class SearchController : ApiController
22+ {
23+ [ActionName (" _search" )]
24+ public IHttpActionResult Post ([FromBody ]SearchDescriptor <dynamic > query )
25+ {;
26+ var client = new ElasticClient ();
27+
28+ // Your server side security goes here
29+
30+ var result = client .Search (q => query );
31+ return Ok (result );
32+ }
33+ }
34+ ```
35+ The fragments ` [RoutePrefix("api/Search")] ` and ` [ActionName("_search")] ` will let you change your elastic search Url
36+ from ` http://localhost:9200/_search ` to ` http://yourwebsite/api/Search/_search ` and let things work as normal.
37+ The fragment ` [FromBody]SearchDescriptor<dynamic> query ` will convert the JSON query into NEST SearchDescriptor.
38+ The fragment ` client.Search(q => query) ` will execute the query.
Original file line number Diff line number Diff line change @@ -24,6 +24,7 @@ aside#menu
2424 li: a( class =(page .metadata .menuitem == ' index-type-inference' )? " selected" : "" , href ="/nest/index-type-inference.html" ) Type/Index Inference
2525 li: a( class =(page .metadata .menuitem == ' handling-responses' )? " selected" : "" , href ="/nest/handling-responses.html" ) Handling responses
2626 li: a( class =(page .metadata .menuitem == ' writing-queries' )? " selected" : "" , href ="/nest/writing-queries.html" ) Writing queries
27+ li: a( class =(page .metadata .menuitem == ' tips-tricks' )? " selected" : "" , href ="/nest/tips-tricks.html" ) Tips & Tricks
2728 - if (page .metadata .menusection == ' core' )
2829 h4
2930 i.fa.fa-chevron-down
You can’t perform that action at this time.
0 commit comments