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
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 all...
Question
.net
#3: Post edited
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 ID2 - matching only main id3 - matching only secondary id4 - no need to match eithermy 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.
#1: Initial revision
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.