将Nest库从1.3.1升级到7.17.5,c#代码如何更新,旧代码如下: public void Commit() { var elasticClient =…
- User
将Nest库从1.3.1升级到7.17.5,c#代码如何更新,旧代码如下: public void Commit() { var elasticClient = ClientBuilder.CreateConnection(this.UnderlyingIndexName); elasticClient.DeleteAlias(new DeleteAliasRequest(“_all”, AliasName)); // if there is an index using the same name as our alias, drop it // because we can’t create an alias if an index with the same name exists if (elasticClient.IndexExists(AliasName).Exists) elasticClient.DeleteIndex(AliasName); // add a new alias pointing to our new index elasticClient.PutAlias(p => p.Index(UnderlyingIndexName).Name(AliasName));
await _client.RefreshAsync();
}
public async Task<IEnumerable> GetAsync(string languageKey, string kgId, long? revision) { var entities = await _client.SearchAsync(sd => {
sd = sd.Filter(fd => { var filterContainer = fd.Term(m => m.Kg, kgId); if (languageKey != null) { filterContainer = filterContainer && fd.Term(m => m.LanguageKey, languageKey); } return filterContainer; });
sd = sd.Size(100000) .SortAscending(m => m.Name); return sd; }); return entities.Documents; }
private IDeleteResponse Clear() { return _client.DeleteByQuery(dbd => dbd.MatchAll()); }
- Assistant
要将Nest库从1.3.1升级到7.17.5,您需要更新您的C#代码以适用于新版本的Nest库。在新版本中,一些API和方法已经发生了变化。以下是您可以更新的代码示例:
using Nest; public void Commit() { var elasticClient = new ElasticClient(ClientSettings); elasticClient.Indices.DeleteAlias(new DeleteAliasRequest(AliasName, Indices.All)); if (elasticClient.Indices.Exists(AliasName).Exists) elasticClient.Indices.Delete(AliasName); elasticClient.Indices.BulkAlias(ba => ba .Add(a => a.Index(UnderlyingIndexName).Alias(AliasName))); elasticClient.Indices.Refresh(Indices.All); } public async Task<IEnumerable<MetadataEntity>> GetAsync(string languageKey, string kgId, long? revision) { var searchRequest = new SearchRequest<MetadataEntity>(Indices.All) { Size = 100000, Sort = new List<ISort> { new FieldSort { Field = "name.keyword" } }, Query = new BoolQuery { Must = new List<QueryContainer> { new TermQuery { Field = "kg", Value = kgId } } } }; if (languageKey != null) { ((BoolQuery)searchRequest.Query).Must.Add(new TermQuery { Field = "languageKey", Value = languageKey }); } var searchResponse = await _client.SearchAsync<MetadataEntity>(searchRequest); return searchResponse.Documents; } private BulkDeleteResponse Clear() { return _client.DeleteByQuery<MetadataEntity>(dbd => dbd .Query(q => q.MatchAll()) .Conflicts(Conflicts.Proceed) ); } - User
- Assistant
- User
CS0030: Cannot convert type 'Nest.QueryContainer' to 'Nest.BoolQuery'
- Assistant
您遇到了CS0030编译错误,原因是无法将类型
Nest.QueryContainer转换为Nest.BoolQuery。这是因为在新版本的Nest库中,BoolQuery类已被重命名为QueryContainerDescriptor<T>bool-query。请按照以下方式更新您的代码:
public async Task<IEnumerable<MetadataEntity>> GetAsync(string languageKey, string kgId, long? revision) { var searchResponse = await _client.SearchAsync<MetadataEntity>(sd => { sd.Size(100000) .SortAscending(m => m.Name) .Query(q => q .Bool(bq => bq .Filter(fd => { QueryContainer filterContainer = fd.Term(m => m.Kg, kgId); if (languageKey != null) { filterContainer &= fd.Term(m => m.LanguageKey, languageKey); } return filterContainer; }) ) ); return sd; }); return searchResponse.Documents; }请注意,我已将
BoolQuery替换为QueryContainerDescriptor<T>以适应新版本的Nest库。这样,您便可以正确地构建布尔查询。请确保根据您的需求进行其他必要的更改。 - User
没有BulkDeleteResponse这个类
- Assistant
- User
将Nest库从1.3.1升级到7.17.5,更新C#代码以适用于新版本的Nest库,还有旧代码如下: private IDeleteResponse Clear() { return _client.DeleteByQuery(dbd => dbd.MatchAll()); }
- Assistant
要将Nest库从1.3.1升级到7.17.5,您需要更新您的C#代码以适用于新版本的Nest库。以下是您可以更新的代码示例:
using Nest; private async Task<DeleteByQueryResponse> ClearAsync() { var response = await _client.DeleteByQueryAsync<MetadataEntity>(dbd => dbd.Query(q => q.MatchAll())); return response; }在新版本的Nest库中,
DeleteByQueryResponse类用于表示删除操作的响应。同时,删除操作的方法名从DeleteByQuery更改为DeleteByQueryAsync(同步版本为DeleteByQuery)。请注意,您的旧代码返回
IDeleteResponse,我更改了返回类型以匹配新版本的Nest库。请确保根据您的需求进行其他必要的更改。 - User
将Nest库从1.3.1升级到7.17.5,更新C#代码以适用于新版本的Nest库,还有旧代码如下: public void CreateIfNotExist() { string indexName = GetIndexName();
if (!_client.IndexExists(id => id.Index(indexName)).Exists) { _client.CreateIndex(indexName, c => c.AddMapping<SparePartEntity>(r => r.Type<SparePartEntity>().MapFromAttributes()) .AddMapping<MetadataEntity>(r => r.Type<MetadataEntity>().MapFromAttributes()) ); //var rootObjectMapping = _client.GetMapping<SparePartEntity>(); //rootObjectMapping.Properties.Add("NameCompletion", new CompletionMapping()); //_client.Map(rootObjectMapping, _configuration.SparePartsIndex, rootObjectMapping.TypeNameMarker.Name, true); } else { var mapFromAttributes = _client.Map<SparePartEntity>(pm => pm.MapFromAttributes()); _logger.Info("updated mapping for spareparts with status:" + mapFromAttributes.Acknowledged); mapFromAttributes = _client.Map<MetadataEntity>(pm => pm.MapFromAttributes()); _logger.Info("updated mapping for metadata with status:" + mapFromAttributes.Acknowledged); } } public void ClearAsync() { _client.DeleteIndex(did => did.Index(_configuration.SparePartsIndex)); //DeleteByQueryAsync<SparePartEntity>(r => r.MatchAll()); } public Task<List<string>> GetAllIds() { var searchAsync = _client.SearchAsync<SparePartEntity>(s => s.MatchAll().FacetTerm(t => t.OnField(sp => sp.SparePartId).Size(int.MaxValue))); return searchAsync.ContinueWith(t => t.Result.Facets.Values.Cast<TermFacet>().SelectMany(v => v.Items.Select(i => i.Term)).ToList()); } - Assistant
要将Nest库从1.3.1升级到7.17.5,您需要更新您的C#代码以适用于新版本的Nest库。以下是您可以更新的代码示例:
using Nest; public void CreateIfNotExist() { string indexName = GetIndexName(); if (!_client.Indices.Exists(indexName).Exists) { var createIndexResponse = _client.Indices.Create(indexName, c => c .Map<SparePartEntity>(m => m.AutoMap()) .Map<MetadataEntity>(m => m.AutoMap()) ); if (!createIndexResponse.IsValid) { // 创建索引失败 // ... } } else { var updateMappingResponse = _client.Indices.UpdateMapping<SparePartEntity>(m => m.AutoMap()); if (!updateMappingResponse.IsValid) { // 更新映射失败 // ... } updateMappingResponse = _client.Indices.UpdateMapping<MetadataEntity>(m => m.AutoMap()); if (!updateMappingResponse.IsValid) { // 更新映射失败 // ... } } } public async Task ClearAsync