當我試圖從ElasticSearch中讀取某些欄位時,我注意到如果我不包含一個文本屬性,那么它就不會將變數名稱映射到欄位。它將讀取該特定欄位的空值。假設我在ES中有一個名為ValueId的欄位,型別為text。在我的模型類中,我有public string ValueId {get; set;}。ValueId應該有一個變數的值,但是當我沒有添加Elasticsearch屬性屬性,即[Text(Name = "ValueId")],它回傳null。
現在,如果我把它設定成下面的樣子,那么它將為該變數回傳一個值。
[Text(Name = "ValueId")]
public string ValueId {get; set; }
我的猜測是由于駱駝的外殼,這對嗎?
另外,我是否有辦法動態地設定該屬性名稱(Name = "")?就像創建一個方法來做這件事一樣。
uj5u.com熱心網友回復:
與NEST Automapping合作,它總是幫助我解決這些問題。https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/auto-map.html
另一點是用NEST正確配置你的彈性,下面的例子顯示了正確的配置方式:
public static void AddElasticsearch(thisIServiceCollection services, IConfiguration配置,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)。
}
同樣,如果繼續什么都不做,可以嘗試使用一個配置。settings.DefaultFieldNameInferrer(p => p);
更多的細節,請點擊這里。
更多的細節可以在這個示例專案中找到。https://github.com/hgmauri/elasticsearch-with-nest
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/315417.html
標籤:
上一篇:RequestError(400,'illegal_argument_exception','[f756ea2593ee][172.18.0.4:9300][indice
下一篇:在類的建構式中使用這個指標陣列c
