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 »

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.

Review Suggested Edit

You can't approve or reject suggested edits because you haven't yet earned the Edit Posts ability.

Approved.
This suggested edit was approved and applied to the post 2 months ago by Alexei‭.

40 / 255
Unable to use conditional Must term in C# NEST client for Elastic Search
  • Hello, I'm trying to write a Elastic Search query using the C# NEST client, but right now I'm stuck on an issue with a conditional query.
  • The query takes in two optional values, meaning they are both allowed to be omitted, but can also be used together.
  • so i want to be able to search for documents and filter either on:
  • 1 - matching both main and secondary ID
  • 2 - matching only main id
  • 3 - matching only secondary id
  • 4 - no need to match either
  • my understanding was that you could make use of conditionLess queries in ES/Nest, but it does not seems to apply.
  • Using syntax like this:
  • ```
  • m.Term(t => t.Field(f => f.MainId).Value(mainId));
  • ```
  • results in a query containing null values, which fails.
  • So my current attempt is this query, but it only works if all values are provided, and fails to query if some values are missing.
  • ```
  • var query = new SearchRequestDescriptor<EventWithPayload>()
  • .Index("MY-INDEX")
  • .Query(q => q
  • .Bool(b => b
  • .Filter(filter => filter
  • .Bool(fb => fb
  • .Must(m =>
  • {
  • if (mainId is not null)
  • m.Term(t => t.Field(f => f.MainId).Value(mainId));
  • },
  • m =>
  • {
  • if (secondId is not null)
  • m.Term(t => t.Field(f => f.SecondId).Value(secondId));
  • },
  • m =>
  • {
  • m.Terms(ts => ts.Field(f => f.EventName).Term(new TermsQueryField(terms)));
  • }
  • )
  • )
  • )
  • .MustNot(
  • mn => mn.Term(t => t.Field(f => f.ThirdId).Value("NOT_FOUND"))
  • )
  • )
  • );
  • ```
  • if both the first and secondary IDs are provided, the query works as expected, but not if one of the values are omitted.
  • I would like the object null to not be serialised at all, but instead i receive an empty object "{}"
  • resulting JSON query with both values:
  • ```
  • {
  • "query": {
  • "bool": {
  • "filter": {
  • "bool": {
  • "must": [
  • {
  • "term": {
  • "mainId": {
  • "value": "ABC123"
  • }
  • }
  • },
  • {
  • "term": {
  • "secondId": {
  • "value": "DEF456"
  • }
  • }
  • },
  • {
  • "terms": {
  • "eventName": [
  • "A",
  • "B",
  • "C"
  • ]
  • }
  • }
  • ]
  • }
  • },
  • "must_not": {
  • "term": {
  • "thirdId": {
  • "value": "NOT_FOUND"
  • }
  • }
  • }
  • }
  • }
  • }
  • ```
  • JSON query when secondary id is missing:
  • ```
  • {
  • "query": {
  • "bool": {
  • "filter": {
  • "bool": {
  • "must": [
  • {
  • "term": {
  • "mainId": {
  • "value": "ABC123"
  • }
  • }
  • },
  • {},
  • {
  • "terms": {
  • "eventName": [
  • "A",
  • "B",
  • "C"
  • ]
  • }
  • }
  • ]
  • }
  • },
  • "must_not": {
  • "term": {
  • "thirdId": {
  • "value": "NOT_FOUND"
  • }
  • }
  • }
  • }
  • }
  • }
  • ```
  • Any help here would be appreciated, or if there is another, better way to solve the issue I'm facing.
  • I'm trying to write a Elastic Search query using the C# NEST client, but right now I'm stuck on an issue with a conditional query.
  • The query takes in two optional values, meaning they are both allowed to be omitted, but can also be used together.
  • I want to be able to search for documents and filter either on:
  • 1. Matching both main and secondary ID
  • 2. Matching only main ID
  • 3. Matching only secondary ID
  • 4. No need to match either
  • My understanding was that you could make use of conditionless queries in ES/Nest, but it does not seems to apply.
  • Using syntax like this:
  • ```csharp
  • m.Term(t => t.Field(f => f.MainId).Value(mainId));
  • ```
  • results in a query containing null values, which fails.
  • So my current attempt is this query, but it only works if all values are provided, and fails to query if some values are missing.
  • ```csharp
  • var query = new SearchRequestDescriptor<EventWithPayload>()
  • .Index("MY-INDEX")
  • .Query(q => q
  • .Bool(b => b
  • .Filter(filter => filter
  • .Bool(fb => fb
  • .Must(m =>
  • {
  • if (mainId is not null)
  • m.Term(t => t.Field(f => f.MainId).Value(mainId));
  • },
  • m =>
  • {
  • if (secondId is not null)
  • m.Term(t => t.Field(f => f.SecondId).Value(secondId));
  • },
  • m =>
  • {
  • m.Terms(ts => ts.Field(f => f.EventName).Term(new TermsQueryField(terms)));
  • }
  • )
  • )
  • )
  • .MustNot(
  • mn => mn.Term(t => t.Field(f => f.ThirdId).Value("NOT_FOUND"))
  • )
  • )
  • );
  • ```
  • If both the main and secondary IDs are provided, the query works as expected, but not if one of the values are omitted.
  • I would like the object `null` to not be serialised at all, but instead I receive an empty object `{}`.
  • Resulting JSON query with both values:
  • ```json
  • {
  • "query": {
  • "bool": {
  • "filter": {
  • "bool": {
  • "must": [
  • {
  • "term": {
  • "mainId": {
  • "value": "ABC123"
  • }
  • }
  • },
  • {
  • "term": {
  • "secondId": {
  • "value": "DEF456"
  • }
  • }
  • },
  • {
  • "terms": {
  • "eventName": [
  • "A",
  • "B",
  • "C"
  • ]
  • }
  • }
  • ]
  • }
  • },
  • "must_not": {
  • "term": {
  • "thirdId": {
  • "value": "NOT_FOUND"
  • }
  • }
  • }
  • }
  • }
  • }
  • ```
  • JSON query when secondary ID is missing:
  • ```json
  • {
  • "query": {
  • "bool": {
  • "filter": {
  • "bool": {
  • "must": [
  • {
  • "term": {
  • "mainId": {
  • "value": "ABC123"
  • }
  • }
  • },
  • {},
  • {
  • "terms": {
  • "eventName": [
  • "A",
  • "B",
  • "C"
  • ]
  • }
  • }
  • ]
  • }
  • },
  • "must_not": {
  • "term": {
  • "thirdId": {
  • "value": "NOT_FOUND"
  • }
  • }
  • }
  • }
  • }
  • }
  • ```
  • Any help here would be appreciated, or if there is another, better way to solve the issue I'm facing.

Suggested 2 months ago by Michael‭