Created
January 8, 2015 11:25
-
-
Save alkampfergit/342f1f47a498914cb74f to your computer and use it in GitHub Desktop.
Dynamically compose query to ElasticSerach with C# nest
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
/// <summary> | |
/// returns a lambda function that compose query in And or Or | |
/// </summary> | |
/// <param name="parameters"></param> | |
/// <returns></returns> | |
private static Func<QueryContainer, QueryContainer, QueryContainer> GetQueryComposer(QueryTypeEnum queryType) | |
{ | |
Func<QueryContainer, QueryContainer, QueryContainer> composer = (q1, q2) => q1 || q2; | |
if (queryType == QueryTypeEnum.And) | |
composer = (q1, q2) => q1 && q2; | |
return composer; | |
} | |
..... | |
//Start composing query agains a document represented by class IndexedDocument | |
//query is dynamically composed based on values of a value parameters object | |
QueryContainer aQuery = null; | |
var composer = GetQueryComposer(parameters.QueryType); | |
//Standard query | |
if (!String.IsNullOrEmpty(parameters.Title)) | |
aQuery = composer(aQuery, Query<IndexedDocument>.Match( | |
md => md.OnField(id => id.Title).Query(parameters.Title))); | |
//query in multifield type. | |
if (!String.IsNullOrEmpty(parameters.Path)) | |
aQuery = composer(aQuery, Query<IndexedDocument>.Match( | |
md => md.OnField(id => id.Path.Suffix("split")).Query(parameters.Path))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment