=====
Index
=====

This test will setup some sample data in our test setup method. After that a
new elasticsearch instance in another sandbox is started for this test. Check
the p01/elasticsearch/test.py file for more info about the sample data and
elasticsearch server setup.

We will test if we can delete an existing index and create them with the same
mapping again:

  >>> import json
  >>> from pprint import pprint
  >>> import p01.elasticsearch.testing
  >>> statusRENormalizer = p01.elasticsearch.testing.statusRENormalizer

Now let's define a new elasticsearch connection based on our server pool:

  >>> conn = p01.elasticsearch.testing.getTestConnection()

Now we are ready to access the elasticsearch server. Check the status:

  >>> statusRENormalizer.pprint(conn.status())
  {u'_shards': {u'failed': 0, u'successful': 1, u'total': 1},
   u'indices': {u'companies': {u'docs': {u'deleted_docs': 0,
                                         u'max_doc': ...,
                                         u'num_docs': ...},
                               u'flush': {u'total': 0,
                                          u'total_time': u'...',
                                          u'total_time_in_millis': ...},
                               u'index': {u'primary_size': u'...',
                                          u'primary_size_in_bytes': ...,
                                          u'size': u'...',
                                          u'size_in_bytes': ...},
                               u'merges': {u'current': 0,
                                           u'current_docs': 0,
                                           u'current_size': u'0b',
                                           u'current_size_in_bytes': 0,
                                           u'total': 0,
                                           u'total_docs': 0,
                                           u'total_size': u'0b',
                                           u'total_size_in_bytes': 0,
                                           u'total_time': u'...',
                                           u'total_time_in_millis': ...},
                               u'refresh': {u'total': ...,
                                            u'total_time': u'...',
                                            u'total_time_in_millis': ...},
                               u'shards': {u'0': [{u'docs': {u'deleted_docs': 0,
                                                             u'max_doc': ...,
                                                             u'num_docs': ...},
                                                   u'flush': {u'total': 0,
                                                              u'total_time': u'...',
                                                              u'total_time_in_millis': ...},
                                                   u'index': {u'size': u'...',
                                                              u'size_in_bytes': ...},
                                                   u'merges': {u'current': 0,
                                                               u'current_docs': 0,
                                                               u'current_size': u'0b',
                                                               u'current_size_in_bytes': 0,
                                                               u'total': 0,
                                                               u'total_docs': 0,
                                                               u'total_size': u'0b',
                                                               u'total_size_in_bytes': 0,
                                                               u'total_time': u'...',
                                                               u'total_time_in_millis': ...},
                                                   u'refresh': {u'total': ...,
                                                                u'total_time': u'...',
                                                                u'total_time_in_millis': ...},
                                                   u'routing': {u'index': u'companies',
                                                                u'node': u'...',
                                                                u'primary': True,
                                                                u'relocating_node': None,
                                                                u'shard': 0,
                                                                u'state': u'STARTED'},
                                                   u'state': u'STARTED',
                                                   u'translog': {u'id': ...,
                                                                 u'operations': 0}}]},
                               u'translog': {u'operations': 0}}},
   u'ok': True}


As you can see, we can test our sample data created mapping:

  >>> pprint(conn.getMapping('companies', 'company'))
  {u'company': {u'properties': {u'__name__': {u'type': u'string'},
                                u'city': {u'type': u'string'},
                                u'number': {u'ignore_malformed': False,
                                            u'type': u'long'},
                                u'street': {u'type': u'string'},
                                u'text': {u'type': u'string'},
                                u'zip': {u'type': u'string'}}}}

And search for our sample data where we added within our sample data generator
in our test setup:

  >>> pprint(conn.search('street').total)
  100


deleteIndex
-----------

Now we will delete the index:

  >>> conn.deleteIndex('companies')
  {u'acknowledged': True, u'ok': True}

As you can see there is no index anymore:

  >>> statusRENormalizer.pprint(conn.status())
  {u'_shards': {u'failed': 0, u'successful': 0, u'total': 0},
   u'indices': {},
   u'ok': True}


createIndex
-----------

