Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using Completion Suggester with NEST
void Main()
{
var indexName = "stackoverflow";
var node = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var settings = new ConnectionSettings(node)
.InferMappingFor<Question>(m => m
.IndexName(indexName)
)
.PrettyJson()
.DisableDirectStreaming()
.OnRequestCompleted(callDetails =>
{
// log out the request
if (callDetails.RequestBodyInBytes != null)
{
Console.WriteLine(
$"{callDetails.HttpMethod} {callDetails.Uri} \n" +
$"{Encoding.UTF8.GetString(callDetails.RequestBodyInBytes)}");
}
else
{
Console.WriteLine($"{callDetails.HttpMethod} {callDetails.Uri}");
}
Console.WriteLine();
// log out the response
if (callDetails.ResponseBodyInBytes != null)
{
Console.WriteLine($"Status: {callDetails.HttpStatusCode}\n" +
$"{Encoding.UTF8.GetString(callDetails.ResponseBodyInBytes)}\n" +
$"{new string('-', 30)}\n");
}
else
{
Console.WriteLine($"Status: {callDetails.HttpStatusCode}\n" +
$"{new string('-', 30)}\n");
}
});
var client = new ElasticClient(settings);
if (client.IndexExists(indexName).Exists)
{
client.DeleteIndex(indexName);
}
var createIndexResponse = client.CreateIndex(indexName, c => c
.Mappings(m => m
.Map<Question>(u => u
.AutoMap()
)
)
);
var questions = new[] {
new Question
{
Title = "Elasticsearch for .NET",
TitleSuggest = new CompletionField
{
Input = new [] { "Elasticsearch.Net", "NEST" }
}
},
new Question
{
Title = "The Elastic stack",
TitleSuggest = new CompletionField
{
Input = new [] { "Elasticsearch", "Logstash", "Kibana", "Beats" }
}
},
new Question
{
Title = "Kibana",
TitleSuggest = new CompletionField
{
Input = new [] { "Kibana", "UI" }
}
}
};
client.IndexMany(questions);
client.Refresh(indexName);
var input = "el";
var response = client.Search<Question>(s => s
.Suggest(su => su
.Completion("title", cs => cs
.Field(f => f.TitleSuggest)
.Prefix(input)
.Fuzzy(f => f
.Fuzziness(Fuzziness.Auto)
)
.Size(5)
)
)
);
var suggestions =
from suggest in response.Suggest["title"]
from option in suggest.Options
select new
{
Id = option.Id,
Text = option.Text,
Score = option.Score,
Source = option.Source
};
}
public class Question
{
public string Title { get; set; }
public CompletionField TitleSuggest { get; set; }
}
@lorentmustafa
Copy link

lorentmustafa commented Mar 22, 2021

suggestions variable returns saying "ienumerable yielded no result". Any fix?

@gabrielsimas
Copy link

gabrielsimas commented Nov 1, 2021

My implementation:

string text = "ger";
string suggestSectionName = "tags-suggest";
string fieldName = "tagsAutocomplete";

var client = await GetClient();
var suggestionResponse = await client.SearchAsync<MyClass>(
    s => s                                                
        .Suggest(
            su => su
                .Completion(suggestSectionName, cs => cs
                    .Field(fieldName)
                    .Prefix(text)                                    
                    .Size(100)
                    .SkipDuplicates()
                )
        )
);                
var suggestionsLambda = suggestionResponse.Suggest[suggestSectionName]
    .SelectMany(x => x
        .Options
            .Select(x => x.Text)
    ).ToArray();

What do you think about this?

And thanks for sharing your knowledge!! Help me a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment