When trying to read certain fields from ElasticSearch, I noticed that if I do not include a text attribute then it will not map the variable name to the field. It will read null for that specific field. Lets say I have a field in ES named ValueId
of type text
. In my model class I have public string ValueId {get; set;}
. ValueId
should have a value for that variable, but when I do not add a Elasticsearch Property Attribute i.e. [Text(Name = "ValueId")]
it returns null.
Now if I have it as the following, then it will return a value for that variable.
[Text(Name = "ValueId")]
public string ValueId {get; set;}
My guess is because of the camel casing, is that correct?
On another note, is there a way I could set that property name (Name = "")
dynamically? As in create a method to do this.
CodePudding user response:
Work with NEST Automapping, it always helps me with these questions: https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/auto-map.html
Another point is to correctly configure your elastic with NEST, the example below shows the correct way of configuration:
public static void AddElasticsearch(this IServiceCollection services, IConfiguration configuration, bool configureDefaultQuery = true)
{
var defaultIndex = configuration["ElasticsearchSettings:defaultIndex"];
var basicAuthUser = configuration["ElasticsearchSettings:username"];
var basicAuthPassword = configuration["ElasticsearchSettings:password"];
var settings = new ConnectionSettings(new Uri(configuration["ElasticsearchSettings:uri"]));
if (!string.IsNullOrEmpty(defaultIndex))
settings = settings.DefaultIndex(defaultIndex);
if (!string.IsNullOrEmpty(basicAuthUser) && !string.IsNullOrEmpty(basicAuthPassword))
settings = settings.BasicAuthentication(basicAuthUser, basicAuthPassword);
var client = new ElasticClient(settings);
services.AddSingleton<IElasticClient>(client);
}
Se mesmo assim continuar vindo nulo, experimente utiliar a configuração: settings.DefaultFieldNameInferrer(p => p);
More details can be found in this example project: https://github.com/hgmauri/elasticsearch-with-nest