我想使用ElasticSearch.NET
nuget将文档添加到弹性搜索中。
它工作正常,但是仅当我使用PostData
创建PostData.MultiJson
对象时。
如果我使用PostData.String
尝试同样的操作,则会添加文档,但是,在创建索引(第一篇文章)时,日期字段将创建为字符串,而不是第一种方法,其中日期字段创建为Date
。
以下示例说明:
public void AddJsonWithDate()
{
var connectionSettings = new ConnectionConfiguration(new Uri(_elasticSearchHost));
var esClient = new ElasticLowLevelClient(connectionSettings);
var obj = new Demo { N1 = 21312, S1 = "test", Date = DateTime.Now };
/*
indexName, PostData.String(postDataJson)
indexName, PostData.MultiJson(new object[] { postDataJson })
*/
var jsonStr = WriteFromObject<Demo>(obj);
var postDataStr = PostData.String(jsonStr);
//var postDataObj = PostData.MultiJson(new object[] { obj });
//var response = esClient.Index<StringResponse>("myindex1", "_doc", postDataObj); /* Date field is created as DATE in the document type */
var response = esClient.Index<StringResponse>("myindex2", "_doc", postDataStr); /* Date field is created as STRING in the document type */
}
public string WriteFromObject<T>(T obj) where T : class, new()
{
//Create a stream to serialize the object to.
MemoryStream ms = new MemoryStream();
// Serializer the User object to the stream.
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
ser.WriteObject(ms, obj);
byte[] json = ms.ToArray();
ms.Close();
return Encoding.UTF8.GetString(json, 0, json.Length);
}
public class Demo
{
public int N1 { get; set; }
public string S1 { get; set; }
public DateTime Date { get; set; }
}
我的应用当前正在以字符串形式发送json,如何解决此问题?