我在我的dotnet项目中使用ElasticSearch 2.1。到目前为止面临的问题是无法在下面的行中插入批量数据获取异常。 var result = client.Bulk(bulkInsertData); 错误: 读取字符串出错。意外的令牌:StartObject。路径'items [0] .index.error',第1行,第137位。
StackTrace:
at Newtonsoft.Json.JsonReader.ReadAsStringInternal()
at Newtonsoft.Json.JsonTextReader.ReadAsString()
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType(JsonReader reader,JsonContract contract,Boolean hasConverter)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject,JsonReader reader,JsonObjectContract contract,JsonProperty member,String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Populate(JsonReader reader,Object target)
at Newtonsoft.Json.Serialization.JsonSerializerProxy.PopulateInternal(JsonReader reader,Object target)
at Newtonsoft.Json.JsonSerializer.Populate(JsonReader reader,Object target)
在Nest.Resolvers.Converters.BulkOperationResponseItemConverter.ReadJson(JsonReader reader,Type objectType,Object existingValue,JsonSerializer serializer)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.DeserializeConvertable(JsonConverter converter,JsonReader reader,Type objectType,Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IList list,JsonReader reader,JsonArrayContract contract,JsonProperty containerProperty,String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader,Type objectType,JsonContract contract,JsonProperty member,Object existingValue,String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader,Type objectType,JsonContract contract,JsonProperty member,JsonContainerContract containerContract,JsonProperty containerMember,Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty属性,JsonConverter propertyConverter,JsonContainerContract containerContract,JsonProperty containerProperty,JsonReader reader,Object target)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject,JsonReader reader,JsonObjectContract contract,JsonProperty member,String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader,Type objectType,JsonContract contract,JsonProperty member,JsonContainerContract containerContract,JsonProperty containerMember,Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader,Type objectType,JsonContract contract,JsonProperty member,JsonContainerContract containerContract,JsonProperty containerMember,Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader,Type objectType,Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader,Type objectType)
在Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader,Type objectType)
在Nest.NestSerializer.DeserializeUsingSettings [T](Stream stream,JsonSerializerSettings设置)
在Nest.NestSerializer.Deserialize [T](流媒体流)
at Elasticsearch.Net.Connection.RequestHandlers.RequestHandler.StreamToTypedResponse [T](ElasticsearchResponse 1 streamResponse, ITransportRequestState requestState, Byte[] readBytes)
at Elasticsearch.Net.Connection.RequestHandlers.RequestHandler.ReturnTypedResponse[T](TransportRequestState
1 requestState,ElasticsearchResponse 1 streamResponse, ElasticsearchServerError& error)
at Elasticsearch.Net.Connection.RequestHandlers.RequestHandler.CoordinateRequest[T](TransportRequestState
1 requestState,Int32 maxRetries,Int32 recoied,Boolean& aliveResponse)
在Elasticsearch.Net.Connection.RequestHandlers.RequestHandler.DoRequest [T](TransportRequestState 1 requestState)
at Elasticsearch.Net.Connection.RequestHandlers.RequestHandler.Request[T](TransportRequestState
1 requestState,Object data)
在Elasticsearch.Net.Connection.Transport.DoRequest [T](字符串方法,字符串路径,对象数据,IRequestParameters requestParameters)
at Elasticsearch.Net.ElasticsearchClient.DoRequest [T](String method,String path,Object data,IRequestParameters requestParameters)
在Elasticsearch.Net.ElasticsearchClient.Bulk [T](Object body,Func 2 requestParameters)
at Nest.RawDispatch.BulkDispatch[T](ElasticsearchPathInfo
1 pathInfo,Object body)
在Nest.ElasticClient.b__339_0(ElasticsearchPathInfo 1 p, IBulkRequest d)
at Nest.ElasticClient.Nest.IHighLevelToLowLevelDispatcher.Dispatch[D,Q,R](D descriptor, Func
3发送)
在Nest.ElasticClient.Bulk(IBulkRequest bulkRequest)
在ElasticSearch.cs中的ElasticSearch.ElasticSearchInsertData.InsertCaseData():第61行
运行以下代码:创建映射并且不会插入数据,因为m得到上述错误使用0文档创建的映射 GET / 1246Index pap / _mapping
"1246Index pap": {
"mappings": {
"ElasticSearchInfo": {
"properties": {
"Number": {
"type": "string",
"index": "no"
},
"ID": {
"type": "string",
"index": "no"
},
"Type": {
"type": "string",
"index": "not_analyzed"
},
"NestedObjectinformation": {
"type": "nested",
"properties": {
"Name": {
"type": "string"
}
}
}
}
}
}
}
代码:
[ElasticType(Name = "ElasticSearchInfo")]
public class ElasticSearchNestedInfo
{
[ElasticProperty(Name = "ID", Index = FieldIndexOption.No, Type = FieldType.String)]
public string ID { get; set; }
[ElasticProperty(Name = "Type", Index = FieldIndexOption.NotAnalyzed, Type = FieldType.String)]
public string Type { get; set; }
[ElasticProperty(Name = "Number", Index = FieldIndexOption.No, Type = FieldType.String)]
public string Number { get; set; }
[ElasticProperty(Name = "nestedobjectinformation", Index = FieldIndexOption.Analyzed, Type = FieldType.Nested)]
public List<ElasticSearchNestedInfo> NestedObjectinformation { get; set; }
}
[ElasticType(Name = "ElasticSearchNestedInfo")]
public class ElasticSearchNestedInfo
{
[ElasticProperty(Name = "Name", Index = FieldIndexOption.No, Type = FieldType.String)]
public string Name { get; set; }
}
List<ElasticSearchInfo> elasticSearchCaseInfo = caseBL.GetElasticSearchInsertData(i);
string connElasticServer = SystemCacheHelper.GetElasticSearchConnectionString();
var node = new Uri(connElasticServer);
var settings = new ConnectionSettings(node).ExposeRawResponse();
var client = new ElasticClient(settings);
BulkDescriptor bulkInsertData = new BulkDescriptor();
foreach (var item in elasticSearchCaseInfo)
{
string index = item.ID + item.Type;
client.CreateIndex(ind => ind.Index(index).AddMapping<ElasticSearchInfo>(map => map.MapFromAttributes()));
// .AddMapping<ElasticSearchNestedInfo>(diag => diag.MapFromAttributes()));
bulkInsertData.Index<ElasticSearchInfo>(bulkInsert => bulkInsert.Document(item)
.Index(index)
.Type(item.Type)
.Id((item.ID)));
}
if (elasticSearchCaseInfo.Count > 0)
{
var result = client.Bulk(bulkInsertData);
client.Map<ElasticSearchInfo>(m => m
.MapFromAttributes()
.Properties(p => p
.NestedObject<ElasticSearchNestedInfo>(no => no
.Name(n => n.NestedObjectinformation.First())
.Dynamic()
.Enabled()
.IncludeInAll()
.IncludeInParent()
.IncludeInRoot()
.MapFromAttributes()
.Path("nestedobjectinformation")
)
)
);
}
由于
答案 0 :(得分:0)
假设您的意图是创建索引并将列表内容放在单个批量插入中。你应该这样做:
public void TestBulkInsert()
{
List<ElasticSearchInfo> elasticSearchCaseInfo = caseBL.GetElasticSearchInsertData(i);
string connElasticServer = SystemCacheHelper.GetElasticSearchConnectionString();
var node = new Uri(connElasticServer);
var settings = new ConnectionSettings(node).ExposeRawResponse();
var client = new ElasticClient(settings);
client.CreateIndex(_ => _.Index<ElasticSearchInfo>()));
client.Map<ElasticSearchInfo>(
m => m
.Index<ElasticSearchInfo>()
.MapFromAttributes()
.Properties(p => p
.NestedObject<ElasticSearchNestedInfo>(no => no
.Name(n => n.NestedObjectinformation.First())
.Dynamic()
.Enabled()
.IncludeInAll()
.IncludeInParent()
.IncludeInRoot()
.MapFromAttributes()
.Path("nestedobjectinformation")
)
)
);
var index = client.Infer.IndexName<ElasticSearchInfo>();
var type = client.Infer.TypeName<ElasticSearchInfo>();
client.Bulk(
bd => elasticSearchCaseInfo.Aggregate(
bd,
(bdi, item) => bdi
.Index<ElasticSearchInfo>(
bid => bid
.Index(index)
.Type(type)
.Id(item.ID)
.Document(item)
)
)
);
}
另外,我强烈建议您不要使用属性映射,或者至少不要将它与流畅的映射混合使用。