Created
July 31, 2015 15:09
-
-
Save mat-mcloughlin/1a8c5734a064b77edcdb to your computer and use it in GitHub Desktop.
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
static void Main(string[] args) | |
{ | |
////////// Setup Elastic | |
var node = new Uri("http://localhost:9200"); | |
var settings = | |
new ConnectionSettings(node).SetDefaultIndex("fansz") | |
.MapDefaultTypeNames(m => m.Add(typeof(UserElastic), "users")); | |
var client = new ElasticClient(settings); | |
client.CreateIndex(c => c | |
.Index("users") | |
.Analysis(an => an | |
.Analyzers(a => a | |
.Add("email-analyzer", new CustomAnalyzer | |
{ | |
Tokenizer = "keyword", Filter = new[] { "lowercase" } | |
}))) | |
.AddMapping<UserElastic>(m => m.Properties(u => u.String(s => s.Name(d => d.Email).Analyzer("email-analyzer"))))); | |
////////// Load Data | |
var storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString); | |
var tableClient = storageAccount.CreateCloudTableClient(); | |
var table = tableClient.GetTableReference("Users"); | |
table.CreateIfNotExists(); | |
TableContinuationToken token = null; | |
var entities = new List<User>(); | |
do | |
{ | |
var queryResult = table.ExecuteQuerySegmented<User>(new TableQuery<User>(), token); | |
entities.AddRange(queryResult.Results); | |
token = queryResult.ContinuationToken; | |
} | |
while (token != null); | |
foreach (var entity in entities) | |
{ | |
client.Index(Map(entity)); | |
} | |
////////// Query that shit! | |
var searchResults = client.Search<UserElastic>(s => s | |
.Query(q => q.Terms(m => m.Phone, new List<string> { "rogers_bashirian@kesslerhirthe.name" }))); | |
foreach (var searchResult in searchResults.Documents) | |
{ | |
Console.WriteLine(searchResult.Email); | |
} | |
Console.WriteLine("done"); | |
Console.ReadLine(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment