# Elastic Search .Net Combine Must and Should Showing All Queries rather than correct combination

**URL:** https://discuss.elastic.co/t/elastic-search-net-combine-must-and-should-showing-all-queries-rather-than-correct-combination/370852
**Category:** Elasticsearch
**Tags:** language-clients
**Created:** [November 20, 2024, 5:09pm UTC](https://discuss.elastic.co/t/elastic-search-net-combine-must-and-should-showing-all-queries-rather-than-correct-combination/370852 "2024-11-20T17:09:41Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![GaryBtP](https://avatars.discourse-cdn.com/v4/letter/g/ce73a5/32.png) [@GaryBtP](https://discuss.elastic.co/u/GaryBtP)
#### Post date: [November 20, 2024, 5:09pm UTC](https://discuss.elastic.co/t/elastic-search-net-combine-must-and-should-showing-all-queries-rather-than-correct-combination/370852/1 "2024-11-20T17:09:41Z")

</div>

I'm struggling to generate a .Net search which will combine a must field of one value and a should search of one of two values from a different field - e.g. results must contain value1 from field1 and either value2 or value3 from field2.

I have successfully created that search in Elastic Search. See below:-

```auto
GET stagingstock/_search
{
  "size" : 1000,
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "field1": {
              "query": "value1",
              "operator": "and",
              "fuzziness": "auto"
            }    
          }
        },
        {"bool": {
      "should": [
        {
          "match": {
            "field2": {
              "query": "value2",
              "operator": "or",
              "fuzziness": "auto"
            }    
          }
        },
        {
          "match": {
            "field2": {
              "query": "value3",
              "operator": "or",
              "fuzziness": "auto"
            }    
          }
        }
      ]
          
        }}
      ]
    }
  },
  "sort": [
    {
      "updatedDate": {
        "order": "desc"
      }
    }
  ]
}

```

But when I try to parse that query into .Net it is returning either value1, value2 or value3. Code below:

```auto
resp2 = await client.SearchAsync<ElasticStock>(s => s
	.Index(index)
	.Size(10000)
	.Query(q => q.Bool(
		b => b.Must(
			mq => mq.Match(
				m => m.Field(
					f => f.Field1)
						.Fuzziness(fuzz)
						.Query(value1)
						.Operator(Operator.And)
					).Bool(
						b2 => b2.Should(
							sq => sq.Match(m2 => m2.Field(f => f.Field2).Query(value2)),
							sq => sq.Match(m2 => m2.Field(f => f.Field2).Query(value3))
						)
					)
				)
			)
		)
	.Sort(so => so
		.Field(f => f.UpdatedDate, new FieldSort { Order = SortOrder.Desc }))
);

```

Can someone tell me what I am doing wrong in .Net as the logic seems the same to me?

Note: I removed the Or Operators in the .Net code but that didn't make any difference to the search.

---

<div class="post-metadata">

### Author: ![flobernd](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flobernd/32/124877_2.png) [@flobernd](https://discuss.elastic.co/u/flobernd)
#### Post date: [November 21, 2024, 11:34am UTC](https://discuss.elastic.co/t/elastic-search-net-combine-must-and-should-showing-all-queries-rather-than-correct-combination/370852/2 "2024-11-21T11:34:45Z")

</div>

Hi @GaryBtP ,

sorry, please ignore this part. I did not read your JSON payload correctly due to the strange indentation 😜

~~IMO your original query is already incorrect. A "bool" query with both "must" and "should" does not achieve the desired result. It only matches the "must" parts and optionally boost query score/relevance depending on the "should" part.~~

You want to nest "bool" queries to achieve a strict `if (a == x && (b == y || b == z))`:

```csharp
var resp2 = await client.SearchAsync<ElasticStock>(s => s
    .Index(index)
    .Size(10000)
    .Query(q => q
        .Bool(
            b => b
                .Must(
                    mq => mq.Match(m => m
                        .Field(f => f.Field1)
                        .Fuzziness(new Fuzziness("auto"))
                        .Query("value1")
                        .Operator(Operator.And)
                    ),
                    mq => mq.Bool(b => b
                        .Should(
                            sq => sq.Match(m2 => m2.Field(f => f.Field2).Query("value2")),
                            sq => sq.Match(m2 => m2.Field(f => f.Field2).Query("value3"))
                        )
                    )
                )
            )
    )
    .Sort(so => so
        .Field(f => f.UpdatedDate, new FieldSort { Order = SortOrder.Desc })
    )
);

```

I tried to visualize the query DSL syntax tree as an ASCII art. Hope this helps 🙂

```auto
// (Field1 == value1 && (Field2 == value2 || Field2 == value3))
// ^ ^ ^ ^ ^ ^ ^ ^^
// : : : : : : : ::. outer bool query end
// : : : : : : : :. inner bool query end
// : : : : : : :. match query
// : : : : : :. inner bool query "should"
// : : : : :. match query
// : : : :. inner bool query start
// : : :. outer bool query "must"
// : :. match query
// :. outer bool query start

```

---

<div class="post-metadata">

### Author: ![GaryBtP](https://avatars.discourse-cdn.com/v4/letter/g/ce73a5/32.png) [@GaryBtP](https://discuss.elastic.co/u/GaryBtP)
#### Post date: [November 21, 2024, 12:22pm UTC](https://discuss.elastic.co/t/elastic-search-net-combine-must-and-should-showing-all-queries-rather-than-correct-combination/370852/3 "2024-11-21T12:22:27Z")

</div>

Thanks. That seems to have worked. I thought by putting the Bool inside the first match I was doing that but clearly not. I did notice the difference between Elasticsearch and .Net was the comma but didn't realise how big of a difference that made.

Greatly appreciate the help.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [December 19, 2024, 12:22pm UTC](https://discuss.elastic.co/t/elastic-search-net-combine-must-and-should-showing-all-queries-rather-than-correct-combination/370852/4 "2024-12-19T12:22:37Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
