Exemple #1
0
    def test__esp_search(self, mock_msearch):
        mock_msearch.return_value = {
            "responses": [
                {
                    "hits": {
                        "total": 20,
                        "hits": [
                            {
                                "_index": "blog",
                                "_type": "posts",
                                "_id": "1",
                                "_score": 1.0,
                                "_source": {"account_number": 1,}
                            }, {
                                "_index": "blog",
                                "_type": "posts",
                                "_id": "6",
                                "_score": 1.0,
                                "_source": {"account_number": 6,}
                            }
                        ]
                    }
                }
            ]
        }

        esp = ElasticsearchProcessor()
        esp.add_search({}, 3, 2, index='blog', doc_type='posts')

        bulk_data = copy.deepcopy(esp.bulk_search_data)
        ddtools.assert_equal(bulk_data, [{'index': 'blog', 'type': 'posts'}, {'from': 4, 'size': 2}])

        responses = esp.search()
        mock_msearch.assert_called_with(bulk_data)

        # ensure that our hack to get size and from into the hit
        # data works
        self.assertEqual(responses[0]._page_num, 3)
        self.assertEqual(responses[0]._page_size, 2)

        # ensure that the bulk data gets reset
        self.assertEqual(len(esp.bulk_search_data), 0)

        page = responses[0].page
        self.assertIsInstance(page, Page)
        self.assertEqual(page.number, 3)
        self.assertTrue(page.has_next())
        self.assertTrue(page.has_previous())
        self.assertEqual(len(page), 2)  # 2 items on the page

        mock_msearch.reset()
        esp.add_search({}, 1, 2, index='blog', doc_type='posts')
        responses = esp.search()
        page = responses[0].page
        self.assertTrue(page.has_next())
        self.assertFalse(page.has_previous())
Exemple #2
0
    def test__esp_add_query_form(self):
        esp = ElasticsearchProcessor()

        page = 1
        page_size = 20

        query = self.form.prepare_query()

        # ElasticsearchProcessor internally sets the from/size parameters
        # on the query; we need to compare with those values included
        query_with_size = query.copy()
        query_with_size.update({
            'from': (page - 1) * page_size,
            'size': page_size
        })

        esp.add_search(self.form)
        ddtools.assert_equal(esp.bulk_search_data[0], {'index': 'blog', 'type': 'posts'})
        ddtools.assert_equal(esp.bulk_search_data[1], query_with_size)
Exemple #3
0
    def test__esp_reset(self):
        esp = ElasticsearchProcessor()

        self.assertTrue(len(esp.bulk_search_data) == 0)
        self.assertTrue(len(esp.page_ranges) == 0)

        esp.add_search(self.form)

        self.assertFalse(len(esp.bulk_search_data) == 0)
        self.assertFalse(len(esp.page_ranges) == 0)

        esp.reset()

        self.assertTrue(len(esp.bulk_search_data) == 0)
        self.assertTrue(len(esp.page_ranges) == 0)
Exemple #4
0
    def test__esp_add_query_dslquery(self):
        page = 1
        page_size = 20

        query = {
            "query": {
                "match": {
                    "_all": "foobar"
                }
            }
        }

        s = Search.from_dict(query.copy())

        # ElasticsearchProcessor internally sets the from/size parameters
        # on the query; we need to compare with those values included
        query_with_size = query.copy()
        query_with_size.update({
            'from': (page - 1) * page_size,
            'size': page_size
        })

        esp = ElasticsearchProcessor()
        esp.add_search(s)
        ddtools.assert_equal(esp.bulk_search_data[0], {})
        ddtools.assert_equal(esp.bulk_search_data[1], query_with_size)

        esp.reset()
        esp.add_search(s, index='blog')
        ddtools.assert_equal(esp.bulk_search_data[0], {'index': 'blog'})
        ddtools.assert_equal(esp.bulk_search_data[1], query_with_size)

        esp.reset()
        esp.add_search(s, index='blog', doc_type='posts')
        ddtools.assert_equal(esp.bulk_search_data[0], {'index': 'blog', 'type': 'posts'})
        ddtools.assert_equal(esp.bulk_search_data[1], query_with_size)

        s = s.index('blog').params(routing='id')

        esp.reset()
        esp.add_search(s)
        ddtools.assert_equal(esp.bulk_search_data[0], {'index': ['blog'], 'routing': 'id'})
        ddtools.assert_equal(esp.bulk_search_data[1], query_with_size)

        s = s.doc_type('posts')

        esp.reset()
        esp.add_search(s)
        ddtools.assert_equal(esp.bulk_search_data[0], {'index': ['blog'], 'type': ['posts'], 'routing': 'id'})
        ddtools.assert_equal(esp.bulk_search_data[1], query_with_size)