16. SIMPLE EXPRESSION DEMO
> using System.Linq.Expressions;
> Expression<Func<int, int, int>> addExpr = (x, y) => x + y;
> addExpr(1, 3)
Expression ”function description”
(1,1): error CS1955: Non-invocable member 'addExpr' cannot be used like a method.
> var binExpr = addExpr.Body as BinaryExpression;
> Console.WriteLine(binExpr);
(x + y)
> var add2 = addExpr.Compile();
> add2(3, 1);
4
17. MORE COMPLEX EXPRESSION DEMO
> public class SomeClass { public string MyString { get; set; } }
> Expression<Func<SomeClass, object>> propExpr = y => y.MyString + y.MyString;
> var compExpr = propExpr.Compile();
> var obj = new SomeClass { MyString = "Hello world" };
> compExpr(obj)
Hello worldHello world
> var body = propExpr.Body as BinaryExpression;
> Console.WriteLine(body);
(y.MyString + y.MyString)
> var left = body.Left as MemberExpression;
> Console.WriteLine(left.Member.Name);
MyString
18. MORE COMPLEX EXPRESSION DEMO
> public class SomeClass { public string MyString { get; set; } }
> Expression<Func<SomeClass, object>> propExpr = y => y.MyString + y.MyString;
> var compExpr = propExpr.Compile();
> var obj = new SomeClass { MyString = "Hello world" };
> compExpr(obj)
Hello worldHello world
> var body = propExpr.Body as BinaryExpression;
Enables us to translate from one domain to another in an ”easy” manner
> Console.WriteLine(body);
(y.MyString + y.MyString)
> var left = body.Left as MemberExpression;
> Console.WriteLine(left.Member.Name);
MyString
20. ELASTICSEARCH CONNECTION
public class ElasticClientWrapper : ElasticClient
{
private static string _connectionString = Settings.ElasticSearchServer;
private static ConnectionSettings _settings =
new ConnectionSettings(new Uri(_connectionString)) //http://demoserver:9200
.SetDefaultIndex(Settings.Alias) //"customer_product_mapping"
.UsePrettyResponses();
public ElasticClientWrapper()
: base(_settings)
{
}
}
//usage
var client = new ElasticClientWrapper();
21. MAPPING
public class Product
{
public double UnitPrice { get; set; }
public int TotalQuantity { get; set; }
[ElasticProperty(Index = FieldIndexOption.not_analyzed)]
public string ProductName { get; set; }
[ElasticProperty(Index = FieldIndexOption.not_analyzed)]
public string CategoryName { get; set; }
}
public class Customer
{
public string CustomerID { get; set; }
public string CompanyName { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string Country { get; set; }
[ElasticProperty(Type = FieldType.nested)]
public Product[] Products { get; set; }
}
22. MAPPING & INDEXING
_client = new ElasticClientWrapper();
Mapping created from attributes
_client.CreateIndex("indexName", s =>
s.AddMapping<Customer>(m => m.MapFromAttributes()));
var customers = _customerRepo.GetCustomers();
_client.IndexMany(customers, "indexName");
Indexing will use the mapping
for the specified index
There is async versions of the
methods
23. ALIAS
_client = new ElasticClientWrapper();
_client.Alias("indexName", "aliasName");
Alias
Index_01
24. SWAPPING
_client = new ElasticClientWrapper();
_client.Swap("aliasName", new [] { "Index_01" }, new [] { "Index_02" } );
Alias
1.
Index_02
Swap
3.
Index_01
Create new index
2.
Alias
Delete old index
25. MY QUERY OBJECT (WILL BE USED IN THE EXAMPLES)
public class SearchModel
{
private int? _numberToTake;
public string Query { get; set; }
public Dictionary<string, IEnumerable<string>> Filter { get; set; }
public int? NumberToTake
{
get { return _numberToTake.HasValue ? _numberToTake.Value : 25; }
set { _numberToTake = value; }
}
}
38. FILTERS – NEST – PART 1, THE MAGIC DICTIONARY
public Dictionary<string, Func<IEnumerable<string>, BaseFilter>> FilterDesc =
new Dictionary<string, Func<IEnumerable<string>, BaseFilter>>()
{
{"products.productName", ps => AddProductsFilter(ps, c => c
.Products[0].ProductName)},
{"products.categoryName", cs => AddProductsFilter(cs, c => c
.Products[0].CategoryName)},
{"country", cs => AddCustomerFilter(cs, c => c.Country)}
};
39. FILTERS – NEST – PART 1, ALL THE HELPERS
private static BaseFilter AddCustomerFilter(IEnumerable<string> items,
Expression<Func<Customer, object>> propExpr)
{
return Filter<Customer>.Terms(propExpr, items.ToArray());
}
private static BaseFilter AddProductsFilter(IEnumerable<string> items,
Expression<Func<Customer, object>> propExpr)
{
return Filter<Customer>.Nested(sel => sel
.Path(c => c.Products)
.Query(q => q.Terms(propExpr, items.ToArray())));
}
public Dictionary<string, Func<IEnumerable<string>, BaseFilter>> FilterDesc =
new Dictionary<string, Func<IEnumerable<string>, BaseFilter>>()
{
{"products.productName", ps => AddProductsFilter(ps, c => c
.Products[0].ProductName)},
{"products.categoryName", cs => AddProductsFilter(cs, c => c
.Products[0].CategoryName)},
{"country", cs => AddCustomerFilter(cs, c => c.Country)}
};
40. FILTERS – NEST – PART 2, THE QUERY
_client.Search<Customer>(sd => sd
.Query(q => q
.Filtered(fq =>
{
fq.Query(qs =>
{
if (!string.IsNullOrEmpty(Input.Query))
{
qs.Bool( /* See previous example */ ));
}
else
{
qs.MatchAll();
}
return qs;
});
if (Input.Filter.Count > 0)
{
var filters =
Input.Filter.Select(_ => FilterDesc[_.Key](_.Value)).ToArray();
fq.Filter(fs => fs.Bool(bf => bf.Must(filters)));
}
}))
.Highlight( /* See previous example */ )
.FacetTerm( /* See previous example */ )
.FacetTerm( /* See previous example */ )
.FacetTerm( /* See previous example */ );
C:\Dev\git> scriptcsscriptcs (ctrl-c or blank to exit)> usingSystem.Linq.Expressions;> Func<int, int, int> add = (x, y) => x + y;> add(1, 3)4> Expression<Func<int, int, int>> addExpr = (x, y) => x + y;> addExpr(1, 3)(1,1): error CS1955: Non-invocablemember 'addExpr' cannot be used like a method.> var body = addExpr.Body as BinaryExpression;> Console.WriteLine(body);(x + y)
C:\Dev\git> scriptcsscriptcs (ctrl-c or blank to exit)> usingSystem.Linq.Expressions;> Func<int, int, int> add = (x, y) => x + y;> add(1, 3)4> Expression<Func<int, int, int>> addExpr = (x, y) => x + y;> addExpr(1, 3)(1,1): error CS1955: Non-invocablemember 'addExpr' cannot be used like a method.> var body = addExpr.Body as BinaryExpression;> Console.WriteLine(body);(x + y)
C:\Dev\git> scriptcsscriptcs (ctrl-c or blank to exit)> usingSystem.Linq.Expressions;> Func<int, int, int> add = (x, y) => x + y;> add(1, 3)4> Expression<Func<int, int, int>> addExpr = (x, y) => x + y;> addExpr(1, 3)(1,1): error CS1955: Non-invocablemember 'addExpr' cannot be used like a method.> var body = addExpr.Body as BinaryExpression;> Console.WriteLine(body);(x + y)
C:\Dev\git> scriptcsscriptcs (ctrl-c or blank to exit)> usingSystem.Linq.Expressions;> Func<int, int, int> add = (x, y) => x + y;> add(1, 3)4> Expression<Func<int, int, int>> addExpr = (x, y) => x + y;> addExpr(1, 3)(1,1): error CS1955: Non-invocablemember 'addExpr' cannot be used like a method.> var body = addExpr.Body as BinaryExpression;> Console.WriteLine(body);(x + y)
C:\Dev\git> scriptcsscriptcs (ctrl-c or blank to exit)> usingSystem.Linq.Expressions;> Func<int, int, int> add = (x, y) => x + y;> add(1, 3)4> Expression<Func<int, int, int>> addExpr = (x, y) => x + y;> addExpr(1, 3)(1,1): error CS1955: Non-invocablemember 'addExpr' cannot be used like a method.> var body = addExpr.Body as BinaryExpression;> Console.WriteLine(body);(x + y)