Last active
March 17, 2017 11:45
-
-
Save PureKrome/5524046 to your computer and use it in GitHub Desktop.
Sample AutoComplete with RavenDb.
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
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Linq; | |
using Raven.Abstractions.Indexing; | |
using Raven.Client; | |
using Raven.Client.Document; | |
using Raven.Client.Embedded; | |
using Raven.Client.Indexes; | |
using WorldDomination.Raven.Client; | |
namespace ConsoleApplication6 | |
{ | |
internal class Program | |
{ | |
private static IDocumentStore _documentStore; | |
private static IDocumentStore DocumentStore | |
{ | |
get | |
{ | |
if (_documentStore == null) | |
{ | |
//var documentStore = new DocumentStore | |
// { | |
// Url = "http://localhost:8080", | |
// DefaultDatabase = "AutoComplete" | |
// }; | |
var documentStore = new EmbeddableDocumentStore | |
{ | |
RunInMemory = true | |
}; | |
// Add default indexes and fake data. | |
var indexesToExecute = new List<Type> {typeof (Locations_AutoComplete)}; | |
documentStore.InitializeWithDefaults(new List<IEnumerable> | |
{ | |
FakeLocations, | |
FakeCrimes | |
}, | |
indexesToExecute); | |
_documentStore = documentStore; | |
} | |
return _documentStore; | |
} | |
} | |
private static IEnumerable<Location> FakeLocations | |
{ | |
get | |
{ | |
return new List<Location> | |
{ | |
new Location {Name = "Carnegie Hill"}, | |
new Location {Name = "Gramercy Park"}, | |
new Location {Name = "Upper West Side"}, | |
new Location {Name = "Central Park"}, | |
new Location {Name = "Lenox Hill"}, | |
new Location {Name = "Lincoln Square"}, | |
new Location {Name = "West Village"}, | |
new Location {Name = "YorkVille"}, | |
new Location {Name = "Upper East Side"}, | |
new Location {Name = "Greenwich Village"}, | |
new Location {Name = "Chelsea"}, | |
new Location {Name = "East Village"}, | |
new Location {Name = "Bowery"}, | |
new Location {Name = "Soho"}, | |
new Location {Name = "Tribeca"} | |
}; | |
} | |
} | |
private static IEnumerable<Crime> FakeCrimes | |
{ | |
get | |
{ | |
return new List<Crime> | |
{ | |
new Crime | |
{ | |
LocationId = "Locations/1", | |
Offense = OffenseType.Theft, | |
Details = "Theft of a bike." | |
}, | |
new Crime | |
{ | |
LocationId = "Locations/1", | |
Offense = OffenseType.Theft, | |
Details = "Kid stole bread." | |
}, | |
new Crime | |
{ | |
LocationId = "Locations/1", | |
Offense = OffenseType.Stabbing, | |
Details = "Man stabbed in butt cheek." | |
}, | |
new Crime | |
{ | |
LocationId = "Locations/2", | |
Offense = OffenseType.Theft, | |
Details = "Stollen mail." | |
}, | |
new Crime | |
{ | |
LocationId = "Locations/3", | |
Offense = OffenseType.Theft, | |
Details = "Lamppost stollen." | |
}, | |
new Crime | |
{ | |
LocationId = "Locations/3", | |
Offense = OffenseType.Murder, | |
Details = "Cat murdered. Hit and run." | |
}, | |
new Crime | |
{ | |
LocationId = "Locations/4", | |
Offense = OffenseType.CarJacking, | |
Details = "Blue car car-jacked." | |
}, | |
new Crime | |
{ | |
LocationId = "Locations/5", | |
Offense = OffenseType.CarJacking, | |
Details = "Red car car-jacked." | |
} | |
}; | |
} | |
} | |
private static void Main() | |
{ | |
var documentSession = DocumentStore.OpenSession(); | |
// No results found. | |
AutoComplete("asdadsdsa", documentSession); | |
// Results. | |
AutoComplete("hill", documentSession); | |
AutoComplete("tribeca", documentSession); | |
AutoComplete("village", documentSession); | |
// Cleanup. | |
documentSession.Dispose(); | |
DocumentStore.Dispose(); | |
} | |
private static void AutoComplete(string query, IDocumentSession documentSession) | |
{ | |
var results = documentSession | |
.Query<Locations_AutoComplete.Result, Locations_AutoComplete>() | |
.Search(x => x.Name, query) | |
.ToList(); | |
DisplayResults(query, results); | |
} | |
private static void DisplayResults(string query, IList<Locations_AutoComplete.Result> results) | |
{ | |
Console.WriteLine("---------------------------------------------------------------"); | |
Console.WriteLine("- Query: " + query); | |
if (results == null || | |
!results.Any()) | |
{ | |
Console.WriteLine(" *** No results."); | |
} | |
else | |
{ | |
foreach (var result in results) | |
{ | |
Console.WriteLine(" - " + result.Name); | |
} | |
} | |
} | |
public class Locations_AutoComplete : AbstractMultiMapIndexCreationTask<Locations_AutoComplete.Result> | |
{ | |
public Locations_AutoComplete() | |
{ | |
AddMap<Location>(locations => | |
from location in locations | |
select new Result | |
{ | |
Name = location.Name | |
}); | |
// I'm guessing we'll be doing a REDUCE later, so i just hacked | |
// up an TransformResults so the other code that uses this index, | |
// is already coded :P | |
TransformResults = (database, results) => from r in results | |
select new Result | |
{ | |
Name = r.Name | |
}; | |
Index(x => x.Name, FieldIndexing.Analyzed); | |
} | |
public class Result | |
{ | |
public string Name { get; set; } | |
} | |
} | |
#region Models | |
private class Crime | |
{ | |
public Crime() | |
{ | |
CreatedOn = DateTime.UtcNow; | |
} | |
public string Id { get; set; } | |
public OffenseType Offense { get; set; } | |
public string Details { get; set; } | |
public string LocationId { get; set; } | |
public DateTime CreatedOn { get; private set; } | |
} | |
private class Location | |
{ | |
public string Id { get; set; } | |
public string Name { get; set; } | |
} | |
private enum OffenseType | |
{ | |
Unknown, | |
Theft, | |
Murder, | |
CarJacking, | |
Stabbing | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment