Created
September 5, 2012 20:19
-
-
Save jasondentler/3643995 to your computer and use it in GitHub Desktop.
Raven search
This file contains hidden or 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 Students_ByCourse : AbstractIndexCreationTask<Student> | |
{ | |
public Students_ByCourse() | |
{ | |
Map = students => from student in students | |
select new {student.Course}; | |
Indexes.Add(s => s.Course, FieldIndexing.Analyzed); | |
} | |
} |
This file contains hidden or 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 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