Last active
December 17, 2015 21:48
-
-
Save synhershko/5677092 to your computer and use it in GitHub Desktop.
NEST Wikipedia search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Viewer | |
{ | |
// Note: For instructions on enabling IIS6 or IIS7 classic mode, | |
// visit http://go.microsoft.com/?LinkId=9394801 | |
public class MvcApplication : System.Web.HttpApplication | |
{ | |
public static ElasticClient ESClient { get; private set; } | |
protected void Application_Start() | |
{ | |
AreaRegistration.RegisterAllAreas(); | |
WebApiConfig.Register(GlobalConfiguration.Configuration); | |
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); | |
RouteConfig.RegisterRoutes(RouteTable.Routes); | |
BundleConfig.RegisterBundles(BundleTable.Bundles); | |
ESClient = new ElasticClient(new ConnectionSettings(new Uri("http://localhost:9200")).SetDefaultIndex("pages")); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PagesController : Controller | |
{ | |
// | |
// GET: /Pages/ | |
public ActionResult Search(string q) | |
{ | |
var results = MvcApplication.ESClient.Search<Page>(query => | |
query.Index("myindex") | |
.Type("page") | |
.Query(x => | |
x.Match(y => y.QueryString(q).OnField("content")) | |
|| x.Match(y => y.QueryString(q).OnField("title")) | |
) | |
); | |
if (!results.IsValid || !results.ConnectionStatus.Success) | |
{ | |
// Error handling | |
// may want to also check results.Shards.Failed on a distributed environment | |
} | |
return View(results.Documents); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment