mercredi 14 septembre 2016

Elasticsearch "get by index" returns the document, while "match_all" returns no results

I am trying to mock elasticsearch data for CI unittesting purposes.

I have some fixtures that I can successfully load with bulk(), but then, for some reason, I cannot match anything, even if the index seemingly contains the documents (because I can get them by their IDs).

The fixtures is a subset of documents that I fetched from real index I am using on production. With production index, all tests pass as expected.

The example of strange behaviour follows:

class MyTestCase(TestCase):
    es = Elasticsearch()

    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.es.indices.create('test_index', SCHEMA)

        with open('fixtures.json') as fixtures:
            bulk(cls.es, json.load(fixtures))

    @classmethod
    def tearDownClass(cls):
        super().tearDownClass()
        cls.es.indices.delete('test_index')

    def test_something(self):
        # check all documents are there:
        with open('fixtures.json') as fixtures:
            for f in json.load(fixtures):
                print(self.es.get(index=self.INDEX_NAME, id=f['_id']))
                # yes they are!

        # BUT:
        match_all = {"query": {"match_all": {}}}
        print('hits:', self.es.search(index='test_index', body=match_all)['hits']['hits'])
        # prints `hits: []` like there was nothing in

        print('count:', self.es.count(index='test_index', body=match_all)['count'])
        # prints `count: 0`

Aucun commentaire:

Enregistrer un commentaire