コード例 #1
0
ファイル: test_esqueryset.py プロジェクト: soitun/commcare-hq
 def test_exclude_source(self):
     hits = ['8063dff5-460b-46f2-b4d0-5871abfd97d4', 'dc1376cd-0869-4c13-a267-365dfc2fa754']
     response = ESQuerySet(
         self.example_response,
         HQESQuery('forms').exclude_source()
     )
     self.assertEqual(response.hits, hits)
コード例 #2
0
ファイル: test_esqueryset.py プロジェクト: zbidi/commcare-hq
 def test_response(self):
     hits = [{
         u'app_id':
         u'fe8481a39c3738749e6a4766fca99efd',
         u'doc_type':
         u'xforminstance',
         u'domain':
         u'mikesproject',
         u'xmlns':
         u'http://openrosa.org/formdesigner/3a7cc07c-551c-4651-ab1a-d60be3017485'
     }, {
         u'app_id':
         u'3d622620ca00d7709625220751a7b1f9',
         u'doc_type':
         u'xforminstance',
         u'domain':
         u'mikesproject',
         u'xmlns':
         u'http://openrosa.org/formdesigner/54db1962-b938-4e2b-b00e-08414163ead4'
     }]
     fields = [u'app_id', u'doc_type', u'domain', u'xmlns']
     response = ESQuerySet(self.example_response,
                           HQESQuery('forms').fields(fields))
     self.assertEquals(response.total, 5247)
     self.assertEquals(response.hits, hits)
コード例 #3
0
ファイル: test_esqueryset.py プロジェクト: soitun/commcare-hq
    def test_flatten_field_dicts(self):
        example_response = {
            'hits': {'hits': [{
                '_source': {
                    'domains': ['joesproject'],
                    }
                },
                {
                    '_source': {
                        'domains': ['mikesproject']
                        }
                    }
                ],
            },
        }

        hits = [
            {
                'domains': 'joesproject',
            },
            {
                'domains': 'mikesproject',
            }
        ]
        fields = ['domain']
        response = ESQuerySet(
            example_response,
            HQESQuery('forms').fields(fields)
        )
        self.assertEqual(response.hits, hits)
コード例 #4
0
ファイル: es_query_fake.py プロジェクト: twymer/commcare-hq
    def run(self):
        result_docs = list(self._result_docs)
        total = len(result_docs)
        if self._sort_field:
            result_docs.sort(key=lambda doc: doc[self._sort_field],
                             reverse=self._sort_desc)
        if self._size is not None:
            result_docs = result_docs[self._start:self._start + self._size]
        else:
            result_docs = result_docs[self._start:]

        def _get_doc(doc):
            if self._source_fields:
                return {
                    key: doc[key]
                    for key in self._source_fields if key in doc
                }
            return doc

        return ESQuerySet(
            {
                'hits': {
                    'hits': [{
                        '_source': _get_doc(doc)
                    } for doc in result_docs],
                    'total': total,
                },
            }, self)
コード例 #5
0
    def run(self):
        result_docs = list(self._result_docs)
        total = len(result_docs)
        if self._sort_field:
            result_docs.sort(key=lambda doc: doc[self._sort_field],
                             reverse=self._sort_desc)
        if self._size is not None:
            result_docs = result_docs[self._start:self._start + self._size]
        else:
            result_docs = result_docs[self._start:]

        return ESQuerySet({
            'hits': {
                'hits': [{'_source': doc} for doc in result_docs],
                'total': total,
            },
        }, self)
コード例 #6
0
ファイル: es_query_fake.py プロジェクト: dimagi/commcare-hq
    def scroll(self):
        result_docs = list(self._result_docs)
        total = len(result_docs)
        if self._sort_field:
            result_docs.sort(key=lambda doc: doc[self._sort_field],
                             reverse=self._sort_desc)
        if self._size is not None:
            result_docs = result_docs[self._start:self._start + self._size]
        else:
            result_docs = result_docs[self._start:]

        def _get_doc(doc):
            if self._source_fields:
                return {key: doc[key] for key in self._source_fields if key in doc}
            return doc

        es_query_set = (ESQuerySet.normalize_result(self,
                                                    {'_source': _get_doc(r)}) for r in result_docs)
        return ScanResult(total, es_query_set)
コード例 #7
0
    def scroll(self):
        result_docs = list(self._result_docs)
        total = len(result_docs)
        if self._sort_field:
            result_docs.sort(key=lambda doc: doc[self._sort_field],
                             reverse=self._sort_desc)
        if self._size is not None:
            result_docs = result_docs[self._start:self._start + self._size]
        else:
            result_docs = result_docs[self._start:]

        def _get_doc(doc):
            if self._source_fields:
                return {key: doc[key] for key in self._source_fields if key in doc}
            return doc

        es_query_set = (ESQuerySet.normalize_result(self,
                                                    {'_source': _get_doc(r)}) for r in result_docs)
        return ScanResult(total, es_query_set)
コード例 #8
0
    def test_result_parsing_basic(self):
        query = HQESQuery('cases').aggregations([
            FilterAggregation('closed', filters.term('closed', True)),
            FilterAggregation('open', filters.term('closed', False))
        ])

        raw_result = {
            "aggregations": {
                "closed": {
                    "doc_count": 1
                },
                "open": {
                    "doc_count": 2
                }
            }
        }
        queryset = ESQuerySet(raw_result, deepcopy(query))
        self.assertEqual(queryset.aggregations.closed.doc_count, 1)
        self.assertEqual(queryset.aggregations.open.doc_count, 2)
コード例 #9
0
 def test_histogram_aggregation(self):
     example_response = {
         "hits": {},
         "shards": {},
         "aggregations": {
             "forms_by_date": {
                 "buckets": [{
                     "key": 1454284800000,
                     "doc_count": 8
                 }, {
                     "key": 1464284800000,
                     "doc_count": 3
                 }]
             }
         }
     }
     expected_output = example_response['aggregations']['forms_by_date'][
         'buckets']
     query = HQESQuery('forms').date_histogram('forms_by_date', '', '')
     res = ESQuerySet(example_response, query)
     output = res.aggregations.forms_by_date.raw_buckets
     self.assertEqual(output, expected_output)
コード例 #10
0
ファイル: test_esqueryset.py プロジェクト: zbidi/commcare-hq
 def test_error(self):
     with self.assertRaises(ESError):
         ESQuerySet(self.example_error, HQESQuery('forms'))
コード例 #11
0
    def test_result_parsing_complex(self):
        query = HQESQuery('cases').aggregation(
            TermsAggregation("users", 'user_id').aggregation(
                FilterAggregation('closed', filters.term(
                    'closed', True))).aggregation(
                        FilterAggregation('open', filters.term(
                            'closed', False)))).aggregation(
                                RangeAggregation('by_date', 'name', [
                                    AggregationRange(end='c'),
                                    AggregationRange(start='f'),
                                    AggregationRange(start='k', end='p')
                                ]))

        raw_result = {
            "aggregations": {
                "users": {
                    "buckets": [{
                        "closed": {
                            "doc_count": 0
                        },
                        "doc_count": 2,
                        "key": "user1",
                        "open": {
                            "doc_count": 2
                        }
                    }],
                    "doc_count_error_upper_bound":
                    0,
                    "sum_other_doc_count":
                    0
                },
                "by_date": {
                    "buckets": {
                        "*-c": {
                            "to": "c",
                            "doc_count": 3
                        },
                        "f-*": {
                            "from": "f",
                            "doc_count": 8
                        },
                        "k-p": {
                            "from": "k",
                            "to": "p",
                            "doc_count": 6
                        }
                    }
                }
            },
        }
        queryset = ESQuerySet(raw_result, deepcopy(query))
        self.assertEqual(queryset.aggregations.users.buckets.user1.key,
                         'user1')
        self.assertEqual(queryset.aggregations.users.buckets.user1.doc_count,
                         2)
        self.assertEqual(
            queryset.aggregations.users.buckets.user1.closed.doc_count, 0)
        self.assertEqual(
            queryset.aggregations.users.buckets.user1.open.doc_count, 2)
        self.assertEqual(
            queryset.aggregations.users.buckets_dict['user1'].open.doc_count,
            2)
        self.assertEqual(queryset.aggregations.users.counts_by_bucket(),
                         {'user1': 2})
        self.assertEqual(queryset.aggregations.by_date.counts_by_bucket(), {
            '*-c': 3,
            'f-*': 8,
            'k-p': 6,
        })