Browse Source

fix bulk removing items from elasticsearch index (#1374)

pull/1376/head
Jason Dove 2 years ago committed by GitHub
parent
commit
d951035183
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 13
      ErsatzTV.Infrastructure/Search/ElasticSearchIndex.cs

13
ErsatzTV.Infrastructure/Search/ElasticSearchIndex.cs

@ -41,7 +41,7 @@ public class ElasticSearchIndex : ISearchIndex
public async Task<bool> IndexExists() public async Task<bool> IndexExists()
{ {
_client ??= new ElasticsearchClient(Uri); _client ??= CreateClient();
ExistsResponse exists = await _client.Indices.ExistsAsync(IndexName); ExistsResponse exists = await _client.Indices.ExistsAsync(IndexName);
return exists.IsValidResponse; return exists.IsValidResponse;
} }
@ -52,7 +52,8 @@ public class ElasticSearchIndex : ISearchIndex
ILocalFileSystem localFileSystem, ILocalFileSystem localFileSystem,
IConfigElementRepository configElementRepository) IConfigElementRepository configElementRepository)
{ {
_client ??= new ElasticsearchClient(Uri); _client ??= CreateClient();
ExistsResponse exists = await _client.Indices.ExistsAsync(IndexName); ExistsResponse exists = await _client.Indices.ExistsAsync(IndexName);
if (!exists.IsValidResponse) if (!exists.IsValidResponse)
{ {
@ -159,7 +160,7 @@ public class ElasticSearchIndex : ISearchIndex
var totalCount = 0; var totalCount = 0;
Query parsedQuery = LuceneSearchIndex.ParseQuery(query); Query parsedQuery = LuceneSearchIndex.ParseQuery(query);
SearchResponse<MinimalElasticSearchItem> response = await _client.SearchAsync<MinimalElasticSearchItem>( SearchResponse<MinimalElasticSearchItem> response = await _client.SearchAsync<MinimalElasticSearchItem>(
s => s.Index(IndexName) s => s.Index(IndexName)
.Sort(ss => ss.Field(f => f.SortTitle, fs => fs.Order(SortOrder.Asc))) .Sort(ss => ss.Field(f => f.SortTitle, fs => fs.Order(SortOrder.Asc)))
@ -188,6 +189,12 @@ public class ElasticSearchIndex : ISearchIndex
// do nothing // do nothing
} }
private static ElasticsearchClient CreateClient()
{
ElasticsearchClientSettings settings = new ElasticsearchClientSettings(Uri).DefaultIndex(IndexName);
return new ElasticsearchClient(settings);
}
private async Task<CreateIndexResponse> CreateIndex() => private async Task<CreateIndexResponse> CreateIndex() =>
await _client.Indices.CreateAsync<ElasticSearchItem>( await _client.Indices.CreateAsync<ElasticSearchItem>(
IndexName, IndexName,

Loading…
Cancel
Save