Skip to content

Instantly share code, notes, and snippets.

@synhershko
Last active December 17, 2015 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save synhershko/5677092 to your computer and use it in GitHub Desktop.
Save synhershko/5677092 to your computer and use it in GitHub Desktop.
NEST Wikipedia search
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"));
}
}
}
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