Skip to content

Instantly share code, notes, and snippets.

@jasondentler
Created September 5, 2012 20:19
Show Gist options
  • Save jasondentler/3643995 to your computer and use it in GitHub Desktop.
Save jasondentler/3643995 to your computer and use it in GitHub Desktop.
Raven search
public class Students_ByCourse : AbstractIndexCreationTask<Student>
{
public Students_ByCourse()
{
Map = students => from student in students
select new {student.Course};
Indexes.Add(s => s.Course, FieldIndexing.Analyzed);
}
}
public ActionResult Search(string query)
{
using (var documentSession = MvcApplication.DocumentStore.OpenSession())
{
RavenQueryStatistics stats;
var ravenQuery = documentSession.Query<Student, Students_ByCourse>()
.Statistics(out stats)
.Search(x => x.Course, query);
var results = ravenQuery.ToList();
if (stats.TotalResults == 0)
{
var suggestions = ravenQuery.Suggest();
return Json(suggestions, JsonRequestBehavior.AllowGet);
}
return Json(results, JsonRequestBehavior.AllowGet);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment