create_bwc_index_with_plugin_mappings.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import create_bwc_index
  2. import logging
  3. import os
  4. import random
  5. import shutil
  6. import subprocess
  7. import sys
  8. import tempfile
  9. def fetch_version(version):
  10. logging.info('fetching ES version %s' % version)
  11. if subprocess.call([sys.executable, os.path.join(os.path.split(sys.argv[0])[0], 'get-bwc-version.py'), version]) != 0:
  12. raise RuntimeError('failed to download ES version %s' % version)
  13. def create_index(plugin, mapping, docs):
  14. '''
  15. Creates a static back compat index (.zip) with mappings using fields defined in plugins.
  16. '''
  17. logging.basicConfig(format='[%(levelname)s] [%(asctime)s] %(message)s', level=logging.INFO,
  18. datefmt='%Y-%m-%d %I:%M:%S %p')
  19. logging.getLogger('elasticsearch').setLevel(logging.ERROR)
  20. logging.getLogger('urllib3').setLevel(logging.WARN)
  21. tmp_dir = tempfile.mkdtemp()
  22. plugin_installed = False
  23. node = None
  24. try:
  25. data_dir = os.path.join(tmp_dir, 'data')
  26. repo_dir = os.path.join(tmp_dir, 'repo')
  27. logging.info('Temp data dir: %s' % data_dir)
  28. logging.info('Temp repo dir: %s' % repo_dir)
  29. version = '2.0.0'
  30. classifier = '%s-%s' %(plugin, version)
  31. index_name = 'index-%s' % classifier
  32. # Download old ES releases if necessary:
  33. release_dir = os.path.join('backwards', 'elasticsearch-%s' % version)
  34. if not os.path.exists(release_dir):
  35. fetch_version(version)
  36. create_bwc_index.install_plugin(version, release_dir, plugin)
  37. plugin_installed = True
  38. node = create_bwc_index.start_node(version, release_dir, data_dir, repo_dir, cluster_name=index_name)
  39. client = create_bwc_index.create_client()
  40. put_plugin_mappings(client, index_name, mapping, docs)
  41. create_bwc_index.shutdown_node(node)
  42. print('%s server output:\n%s' % (version, node.stdout.read().decode('utf-8')))
  43. node = None
  44. create_bwc_index.compress_index(classifier, tmp_dir, 'plugins/%s/src/test/resources/indices/bwc' %plugin)
  45. finally:
  46. if node is not None:
  47. create_bwc_index.shutdown_node(node)
  48. if plugin_installed:
  49. create_bwc_index.remove_plugin(version, release_dir, plugin)
  50. shutil.rmtree(tmp_dir)
  51. def put_plugin_mappings(client, index_name, mapping, docs):
  52. client.indices.delete(index=index_name, ignore=404)
  53. logging.info('Create single shard test index')
  54. client.indices.create(index=index_name, body={
  55. 'settings': {
  56. 'number_of_shards': 1,
  57. 'number_of_replicas': 0
  58. },
  59. 'mappings': {
  60. 'type': mapping
  61. }
  62. })
  63. health = client.cluster.health(wait_for_status='green', wait_for_relocating_shards=0)
  64. assert health['timed_out'] == False, 'cluster health timed out %s' % health
  65. logging.info('Indexing documents')
  66. for i in range(len(docs)):
  67. client.index(index=index_name, doc_type="type", id=str(i), body=docs[i])
  68. logging.info('Flushing index')
  69. client.indices.flush(index=index_name)
  70. logging.info('Running basic checks')
  71. count = client.count(index=index_name)['count']
  72. assert count == len(docs), "expected %d docs, got %d" %(len(docs), count)
  73. def main():
  74. docs = [
  75. {
  76. "foo": "abc"
  77. },
  78. {
  79. "foo": "abcdef"
  80. },
  81. {
  82. "foo": "a"
  83. }
  84. ]
  85. murmur3_mapping = {
  86. 'properties': {
  87. 'foo': {
  88. 'type': 'string',
  89. 'fields': {
  90. 'hash': {
  91. 'type': 'murmur3'
  92. }
  93. }
  94. }
  95. }
  96. }
  97. create_index("mapper-murmur3", murmur3_mapping, docs)
  98. size_mapping = {
  99. '_size': {
  100. 'enabled': True
  101. }
  102. }
  103. create_index("mapper-size", size_mapping, docs)
  104. if __name__ == '__main__':
  105. main()