Troubleshooting Connection Issues for Elasticsearch
Platform notice: Server and Data Center only. This article only applies to Atlassian products on the Server and Data Center platforms.
Support for Server* products ended on February 15th 2024. If you are running a Server product, you can visit the Atlassian Server end of support announcement to review your migration options.
*Except Fisheye and Crucible
Summary
Bitbucket Server is unable to connect to Elasticsearch.
One of the following log line appears in the atlassian-bitbucket.log:
Connection refused
DEBUG "POST /rest/search/latest/search HTTP/1.1" c.a.b.s.internal.rest.SearchResource Connection exception: Connection refused: no further information DEBUG "POST /rest/search/latest/search HTTP/1.1" c.atlassian.bitbucket.search.timing Timing: Search request execution took 1.023 s [1022 ms] for query 'XXX'
HTTP 401 status code
DefaultElasticsearchConnectionTester Testing connection with Elasticsearch failed: HTTP response status code is 401
This will cause a large error on the screen when performing a search.
Diagnosis
Diagnostic Steps
- There are no details in the Elasticsearch username and/or password fields
- The option to let the installer startup Bitbucket Server was not chosen
If you are receiving 401 errors, these indicate that authentication issues are happening - meaning it's important to check if Bitbucket has been configured with the correct credentials to access Elasticsearch.
Once the application has started up, it's suggested that you fully execute a test of Elasticsearch at Administration (cog in the top right) → Server Settings → Test Elasticsearch Connection
If you receive the following error, then it's highly recommended you follow the steps listed in this knowledge article in order to help confirm that the credentials you're using are correct:
Cause
This depends on what error you were seeing in the logs:
Stacktrace #1 - Connection Refused
Elasticsearch was not started.
Stacktrace #2 - HTTP 401 Errors
This is due to the fact that Bitbucket Server started before Elasticsearch on the first-ever run. Therefore Bitbucket Server did not get to negotiate a username and password for Elasticsearch. This negotiation only happens during the Bitbucket Server startup. Therefore if it is missed, it doesn't get the chance to try again. This really only has to succeed once.
Solution
The resolution is the same for both cases and requires the following steps to be performed:
- Start Elasticsearch
- Restart Bitbucket Server
The steps on how to perform the restart are detailed on the Start and stop Bitbucket page.
This will then mean that Elasticsearch is already waiting for Bitbucket Server to start up. Therefore when Bitbucket Server gets to the stage of startup where it attempts to negotiate a password, Elasticsearch will be ready to accept that connection.
- In the case of using Remote Elasticsearch, Start the Elasticsearch services first and then start the Bitbucket application Services.
- In case of using Bundled Elasticsearch, Restart the Bitbucket Application Services. Bundled Elasticsearch will be automatically started when the Bitbucket application service starts.
Bitbucket Server 5.0
As of Bitbucket Server 5.0, Bundled ElasticSearch is now automatically started when the Bitbucket application is started.