Last active
December 21, 2015 14:38
-
-
Save gondar/6320578 to your computer and use it in GitHub Desktop.
Comparing different count methods in ElasticSearch.
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.Generic; | |
using System.Linq; | |
using ElasticSearchCountExperiment.RealDataExperiments; | |
using PlainElastic.Net; | |
using PlainElastic.Net.Queries; | |
using PlainElastic.Net.Serialization; | |
using PlainElastic.Net.Utils; | |
namespace ElasticSearchCountExperiment | |
{ | |
class Log | |
{ | |
public string LogName { get; set; } | |
public int TermId { get; set; } | |
public string RestaurantName { get; set; } | |
public DateTime DateTime { get; set; } | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var connection = new ElasticConnection("localhost", 9200); | |
var serializer = new JsonNetSerializer(); | |
PopulateDatabase(connection, serializer); | |
//var counts = GetCount(connection, serializer); | |
//var counts = GetCountSearch(connection, serializer); | |
var counts = GetCountAll(connection, serializer); | |
foreach (var c in counts) | |
{ | |
Console.WriteLine("Category: {0} Items Count: {1}".F(c.Item1, c.Item2)); | |
} | |
Console.ReadKey(); | |
connection.Delete(Commands.Delete("store", "item")); | |
} | |
private static List<Tuple<string, long>> GetCount(ElasticConnection connection, JsonNetSerializer serializer) | |
{ | |
string query = new CountBuilder<Log>().Query(qry => qry | |
.Field(fd => fd | |
.Field(fdd => fdd.RestaurantName) | |
.Query("Restaurant Charlie") | |
.DefaultOperator(Operator.AND)) | |
).Build(); | |
string result = connection.Post(Commands.Count("store", "item"), query); | |
var searchResults = serializer.ToCountResult(result); | |
return new List<Tuple<string, long>> | |
{ | |
new Tuple<string, long>("Restaurant Charlie", searchResults.count) | |
}; | |
} | |
private static List<Tuple<string, long>> GetCountSearch(ElasticConnection connection, JsonNetSerializer serializer) | |
{ | |
string query = new QueryBuilder<Log>().Query(q => q | |
.Field(fd => fd | |
.Field(fdd => fdd.RestaurantName) | |
.Query("Restaurant Charlie") | |
.DefaultOperator(Operator.AND)) | |
).Build(); | |
string result = connection.Post(Commands.Search("store", "item").SearchType(SearchType.count), query); | |
// Parse facets query result | |
var searchResults = serializer.ToSearchResult<Log>(result); | |
return new List<Tuple<string, long>> | |
{ | |
new Tuple<string, long>("Charlie", searchResults.hits.total) | |
}; | |
} | |
private static List<Tuple<string,long>> GetCountAll(ElasticConnection connection, JsonNetSerializer serializer) | |
{ | |
string query = new QueryBuilder<Log>() | |
.Query(q => q | |
.MatchAll() | |
) | |
.Facets(facets => facets | |
.Terms(t => t | |
.FacetName("MyFacet") | |
.Field(item => item.TermId) | |
.Size(100) | |
) | |
) | |
.BuildBeautified(); | |
string result = connection.Post(Commands.Search("store", "item").SearchType(SearchType.count), query); | |
var searchResults = serializer.ToSearchResult<Log>(result); | |
var itemsPerCategoryTerms = searchResults.facets.Facet<TermsFacetResult>("MyFacet").terms.Select(x=>new Tuple<string,long>(x.term,x.count)); | |
return itemsPerCategoryTerms.ToList(); | |
} | |
private static void PopulateDatabase(IElasticConnection connection, IJsonSerializer serializer) | |
{ | |
var selectedCharlie = new Log | |
{ | |
LogName = "Result-Selected", | |
RestaurantName = "Restaurant Charlie", | |
TermId = 1, | |
DateTime = DateTime.Now | |
}; | |
connection.Put(Commands.Index("store", "item", id: 1.AsString()).Refresh(), serializer.Serialize(selectedCharlie)); | |
connection.Put(Commands.Index("store", "item", id: 2.AsString()).Refresh(), serializer.Serialize(selectedCharlie)); | |
connection.Put(Commands.Index("store", "item", id: 3.AsString()).Refresh(), serializer.Serialize(selectedCharlie)); | |
var selectedEla = new Log | |
{ | |
LogName = "Result-Selected", | |
RestaurantName = "Restaurant Ela", | |
TermId = 2, | |
DateTime = DateTime.Now | |
}; | |
connection.Put(Commands.Index("store", "item", id: 4.AsString()).Refresh(), serializer.Serialize(selectedEla)); | |
connection.Put(Commands.Index("store", "item", id: 5.AsString()).Refresh(), serializer.Serialize(selectedEla)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment