Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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

60%
+1 −0
Q&A Unable to use conditional Must term in C# NEST client for Elastic Search

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...

posted 1mo ago by hest‭

Answer
#1: Initial revision by user avatar hest‭ · 2024-10-08T11:26:08Z (about 1 month ago)
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;

        }

```