diff --git a/example/load.py b/example/load.py index 6b6d67e6..20460ccf 100644 --- a/example/load.py +++ b/example/load.py @@ -10,6 +10,7 @@ import logging import git from elasticsearch import Elasticsearch +from elasticsearch.exceptions import TransportError from elasticsearch.helpers import bulk, streaming_bulk def create_git_index(client, index): @@ -17,69 +18,75 @@ def create_git_index(client, index): user_mapping = { 'properties': { 'name': { - 'type': 'multi_field', + 'type': 'string', 'fields': { 'raw': {'type' : 'string', 'index' : 'not_analyzed'}, - 'name': {'type' : 'string'} + } + } + } + } + + create_index_body = { + 'settings': { + # just one shard, no replicas for testing + 'number_of_shards': 1, + 'number_of_replicas': 0, + + # custom analyzer for analyzing file paths + 'analysis': { + 'analyzer': { + 'file_path': { + 'type': 'custom', + 'tokenizer': 'path_hierarchy', + 'filter': ['lowercase'] + } + } + } + }, + 'mappings': { + 'commits': { + '_parent': { + 'type': 'repos' + }, + 'properties': { + 'author': user_mapping, + 'authored_date': {'type': 'date'}, + 'committer': user_mapping, + 'committed_date': {'type': 'date'}, + 'parent_shas': {'type': 'string', 'index' : 'not_analyzed'}, + 'description': {'type': 'string', 'analyzer': 'snowball'}, + 'files': {'type': 'string', 'analyzer': 'file_path'} + } + }, + 'repos': { + 'properties': { + 'owner': user_mapping, + 'created_at': {'type': 'date'}, + 'description': { + 'type': 'string', + 'analyzer': 'snowball', + }, + 'tags': { + 'type': 'string', + 'index': 'not_analyzed' + } } } } } # create empty index - client.indices.create( - index=index, - body={ - 'settings': { - # just one shard, no replicas for testing - 'number_of_shards': 1, - 'number_of_replicas': 0, - - # custom analyzer for analyzing file paths - 'analysis': { - 'analyzer': { - 'file_path': { - 'type': 'custom', - 'tokenizer': 'path_hierarchy', - 'filter': ['lowercase'] - } - } - } - }, - 'mappings': { - 'commits': { - '_parent': { - 'type': 'repos' - }, - 'properties': { - 'author': user_mapping, - 'authored_date': {'type': 'date'}, - 'committer': user_mapping, - 'committed_date': {'type': 'date'}, - 'parent_shas': {'type': 'string', 'index' : 'not_analyzed'}, - 'description': {'type': 'string', 'analyzer': 'snowball'}, - 'files': {'type': 'string', 'analyzer': 'file_path'} - } - }, - 'repos': { - 'properties': { - 'owner': user_mapping, - 'created_at': {'type': 'date'}, - 'description': { - 'type': 'string', - 'analyzer': 'snowball', - }, - 'tags': { - 'type': 'string', - 'index': 'not_analyzed' - } - } - } - } - }, + try: + client.indices.create( + index=index, + body=create_index_body, + ) + except TransportError, e: # ignore already existing index - ignore=400 - ) + if e.error == 'index_already_exists_exception': + pass + else: + raise def parse_commits(head, name): """