# Issue with CompletionSuggester query using DotNet Elastic.Clients.Elasticsearch

**URL:** <https://discuss.elastic.co/t/issue-with-completionsuggester-query-using-dotnet-elastic-clients-elasticsearch/374959>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [February 24, 2025, 6:50pm UTC](https://discuss.elastic.co/t/issue-with-completionsuggester-query-using-dotnet-elastic-clients-elasticsearch/374959 "2025-02-24T18:50:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Reed\_M](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/reed_m/32/141606_2.png) [@Reed\_M](https://discuss.elastic.co/u/Reed_M)\
**Post date:** [February 24, 2025, 6:50pm UTC](https://discuss.elastic.co/t/issue-with-completionsuggester-query-using-dotnet-elastic-clients-elasticsearch/374959/1 "2025-02-24T18:50:44Z")

</div>

I am trying to query the CompletionSuggester using Elastic.Clients.Elasticsearch. I am able to query with the following (use either prefix OR regex gets the right result):

```auto
POST customer-catalog-test-0001/_search
{
  "_source": false,
  "suggest": {
    "product-suggest": {
      "prefix": "male",
      "regex": ".*male.*",
      "completion": {
        "field": "autoCompleteSuggest",
        "skip_duplicates": true,
        "size": 5
      }
    }
  }
}

```

BUT when I am converting it to DotNet Elastic.Clients.Elasticsearch (v8.17), in the CompletionSuggester class, there is not `Prefix` field, only `Regex` field that takes `Elastic.Clients.Elasticsearch.Core.Search.RegexOptions`. However, I cannot set the "expression" for the `RegexOptions`. This is what the `RegexOptions` like:

```auto
public sealed partial class RegexOptions
{
	/// <summary>
	/// <para>
	/// Optional operators for the regular expression.
	/// </para>
	/// </summary>
	[JsonInclude, JsonPropertyName("flags")]
	public object? Flags { get; set; }

	/// <summary>
	/// <para>
	/// Maximum number of automaton states required for the query.
	/// </para>
	/// </summary>
	[JsonInclude, JsonPropertyName("max_determinized_states")]
	public int? MaxDeterminizedStates { get; set; }
}

```

My current code:

```auto
public async void QuerySuggest()
{
    var client = new ElasticsearchClient(new ElasticsearchClientSettings(new Uri("http://localhost:9200")));

    var searchRequest = new SearchRequest("customer-catalog-test-0001")
    {
        // Exclude the _source
        Source = new SourceConfig(false),

        // Define the suggester
        Suggest = new Suggester
        {
            Suggesters =
            {
                {
                    "product-suggest",
                    new CompletionSuggester
                    {
                        Field = "autoCompleteSuggest",
                        Size = 5,
                        SkipDuplicates = true,
                        Regex = new RegexOptions
                        {
                            Value = "male.*"
                        }
                    }
                }
            }
        }
    };
}

```

So..., am I not doing it right OR there is something wrong with CompletionSuggester/RegexOptions defination?

---

<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:** [February 25, 2025, 7:59am UTC](https://discuss.elastic.co/t/issue-with-completionsuggester-query-using-dotnet-elastic-clients-elasticsearch/374959/2 "2025-02-25T07:59:50Z")

</div>

Hi @Reed_M ,

you got tricked by implicit conversion operators here 😕

`Suggesters` has a `Dictionary<string, FieldSuggester>` type. `FieldSuggester` is implicitly constructible from `CompletionSuggester` which is why your code compiles.

To achieve the regex/match lookup, you might use this code instead:

```csharp
var suggester = FieldSuggester.Completion(new CompletionSuggester
{
	Field = "autoCompleteSuggest",
	Size = 5,
	SkipDuplicates = true,
					
});

suggester.Prefix = "match";
// ...

var searchRequest = new SearchRequest("customer-catalog-test-0001")
{
	// Exclude the _source
	Source = new SourceConfig(false),

	// Define the suggester
	Suggest = new Suggester
	{
		Suggesters =
		{
			{
				"product-suggest",
				suggester
			}
		}
	}
};

```

I'm going to improve this syntax a little bit for the next major version. The static factory method will be replaced by simple properties. This will at least allow you to initialize everything inline like this:

```csharp
Suggesters =
{
    {
        "product-suggest",
        new FieldSuggester
        {
            Completion = new CompletionSuggester
            {
                Field = "autoCompleteSuggest",
                Size = 5,
                SkipDuplicates = true,			
            },
            Prefix = "match",
            // ...
        }
    }
}

```

I created an explanatory screenshot that hopefully demonstrates how these "container" types work to avoid confusion in the future:

 ![1818-1037-max](https://us1.discourse-cdn.com/elastic/original/3X/1/d/1da8790ba974865f3f616aa160dff4a132114ce1.png)

---

<div class="post-metadata">

**Author:** ![Reed\_M](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/reed_m/32/141606_2.png) [@Reed\_M](https://discuss.elastic.co/u/Reed_M)\
**Post date:** [February 25, 2025, 7:00pm UTC](https://discuss.elastic.co/t/issue-with-completionsuggester-query-using-dotnet-elastic-clients-elasticsearch/374959/3 "2025-02-25T19:00:12Z")

</div>

Thank you so much. That explains everything. I have spent hours looking at source code, but I just seems ignored them somehow. I am sure this post will help so many others, because I have tried several AI tools even before looking at the source code, and they all gave wrong solutions...

---

<div class="post-metadata">

**Author:** ![Reed\_M](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/reed_m/32/141606_2.png) [@Reed\_M](https://discuss.elastic.co/u/Reed_M)\
**Post date:** [February 25, 2025, 8:03pm UTC](https://discuss.elastic.co/t/issue-with-completionsuggester-query-using-dotnet-elastic-clients-elasticsearch/374959/4 "2025-02-25T20:03:40Z")

</div>

After trying it out, I think there is a final piece to be added for a complete solution.  
There is NullReferenceException at

```auto
Suggesters =
{
	{
		"product-suggest",
		suggester
	}
}

```

But addressing explicit type `new Dictionary<string, FieldSuggester>` solves it.

```auto
Suggesters = new Dictionary<string, FieldSuggester>
{
	{
		"product-suggest",
		suggester
	}
}

```

Making the completed solution to be:

```auto
var suggester = FieldSuggester.Completion(new CompletionSuggester
{
	Field = "autoCompleteSuggest",
	Size = 5,
	SkipDuplicates = true,
					
});

suggester.Prefix = "match";
// ...

var searchRequest = new SearchRequest("customer-catalog-test-0001")
{
	// Exclude the _source
	Source = new SourceConfig(false),

	// Define the suggester
	Suggest = new Suggester
	{
		// NullReferenceException if no explicit new Dictionary<string, FieldSuggester>
		Suggesters = new Dictionary<string, FieldSuggester>
		{
			{
				"product-suggest",
				suggester
			}
		}
	}
};

```
