示例#1
0
    def setUp(self):
        """ If you're reading this. I am gone and dead, presumably.
            Elasticsearch's logging is quite loud and lets us know
            about anticipated errors, so I set the level to ERROR only.
            If elasticsearch is giving you trouble in tests and you
            aren't seeing any info, get rid of this. God bless you.
        """
        self.elasticsearchLogger.setLevel(logging.ERROR)
        self.es = connections.get_connection("default")
        self.indexes = get_indexes()

        for index in list(self.indexes):
            self.es.indices.delete_alias("{}*".format(index), "_all", ignore=[404])
            self.es.indices.delete("{}*".format(index), ignore=[404])

        for index, body in self.indexes.items():
            sync_index(index, body)
示例#2
0
文件: conftest.py 项目: theonion/djes
def es_client(request):
    es = connections.get_connection("default")
    indexes = get_indexes()

    for index in list(indexes):
        es.indices.delete_alias("{}_*".format(index), "_all", ignore=[404])
        es.indices.delete("{}_*".format(index), ignore=[404])

    for index, body in indexes.items():
        sync_index(index, body)

    def fin():
        for index in list(indexes):
            es.indices.delete_alias("{}_*".format(index), "_all", ignore=[404])
            es.indices.delete("{}_*".format(index), ignore=[404])

    request.addfinalizer(fin)
    return es  # provide the fixture value
示例#3
0
文件: test.py 项目: theonion/djes
    def setUp(self):
        """ If you're reading this. I am gone and dead, presumably.
            Elasticsearch's logging is quite loud and lets us know
            about anticipated errors, so I set the level to ERROR only.
            If elasticsearch is giving you trouble in tests and you
            aren't seeing any info, get rid of this. God bless you.
        """
        self.elasticsearchLogger.setLevel(logging.ERROR)
        self.es = connections.get_connection("default")
        self.indexes = get_indexes()

        for index in list(self.indexes):
            self.es.indices.delete_alias("{}*".format(index),
                                         "_all",
                                         ignore=[404])
            self.es.indices.delete("{}*".format(index), ignore=[404])

        for index, body in self.indexes.items():
            sync_index(index, body)
示例#4
0
def test_sync_index_exists_without_version(es_client):
    alias_name = 'djes-integration'
    es_client.indices.delete_alias(alias_name + '*', '_all', ignore=[404])
    es_client.indices.delete(alias_name + '*', ignore=[404])

    settings_body = {
        "settings": {
            "index": {
                "number_of_replicas": "1",
                "analysis": {
                    "analyzer": {
                        "autocomplete": {
                            "type": "custom",
                            "tokenizer": "standard",
                            "char_filter": ["html_strip"],
                            "filter": ["lowercase", "stop", "snowball"]
                        }
                    }
                }
            },
        },
        "mappings": {
            "testing": {
                "properties": {
                    "foo": {
                        "type": "string"
                    }
                }
            }
        }
    }
    alias = es_client.indices.get_alias(alias_name)
    assert not alias

    sync_index(alias_name, body=settings_body)
    assert es_client.indices.exists('{}_0001'.format(alias_name))

    sync_index(alias_name, body=settings_body)
    assert es_client.indices.exists('{}_0001'.format(alias_name))
示例#5
0
def test_sync_index(es_client):
    # Some setup code
    es_client.indices.delete_alias("djes-testing-index_*", "_all", ignore=[404])
    es_client.indices.delete("djes-testing-index_*", ignore=[404])

    settings_body = {
        "settings": {
            "index": {
                "number_of_replicas": "1",

                "analysis": {
                    "analyzer": {
                        "autocomplete": {
                            "type": "custom",
                            "tokenizer": "standard",
                            "char_filter":  ["html_strip"],
                            "filter": ["lowercase", "stop", "snowball"]
                        }
                    }
                }
            },
        },
        "mappings": {
            "testing": {
                "properties": {
                    "foo": {"type": "string"}
                }
            }
        }
    }
    sync_index("djes-testing-index", body=settings_body)

    assert es_client.indices.exists("djes-testing-index_0001")
    assert es_client.indices.get_alias("djes-testing-index") == {
        "djes-testing-index_0001": {
            "aliases": {"djes-testing-index": {}}
        }
    }

    # Let's sync again, this should update the settings
    settings_body["settings"]["index"]["number_of_replicas"] = "2"
    sync_index("djes-testing-index", body=settings_body)
    assert es_client.indices.exists("djes-testing-index_0001")
    assert es_client.indices.exists("djes-testing-index_0002") is False

    # Make sure the settings took
    new_settings = es_client.indices.get_settings("djes-testing-index_0001")
    new_settings = new_settings["djes-testing-index_0001"]["settings"]
    assert new_settings["index"]["number_of_replicas"] == "2"

    # Now let's add another mapping
    settings_body["mappings"]["testing-two"] = {
        "properties": {
            "bar": {"type": "integer"}
        }
    }
    sync_index("djes-testing-index", body=settings_body)

    # Make sure the new mapping got added
    assert es_client.indices.exists("djes-testing-index_0001")
    assert es_client.indices.exists("djes-testing-index_0002") is False
    new_mapping = es_client.indices.get_mapping(index="djes-testing-index", doc_type="testing-two")
    new_mapping = new_mapping["djes-testing-index_0001"]
    assert new_mapping["mappings"]["testing-two"] == settings_body["mappings"]["testing-two"]

    # Now lets add another field to that mapping
    settings_body["mappings"]["testing-two"] = {
        "properties": {
            "bar": {"type": "integer"},
            "baz": {"type": "string"}
        }
    }
    sync_index("djes-testing-index", body=settings_body)
    assert es_client.indices.exists("djes-testing-index_0001")
    assert es_client.indices.exists("djes-testing-index_0002") is False
    new_mapping = es_client.indices.get_mapping(index="djes-testing-index", doc_type="testing-two")
    new_mapping = new_mapping["djes-testing-index_0001"]
    assert new_mapping["mappings"]["testing-two"] == settings_body["mappings"]["testing-two"]

    # Now let's add a mapping that will error out
    settings_body["mappings"]["testing-two"] = {
        "properties": {
            "bar": {"type": "integer"},
            "baz": {"type": "long"}
        }
    }
    sync_index("djes-testing-index", body=settings_body)

    assert es_client.indices.exists("djes-testing-index_0001")
    assert es_client.indices.exists("djes-testing-index_0002")
    new_mapping = es_client.indices.get_mapping(index="djes-testing-index", doc_type="testing-two")
    new_mapping = new_mapping["djes-testing-index_0002"]
    assert new_mapping["mappings"]["testing-two"] == settings_body["mappings"]["testing-two"]

    # Add another mapping
    settings_body["mappings"]["testing-two"] = {
        "properties": {
            "foo": {"type": "long"},
            "baz": {"type": "integer"}
        }
    }
    sync_index("djes-testing-index", body=settings_body)
    assert es_client.indices.exists("djes-testing-index_0001")
    assert es_client.indices.exists("djes-testing-index_0002")
    assert es_client.indices.exists("djes-testing-index_0003")

    es_client.indices.delete_alias("djes-testing-index_*", "_all", ignore=[404])
    es_client.indices.delete("djes-testing-index_*", ignore=[404])
示例#6
0
def test_sync_index(es_client):
    # Some setup code
    es_client.indices.delete_alias("djes-testing-index_*",
                                   "_all",
                                   ignore=[404])
    es_client.indices.delete("djes-testing-index_*", ignore=[404])

    settings_body = {
        "settings": {
            "index": {
                "number_of_replicas": "1",
                "analysis": {
                    "analyzer": {
                        "autocomplete": {
                            "type": "custom",
                            "tokenizer": "standard",
                            "char_filter": ["html_strip"],
                            "filter": ["lowercase", "stop", "snowball"]
                        }
                    }
                }
            },
        },
        "mappings": {
            "testing": {
                "properties": {
                    "foo": {
                        "type": "string"
                    }
                }
            }
        }
    }
    sync_index("djes-testing-index", body=settings_body)

    assert es_client.indices.exists("djes-testing-index_0001")
    assert es_client.indices.get_alias("djes-testing-index") == {
        "djes-testing-index_0001": {
            "aliases": {
                "djes-testing-index": {}
            }
        }
    }

    # Let's sync again, this should update the settings
    settings_body["settings"]["index"]["number_of_replicas"] = "2"
    sync_index("djes-testing-index", body=settings_body)
    assert es_client.indices.exists("djes-testing-index_0001")
    assert es_client.indices.exists("djes-testing-index_0002") is False

    # Make sure the settings took
    new_settings = es_client.indices.get_settings("djes-testing-index_0001")
    new_settings = new_settings["djes-testing-index_0001"]["settings"]
    assert new_settings["index"]["number_of_replicas"] == "2"

    # Now let's add another mapping
    settings_body["mappings"]["testing-two"] = {
        "properties": {
            "bar": {
                "type": "integer"
            }
        }
    }
    sync_index("djes-testing-index", body=settings_body)

    # Make sure the new mapping got added
    assert es_client.indices.exists("djes-testing-index_0001")
    assert es_client.indices.exists("djes-testing-index_0002") is False
    new_mapping = es_client.indices.get_mapping(index="djes-testing-index",
                                                doc_type="testing-two")
    new_mapping = new_mapping["djes-testing-index_0001"]
    assert new_mapping["mappings"]["testing-two"] == settings_body["mappings"][
        "testing-two"]

    # Now lets add another field to that mapping
    settings_body["mappings"]["testing-two"] = {
        "properties": {
            "bar": {
                "type": "integer"
            },
            "baz": {
                "type": "string"
            }
        }
    }
    sync_index("djes-testing-index", body=settings_body)
    assert es_client.indices.exists("djes-testing-index_0001")
    assert es_client.indices.exists("djes-testing-index_0002") is False
    new_mapping = es_client.indices.get_mapping(index="djes-testing-index",
                                                doc_type="testing-two")
    new_mapping = new_mapping["djes-testing-index_0001"]
    assert new_mapping["mappings"]["testing-two"] == settings_body["mappings"][
        "testing-two"]

    # Now let's add a mapping that will error out
    settings_body["mappings"]["testing-two"] = {
        "properties": {
            "bar": {
                "type": "integer"
            },
            "baz": {
                "type": "long"
            }
        }
    }
    sync_index("djes-testing-index", body=settings_body)

    assert es_client.indices.exists("djes-testing-index_0001")
    assert es_client.indices.exists("djes-testing-index_0002")
    new_mapping = es_client.indices.get_mapping(index="djes-testing-index",
                                                doc_type="testing-two")
    new_mapping = new_mapping["djes-testing-index_0002"]
    assert new_mapping["mappings"]["testing-two"] == settings_body["mappings"][
        "testing-two"]

    # Add another mapping
    settings_body["mappings"]["testing-two"] = {
        "properties": {
            "foo": {
                "type": "long"
            },
            "baz": {
                "type": "integer"
            }
        }
    }
    sync_index("djes-testing-index", body=settings_body)
    assert es_client.indices.exists("djes-testing-index_0001")
    assert es_client.indices.exists("djes-testing-index_0002")
    assert es_client.indices.exists("djes-testing-index_0003")

    es_client.indices.delete_alias("djes-testing-index_*",
                                   "_all",
                                   ignore=[404])
    es_client.indices.delete("djes-testing-index_*", ignore=[404])