'Problem with Elasticsearch and getting results
I have a Elasticsearch in a dockercontainer, but I have problems getting results back. My set up is like this: In program.cs
var pool = new SingleNodeConnectionPool(new Uri(builder.Configuration.GetConnectionString("Elasticsearch")));
var settings = new ConnectionSettings(pool)
.DisableDirectStreaming()
.DefaultIndex(ElasticSearchHelper.AddressesIndexName);
var client = new ElasticClient(settings);
builder.Services.AddSingleton(client);
My indexer:
await _elasticClient.Indices.DeleteAsync(AddressesIndexName, ct: cancellationToken);
if (!(await _elasticClient.Indices.ExistsAsync(AddressesIndexName, ct: cancellationToken)).Exists)
{
var result = await _elasticClient.Indices.CreateAsync(AddressesIndexName, m =>
m.Map<Address>(d =>
d.AutoMap()
.Properties(
p => p
.Text(t => t.Name(n => n.Street))
.Keyword(t => t.Name(n => n.AddressId))
.Text(t => t.Name(n => n.AddressId))
)
)
, ct: cancellationToken);
}
int totalRows = 0;
var addresses = await _dataContext.Addresses.Take(100).ToListAsync(cancellationToken);
performContext.WriteLine($"Found {addresses.Count} addresses");
if (addresses.Count > 0)
{
var bulkAllObservable = _elasticClient.BulkAll(addresses, b => b
.Index(AddressesIndexName)
.BackOffTime("30s")
.BackOffRetries(2)
.RefreshOnCompleted()
.MaxDegreeOfParallelism(Environment.ProcessorCount)
.Size(1000)
)
.Wait(TimeSpan.FromMinutes(15), next =>
{
totalRows = totalRows + next.Items.Count;
performContext.WriteLine(ConsoleTextColor.Green, "{totalRows} of {addresses.Count}");
});
}
My first query that does return results
var docs = _elasticClient.Search<Address>(b => b
.Index(ElasticSearchHelper.AddressesIndexName)
.Query(q => q
.Terms(t => t.Field(f => f.Street).Terms("skovvangen"))
)
);
My second query that doesn't work:
docs = _elasticClient.Search<Address>(b => b
.Index(ElasticSearchHelper.AddressesIndexName)
.Query(q => q
.Terms(t => t.Field(f => f.AddressId).Terms("000021c5-e9ee-411d-b2d8-ec9161780ccd"))
)
);
The first query returns a result but the second does not no matter what I do. I am guess it is because I haven't set up the indexing correctly. I am new to Elasticsearch, so I really need some pointers to what I am doing wrong. Hope you can help me.
Solution 1:[1]
In case of an exception during DB access, Hibernate will generally mark the complete transaction as rollbackOnly, even if one would explicitly try to avoid this behavior via the @Transactional(noRollbackFor=Exception.class) annotation attribute.
So in these cases, there needs to be a different solution, such as having separate transactions for the two method calls.
(Similar problem: handle sql exception for large data insert)
Solution 2:[2]
I assume you have a transaction managed by Spring (@Transactional) somewhere inside methodB. In Spring, a transaction is marked for rollback when a RuntimeException is thrown within this transaction.
From the Spring docs:
In its default configuration, the Spring Frameworkâs transaction infrastructure code only marks a transaction for rollback in the case of runtime, unchecked exceptions; that is, when the thrown exception is an instance or subclass of RuntimeException.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | fladdimir |
| Solution 2 | maciejtoporowicz |
