summaryrefslogtreecommitdiff
path: root/dev-tools/create_bwc_index_with_plugin_mappings.py
blob: c30de412d1d4eaf92ae3fdf038800221f1ec415a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import create_bwc_index
import logging
import os
import random
import shutil
import subprocess
import sys
import tempfile

def fetch_version(version):
  logging.info('fetching ES version %s' % version)
  if subprocess.call([sys.executable, os.path.join(os.path.split(sys.argv[0])[0], 'get-bwc-version.py'), version]) != 0:
    raise RuntimeError('failed to download ES version %s' % version)

def create_index(plugin, mapping, docs):
  '''
  Creates a static back compat index (.zip) with mappings using fields defined in plugins.
  '''
  
  logging.basicConfig(format='[%(levelname)s] [%(asctime)s] %(message)s', level=logging.INFO,
                      datefmt='%Y-%m-%d %I:%M:%S %p')
  logging.getLogger('elasticsearch').setLevel(logging.ERROR)
  logging.getLogger('urllib3').setLevel(logging.WARN)

  tmp_dir = tempfile.mkdtemp()
  plugin_installed = False
  node = None
  try:
    data_dir = os.path.join(tmp_dir, 'data')
    repo_dir = os.path.join(tmp_dir, 'repo')
    logging.info('Temp data dir: %s' % data_dir)
    logging.info('Temp repo dir: %s' % repo_dir)

    version = '2.0.0'
    classifier = '%s-%s' %(plugin, version)
    index_name = 'index-%s' % classifier

    # Download old ES releases if necessary:
    release_dir = os.path.join('backwards', 'elasticsearch-%s' % version)
    if not os.path.exists(release_dir):
      fetch_version(version)

    create_bwc_index.install_plugin(version, release_dir, plugin)
    plugin_installed = True
    node = create_bwc_index.start_node(version, release_dir, data_dir, repo_dir, cluster_name=index_name)
    client = create_bwc_index.create_client()
    put_plugin_mappings(client, index_name, mapping, docs)
    create_bwc_index.shutdown_node(node)

    print('%s server output:\n%s' % (version, node.stdout.read().decode('utf-8')))
    node = None
    create_bwc_index.compress_index(classifier, tmp_dir, 'plugins/%s/src/test/resources/indices/bwc' %plugin)
  finally:
    if node is not None:
      create_bwc_index.shutdown_node(node)
    if plugin_installed:
      create_bwc_index.remove_plugin(version, release_dir, plugin)
    shutil.rmtree(tmp_dir)

def put_plugin_mappings(client, index_name, mapping, docs):
  client.indices.delete(index=index_name, ignore=404)
  logging.info('Create single shard test index')

  client.indices.create(index=index_name, body={
    'settings': {
      'number_of_shards': 1,
      'number_of_replicas': 0
    },
    'mappings': {
      'type': mapping
    }
  })
  health = client.cluster.health(wait_for_status='green', wait_for_relocating_shards=0)
  assert health['timed_out'] == False, 'cluster health timed out %s' % health

  logging.info('Indexing documents')
  for i in range(len(docs)):
    client.index(index=index_name, doc_type="type", id=str(i), body=docs[i])
  logging.info('Flushing index')
  client.indices.flush(index=index_name)

  logging.info('Running basic checks')
  count = client.count(index=index_name)['count']
  assert count == len(docs), "expected %d docs, got %d" %(len(docs), count)

def main():
  docs = [
    {
      "foo": "abc"
    },
    {
      "foo": "abcdef"
    },
    {
      "foo": "a"
    }
  ]

  murmur3_mapping = {
    'properties': {
      'foo': {
        'type': 'string',
        'fields': {
          'hash': {
            'type': 'murmur3'
          }
        }
      }
    }
  }

  create_index("mapper-murmur3", murmur3_mapping, docs)

  size_mapping = {
    '_size': {
      'enabled': True
    }
  }

  create_index("mapper-size", size_mapping, docs)

if __name__ == '__main__':
  main()