-
-
Save sefacan/d2d92cdafe934f98a51ee6279256f64f to your computer and use it in GitHub Desktop.
Using Completion Suggester with NEST
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment