mirror of
https://github.com/Hopiu/wagtail.git
synced 2026-03-17 06:20:31 +00:00
Fixed backwards compatibility with Elasticsearch URLs setting
This commit is contained in:
parent
494a26b731
commit
930833324d
2 changed files with 70 additions and 4 deletions
|
|
@ -2,6 +2,8 @@ from __future__ import absolute_import
|
|||
|
||||
import json
|
||||
|
||||
from six.moves.urllib.parse import urlparse
|
||||
|
||||
from django.db import models
|
||||
from django.db.models.sql.where import SubqueryConstraint
|
||||
|
||||
|
|
@ -446,17 +448,30 @@ class ElasticSearch(BaseSearch):
|
|||
super(ElasticSearch, self).__init__(params)
|
||||
|
||||
# Get settings
|
||||
self.es_hosts = params.pop('HOSTS', None)
|
||||
self.es_urls = params.pop('URLS', ['http://localhost:9200'])
|
||||
self.es_index = params.pop('INDEX', 'wagtail')
|
||||
self.es_timeout = params.pop('TIMEOUT', 5)
|
||||
self.es_force_new = params.pop('FORCE_NEW', False)
|
||||
self.es_timeout = params.pop('TIMEOUT', 10)
|
||||
|
||||
# If HOSTS is not set, convert URLS setting to HOSTS
|
||||
if self.es_hosts is None:
|
||||
self.es_hosts = []
|
||||
|
||||
for url in self.es_urls:
|
||||
parsed_url = urlparse(url)
|
||||
|
||||
self.es_hosts.append({
|
||||
'host': parsed_url.hostname,
|
||||
'port': parsed_url.port or 9200,
|
||||
'url_prefix': parsed_url.path,
|
||||
'use_ssl': parsed_url.scheme == 'https',
|
||||
})
|
||||
|
||||
# Get ElasticSearch interface
|
||||
# Any remaining params are passed into the ElasticSearch constructor
|
||||
self.es = Elasticsearch(
|
||||
urls=self.es_urls,
|
||||
hosts=self.es_hosts,
|
||||
timeout=self.es_timeout,
|
||||
force_new=self.es_force_new,
|
||||
**params)
|
||||
|
||||
def reset_index(self):
|
||||
|
|
|
|||
|
|
@ -397,3 +397,54 @@ class TestElasticSearchMappingInheritance(TestCase):
|
|||
}
|
||||
|
||||
self.assertDictEqual(document, expected_result)
|
||||
|
||||
|
||||
class TestBackendConfiguration(TestCase):
|
||||
def setUp(self):
|
||||
# Import using a try-catch block to prevent crashes if the elasticsearch-py
|
||||
# module is not installed
|
||||
try:
|
||||
from wagtail.wagtailsearch.backends.elasticsearch import ElasticSearch
|
||||
except ImportError:
|
||||
raise unittest.SkipTest("elasticsearch-py not installed")
|
||||
|
||||
self.ElasticSearch = ElasticSearch
|
||||
|
||||
def test_default_settings(self):
|
||||
backend = self.ElasticSearch(params={})
|
||||
|
||||
self.assertEqual(len(backend.es_hosts), 1)
|
||||
self.assertEqual(backend.es_hosts[0]['host'], 'localhost')
|
||||
self.assertEqual(backend.es_hosts[0]['port'], 9200)
|
||||
self.assertEqual(backend.es_hosts[0]['use_ssl'], False)
|
||||
|
||||
def test_hosts(self):
|
||||
# This tests that HOSTS goes to es_hosts
|
||||
backend = self.ElasticSearch(params={
|
||||
'HOSTS': [
|
||||
{
|
||||
'host': '127.0.0.1',
|
||||
'port': 9300,
|
||||
'use_ssl': True,
|
||||
}
|
||||
]
|
||||
})
|
||||
|
||||
self.assertEqual(len(backend.es_hosts), 1)
|
||||
self.assertEqual(backend.es_hosts[0]['host'], '127.0.0.1')
|
||||
self.assertEqual(backend.es_hosts[0]['port'], 9300)
|
||||
self.assertEqual(backend.es_hosts[0]['use_ssl'], True)
|
||||
|
||||
def test_urls(self):
|
||||
# This test backwards compatibility with old URLS setting
|
||||
backend = self.ElasticSearch(params={
|
||||
'URLS': ['http://localhost:12345', 'https://127.0.0.1:54321'],
|
||||
})
|
||||
|
||||
self.assertEqual(len(backend.es_hosts), 2)
|
||||
self.assertEqual(backend.es_hosts[0]['host'], 'localhost')
|
||||
self.assertEqual(backend.es_hosts[0]['port'], 12345)
|
||||
self.assertEqual(backend.es_hosts[0]['use_ssl'], False)
|
||||
self.assertEqual(backend.es_hosts[1]['host'], '127.0.0.1')
|
||||
self.assertEqual(backend.es_hosts[1]['port'], 54321)
|
||||
self.assertEqual(backend.es_hosts[1]['use_ssl'], True)
|
||||
|
|
|
|||
Loading…
Reference in a new issue