I'm trying to implement a sample code that use suggestion feature from Elastic Search.
The indexed document consist of a flat POCO
public class CandidateDocument
{
public Guid Id { get; set; }
public string Name { get; set; }
public string FirstName { get; set; }
public DateTime BirthDate { get; set; }
public CompletionField Suggest { get; set; }
}
The mapping is the following
var client = new ElasticClient(settings);
client.Indices.Create("candidates", c =>
c.Map<CandidateDocument>(m =>
m.Properties(ps => ps.Text(s => s.Name(n => n.Name)
.Store(false)
.Fields(f => f.Keyword(k => k.Name("nameRaw"))))
.Text(s => s.Name(n => n.FirstName)
.Store(false)
.Fields(f => f.Keyword(k => k.Name("firstNameRaw"))
.Date(s => s.Name(n => n.BirthDate).Format("ddMMyyyy"))
.Keyword(s => s.Name(n => n.Id))))
.Completion(c => c.Name(n => n.Suggest)))));
Indexing a document looks like:
var candidateDocument = new CandidateDocument
{
Id = Guid.NewGuid(),
Name = "Lennon",
FirstName = "John",
BirthDate = DateTime.Now,
Suggest = new CompletionField
{
Input = new[] { "Lennon" },
}
};
var indexResponse = await this.elasticClient.IndexAsync(candidateDocument, i => i.Index("candidates"));
And my query is :
var searchResponse = await this.elasticClient.SearchAsync<CandidateDocument>(s => s.Index("candidates").Suggest(su => su
.Completion("suggestions", c => c
.Field(f => f.Suggest)
.Prefix(query)
.Fuzzy(f => f.Fuzziness(Fuzziness.Auto))
.Size(5))));
I'm facing an issue the following issue :
Elasticsearch.Net.ElasticsearchClientException: Request failed to execute. Call: Status code 400 from: POST /candidates/_search?typed_keys=true. ServerError: Type: search_phase_execution_exception Reason: "all shards failed" CausedBy: "Type: illegal_argument_exception Reason: "no mapping found for field [suggest]" CausedBy: "Type: illegal_argument_exception Reason: "no mapping found for field [suggest]"
Looking at my index mapping using kibana :
"suggest": {
"properties": {
"input": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
By the way I'm using latest version of Nest (7.9.0) in .NET Core 3.1
I would appreciate your help.
It looks like the candidates index and suggest field may have already existed at the point of issuing the create index with mapping request. You’d be able to check the CreateIndexResponse
returned to see if this is the case; the IsValid
property would be false, with the error details on the response providing why.
Elasticsearch’s default behaviour will create an index and mapping if an index request comes in to index a document into it, inferring the mapping for fields based on the first document it sees. This is useful behaviour for many use cases, but for search use cases, you typically want to control the creation of indices with explicit mappings as you’re doing, so to turn this behaviour off you can set the action.auto_create_index
cluster setting to false
.. Now, if you delete the index and recreate it again with the create index request you have, it won’t automatically be created before you have a chance to explicitly create it.