Now we can create the index again. Let's get our sample data mapping:

  >>> import os.path
  >>> import json
  >>> import p01.elasticsearch
  >>> mFile = os.path.join(os.path.dirname(p01.elasticsearch.__file__),
  ...     'sample', 'config', 'companies', 'company.json')

  >>> f = open(mFile)
  >>> data = f.read()
  >>> f.close()
  >>> mappings = json.loads(data)
  >>> pprint(mappings)
  {u'company': {u'_all': {u'enabled': True, u'store': u'yes'},
                u'_id': {u'store': u'yes'},
                u'_index': {u'enabled': True},
                u'_source': {u'enabled': False},
                u'_type': {u'store': u'yes'},
                u'properties': {u'__name__': {u'include_in_all': False,
                                              u'index': u'not_analyzed',
                                              u'store': u'yes',
                                              u'type': u'string'},
                                u'_id': {u'include_in_all': False,
                                         u'index': u'no',
                                         u'store': u'yes',
                                         u'type': u'string'},
                                u'city': {u'boost': 1.0,
                                          u'include_in_all': True,
                                          u'index': u'not_analyzed',
                                          u'null_value': u'na',
                                          u'store': u'yes',
                                          u'type': u'string'},
                                u'street': {u'boost': 1.0,
                                            u'include_in_all': True,
                                            u'index': u'not_analyzed',
                                            u'null_value': u'na',
                                            u'store': u'yes',
                                            u'type': u'string'},
                                u'text': {u'boost': 1.0,
                                          u'include_in_all': True,
                                          u'index': u'not_analyzed',
                                          u'null_value': u'na',
                                          u'store': u'yes',
                                          u'type': u'string'},
                                u'zip': {u'boost': 1.0,
                                         u'include_in_all': True,
                                         u'index': u'not_analyzed',
                                         u'null_value': u'na',
                                         u'store': u'yes',
                                         u'type': u'string'}}}}

Now we can create an new index with the given mapping:

  >>> conn.createIndex('companies', mappings=mappings)
  {u'acknowledged': True, u'ok': True}

As you can see, our index and mapping is back again:

  >>> statusRENormalizer.pprint(conn.status())
  {u'_shards': {u'failed': 0, u'successful': 1, u'total': 1},
   u'indices': {u'companies': {u'docs': {u'deleted_docs': 0,
                                         u'max_doc': ...,
                                         u'num_docs': ...},
                               u'flush': {u'total': 0,
                                          u'total_time': u'...',
                                          u'total_time_in_millis': ...},
                               u'index': {u'primary_size': u'...',
                                          u'primary_size_in_bytes': ...,
                                          u'size': u'...',
                                          u'size_in_bytes': ...},
                               u'merges': {u'current': 0,
                                           u'current_docs': 0,
                                           u'current_size': u'0b',
                                           u'current_size_in_bytes': 0,
                                           u'total': 0,
                                           u'total_docs': 0,
                                           u'total_size': u'0b',
                                           u'total_size_in_bytes': 0,
                                           u'total_time': u'...',
                                           u'total_time_in_millis': ...},
                               u'refresh': {u'total': ...,
                                            u'total_time': u'...',
                                            u'total_time_in_millis': ...},
                               u'shards': {u'0': [{u'docs': {u'deleted_docs': 0,
                                                             u'max_doc': ...,
                                                             u'num_docs': ...},
                                                   u'flush': {u'total': 0,
                                                              u'total_time': u'...',
                                                              u'total_time_in_millis': ...},
                                                   u'index': {u'size': u'...',
                                                              u'size_in_bytes': ...},
                                                   u'merges': {u'current': 0,
                                                               u'current_docs': 0,
                                                               u'current_size': u'0b',
                                                               u'current_size_in_bytes': 0,
                                                               u'total': 0,
                                                               u'total_docs': 0,
                                                               u'total_size': u'0b',
                                                               u'total_size_in_bytes': 0,
                                                               u'total_time': u'...',
                                                               u'total_time_in_millis': ...},
                                                   u'refresh': {u'total': ...,
                                                                u'total_time': u'...',
                                                                u'total_time_in_millis': ...},
                                                   u'routing': {u'index': u'companies',
                                                                u'node': u'...',
                                                                u'primary': True,
                                                                u'relocating_node': None,
                                                                u'shard': 0,
                                                                u'state': u'STARTED'},
                                                   u'state': u'STARTED',
                                                   u'translog': {u'id': ...,
                                                                 u'operations': 0}}]},
                               u'translog': {u'operations': 0}}},
   u'ok': True}

  >>> pprint(conn.getMapping('companies', 'company'))
  {u'company': {u'_all': {u'store': u'yes'},
                u'_id': {u'store': u'yes'},
                u'_index': {u'enabled': True},
                u'_source': {u'enabled': False},
                u'_type': {u'store': u'yes'},
                u'properties': {u'__name__': {u'include_in_all': False,
                                              u'index': u'not_analyzed',
                                              u'store': u'yes',
                                              u'type': u'string'},
                                u'city': {u'include_in_all': True,
                                          u'index': u'not_analyzed',
                                          u'null_value': u'na',
                                          u'store': u'yes',
                                          u'type': u'string'},
                                u'street': {u'include_in_all': True,
                                            u'index': u'not_analyzed',
                                            u'null_value': u'na',
                                            u'store': u'yes',
                                            u'type': u'string'},
                                u'text': {u'include_in_all': True,
                                          u'index': u'not_analyzed',
                                          u'null_value': u'na',
                                          u'store': u'yes',
                                          u'type': u'string'},
                                u'zip': {u'include_in_all': True,
                                         u'index': u'not_analyzed',
                                         u'null_value': u'na',
                                         u'store': u'yes',
                                         u'type': u'string'}}}}

As you can see the index is empty:

  >>> pprint(conn.search('street').total)
  0
