Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
Hello, a a late update, mainly to help others facing the problem in the future. it turns out that the latest version of NEST could not handle conditionLess values, and it also fails if you iterate...
Answer
#1: Initial revision
Hello, a a late update, mainly to help others facing the problem in the future. it turns out that the latest version of NEST could not handle conditionLess values, and it also fails if you iterate over possible values, since that would return a empty object - which Elastic won't accept. So instead my solution to the problem was to create separate lists for my different filter conditions, populate with possible values and insert the finished list in the query creation. not in any way pretty code, but it does work, without duplicating JSON attributes all over the place, so its something. Code example: ```csharp public async Task<IEnumerable<DataClass>> GetData(string firstName, string? lastName, string? MiddleName, List<string> eventsFilter, List<string> allowedIds, CancellationToken cancellation = default) { var events = new ReadOnlyCollection<FieldValue>(eventsFilter.ConvertAll(x => (FieldValue)x)); var must = new List<Action<QueryDescriptor<DataClass>>>(); var mustNot = new List<Action<QueryDescriptor<DataClass>>>(); var should = new List<Action<QueryDescriptor<DataClass>>>(); must.Add(q => q.Term(t => t.Field(f => f.FirstName).Value(firstName))); must.Add(q => q.Terms(ts => ts.Field(f => f.EventName).Term(new TermsQueryField(events)))); mustNot.Add(q => q.Term(t => t.Field(f => f.EventPayload!.Fraud).Value(0))); mustNot.Add(q => q.Term(t => t.Field(f => f.EventPayload!.IncorrectTransit).Value(0))); if (lastName is not null) { must.Add(q => q.Term(t => t.Field(f => f.lastName).Value(lastName))); mustNot.Add(q => q.Exists(e => e.Field(f => f.MiddleName))); } if (MiddleName is not null) { must.Add(q => q.Term(t => t.Field(f => f.MiddleName).Value(MiddleName))); mustNot.Add(q => q.Term(t => t.Field(f => f.MyId).Value("NOT_FOUND"))); } should.AddRange(allowedIds.Select(id => (Action<QueryDescriptor<DataClass>>)(q => q.Term(t => t.Field(f => f.MyId).Value(id))))); var query2 = new SearchRequestDescriptor<DeviceEvent>() .Index("MY-INDEX*") .Sort(s => s.Field(f => f.Timestamp, new FieldSort { Order = SortOrder.Desc })) .Query(q => q .Bool(b => b .Filter(filter => filter.Bool(fb => fb .Must(must.ToArray()) .Should(should.ToArray()) .MustNot(mustNot.ToArray()))))); var result = await _client.SearchAsync(query2, cancellation); return result.Documents; } ```