Exemplo n.º 1
0
def setup_index():
    """Sets up index and mapping if needed"""
    try:
        es = Elasticsearch(get_elasticsearch_endpoint()).indices

        # create index with mapping if needed
        if not es.exists(index=get_elasticsearch_index()):
            es.create(
                index=get_elasticsearch_index(),
                body='{"mappings": {"properties": {"filter": {' +
                '"type":  "keyword"},"autocomplete": {"type": "completion"' +
                ',"contexts": [{"name": "filter","type": "category","path":' +
                ' "filter"}]}}}}')
        # add mapping if needed
        elif len(
                es.get_field_mapping(fields='autocomplete',
                                     index=get_elasticsearch_index())
            [get_elasticsearch_index()]['mappings']) == 0:
            es.put_mapping(
                index=get_elasticsearch_index(),
                body='{"mappings": {"properties": {"filter": {' +
                '"type":  "keyword"},"autocomplete": {"type": "completion"' +
                ',"contexts": [{"name": "filter","type": "category","path":' +
                ' "filter"}]}}}}')

        # continue workflow
        check_records_to_load_into_xse()
    except Exception as e:
        logging.error(e)
        raise SystemExit('Exiting! Connection error with elastic search')