コード例 #1
0
ファイル: filters.py プロジェクト: nmabhinandan/mfl_api
    def filter(self, qs, value):
        super(SearchFilter, self).filter(qs, value)
        api = ElasticAPI()

        document_type = qs.model
        index_name = settings.SEARCH.get('INDEX_NAME')
        if self.search_type == 'full_text':
            result = api.search_document(index_name, document_type, value)
        else:
            result = api.search_auto_complete_document(
                index_name, document_type, value)

        hits = []
        try:
            hits = result.json().get('hits').get('hits') if result.json() \
                else hits
        except AttributeError:
            hits = hits

        hits_ids_list = [str(hit.get('_id')) for hit in hits]

        pk_list = hits_ids_list

        if qs.model._meta.managed:
            table_name = "{0}_{1}.id".format(
                qs.model._meta.app_label, qs.model.__name__.lower())
        else:
            table_name = "{}.id".format(qs.model._meta.db_table)

        clauses = ' '.join(
            [
                "WHEN %s='%s' THEN '%s'" % (
                    table_name, pk, i) for i, pk in enumerate(pk_list)
            ]
        )

        ordering = 'CASE %s END' % clauses
        queryset = qs.model.objects.filter(pk__in=pk_list).extra(
            select={'ordering': ordering}, order_by=('ordering',))

        return queryset
コード例 #2
0
    def filter(self, qs, value):
        super(SearchFilter, self).filter(qs, value)
        api = ElasticAPI()

        document_type = qs.model
        index_name = settings.SEARCH.get('INDEX_NAME')
        if self.search_type == 'full_text':
            result = api.search_document(index_name, document_type, value)
        else:
            result = api.search_auto_complete_document(index_name,
                                                       document_type, value)

        hits = []
        try:
            hits = result.json().get('hits').get('hits') if result.json() \
                else hits
        except AttributeError:
            hits = hits

        hits_ids_list = [str(hit.get('_id')) for hit in hits]

        pk_list = hits_ids_list

        if qs.model._meta.managed:
            table_name = "{0}_{1}.id".format(qs.model._meta.app_label,
                                             qs.model.__name__.lower())
        else:
            table_name = "{}.id".format(qs.model._meta.db_table)

        clauses = ' '.join([
            "WHEN %s='%s' THEN '%s'" % (table_name, pk, i)
            for i, pk in enumerate(pk_list)
        ])

        ordering = 'CASE %s END' % clauses
        queryset = qs.model.objects.filter(pk__in=pk_list).extra(
            select={'ordering': ordering}, order_by=('ordering', ))

        return queryset
コード例 #3
0
ファイル: test_search.py プロジェクト: FelixOngati/mfl_api
class TestElasticSearchAPI(TestCase):

    def setUp(self):
        self.elastic_search_api = ElasticAPI()
        super(TestElasticSearchAPI, self).setUp()

    def test_setup_index(self):
        self.elastic_search_api.delete_index(index_name='test_index')
        result = self.elastic_search_api.setup_index(index_name='test_index')
        self.assertEquals(200, result.status_code)
        self.elastic_search_api.delete_index(index_name='test_index')

    def test_get_non_existing_index(self):
        index_name = 'test_index'
        self.elastic_search_api.delete_index(index_name)
        self.elastic_search_api.get_index(index_name)

    def test_get_exsting_index(self):
        index_name = 'test_index'
        self.elastic_search_api.setup_index(index_name=index_name)
        self.elastic_search_api.get_index(index_name)
        self.elastic_search_api.delete_index(index_name='test_index')

    def test_delete_index(self):
        index_name = 'test_index_3'
        response = self.elastic_search_api.setup_index(index_name=index_name)
        self.assertEquals(200, response.status_code)
        self.elastic_search_api.delete_index(index_name)

    def test_index_document(self):
        facility = mommy.make(Facility, name='Fig tree medical clinic')
        self.elastic_search_api.setup_index(index_name='test_index')
        result = index_instance(
            'facilities', 'Facility', str(facility.id), 'test_index')
        self.assertTrue(result)

    def test_search_document_no_instance_type(self):
        index_name = 'test_index'
        response = self.elastic_search_api.setup_index(index_name=index_name)
        self.assertEquals(200, response.status_code)
        facility = mommy.make(Facility, name='Fig tree medical clinic')
        result = index_instance(
            'facilities', 'Facility', str(facility.id), 'test_index')
        self.assertTrue(result)
        self.elastic_search_api.search_document(
            index_name=index_name, instance_type=Facility, query='tree')

    def test_remove_document(self):
        index_name = 'test_index'
        self.elastic_search_api.setup_index(index_name=index_name)
        facility = mommy.make(Facility, name='Fig tree medical clinic')
        result = index_instance(
            'facilities', 'Facility', str(facility.id), 'test_index')
        self.assertTrue(result)
        self.elastic_search_api.remove_document(
            index_name, 'facility', str(facility.id))
        self.elastic_search_api.delete_index(index_name='test_index')

    def test_index_settings(self):
        expected_map = get_mappings()
        for key, value in expected_map.items():
            for key_2, value_2 in expected_map.get(key).items():
                for key_3, value_3 in expected_map.get(key).get(key_2).items():
                        values = expected_map.get(key).get(key_2).get(key_3)
                        self.assertEquals(
                            'autocomplete',
                            values.get('index_analyzer'))
                        self.assertEquals(
                            'autocomplete',
                            values.get('search_analyzer'))
                        self.assertEquals(
                            'string',
                            values.get('type'))
                        self.assertEquals(
                            False,
                            values.get('coerce'))
                        self.assertEquals(
                            True,
                            values.get('store'))

    def test_is_on_true(self):
        self.assertTrue(self.elastic_search_api._is_on)

    def test_is_on_false(self):
        with patch('search.search_utils.requests.get') as mock_get:
            mock_get.side_effect = ConnectionError
            elastic_api = ElasticAPI()
            self.assertFalse(elastic_api._is_on)

    def tearDown(self):
        self.elastic_search_api.delete_index(index_name='test_index')
        super(TestElasticSearchAPI, self).tearDown()
コード例 #4
0
class TestElasticSearchAPI(TestCase):
    def setUp(self):
        self.elastic_search_api = ElasticAPI()
        super(TestElasticSearchAPI, self).setUp()

    def test_setup_index(self):
        self.elastic_search_api.delete_index(index_name='test_index')
        result = self.elastic_search_api.setup_index(index_name='test_index')
        self.assertEquals(200, result.status_code)
        self.elastic_search_api.delete_index(index_name='test_index')

    def test_get_non_existing_index(self):
        index_name = 'test_index'
        self.elastic_search_api.delete_index(index_name)
        self.elastic_search_api.get_index(index_name)

    def test_get_exsting_index(self):
        index_name = 'test_index'
        self.elastic_search_api.setup_index(index_name=index_name)
        self.elastic_search_api.get_index(index_name)
        self.elastic_search_api.delete_index(index_name='test_index')

    def test_delete_index(self):
        index_name = 'test_index_3'
        response = self.elastic_search_api.setup_index(index_name=index_name)
        self.assertEquals(200, response.status_code)
        self.elastic_search_api.delete_index(index_name)

    def test_index_document(self):
        facility = mommy.make(Facility, name='Fig tree medical clinic')
        self.elastic_search_api.setup_index(index_name='test_index')
        result = index_instance(facility, 'test_index')
        self.assertEquals(201, result.status_code)

    def test_search_document_no_instance_type(self):
        index_name = 'test_index'
        response = self.elastic_search_api.setup_index(index_name=index_name)
        self.assertEquals(200, response.status_code)
        facility = mommy.make(Facility, name='Fig tree medical clinic')
        result = index_instance(facility, 'test_index')
        self.assertEquals(201, result.status_code)
        self.elastic_search_api.search_document(index_name=index_name,
                                                instance_type=Facility,
                                                query='tree')

    def test_remove_document(self):
        index_name = 'test_index'
        self.elastic_search_api.setup_index(index_name=index_name)
        facility = mommy.make(Facility, name='Fig tree medical clinic')
        result = index_instance(facility, 'test_index')
        self.assertEquals(201, result.status_code)
        self.elastic_search_api.remove_document(index_name, 'facility',
                                                str(facility.id))
        self.elastic_search_api.delete_index(index_name='test_index')

    def test_index_settings(self):
        expected_map = get_mappings()
        for key, value in expected_map.items():
            for key_2, value_2 in expected_map.get(key).items():
                for key_3, value_3 in expected_map.get(key).get(key_2).items():
                    values = expected_map.get(key).get(key_2).get(key_3)
                    self.assertEquals('autocomplete',
                                      values.get('index_analyzer'))
                    self.assertEquals('autocomplete',
                                      values.get('search_analyzer'))
                    self.assertEquals('string', values.get('type'))
                    self.assertEquals(False, values.get('coerce'))
                    self.assertEquals(True, values.get('store'))

    def tearDown(self):
        self.elastic_search_api.delete_index(index_name='test_index')
        super(TestElasticSearchAPI, self).tearDown()
コード例 #5
0
    def filter(self, qs, value):
        """Override this method in order to search in Elasticsearch index."""
        super(SearchFilter, self).filter(qs, value)
        api = ElasticAPI()
        if api._is_on:

            document_type = qs.model
            index_name = settings.SEARCH.get('INDEX_NAME')
            if self.search_type == 'full_text':
                result = api.search_document(index_name, document_type, value)
            else:
                result = api.search_auto_complete_document(
                    index_name, document_type, value)

            hits = []
            try:
                hits = result.json().get('hits').get('hits') if result.json() \
                    else hits
            except AttributeError:
                hits = hits

            hits_ids_list = [str(hit.get('_id')) for hit in hits]

            pk_list = hits_ids_list

            if qs.model._meta.managed:
                table_name = "{0}_{1}.id".format(
                    qs.model._meta.app_label, qs.model.__name__.lower())
            else:
                table_name = "{}.id".format(qs.model._meta.db_table)

            clauses = ' '.join(
                [
                    "WHEN %s='%s' THEN '%s'" % (
                        table_name, pk, i) for i, pk in enumerate(pk_list)
                ]
            )

            ordering = 'CASE %s END' % clauses
            queryset = qs.filter(pk__in=pk_list).extra(
                select={'ordering': ordering}, order_by=('ordering',))

            return queryset
        else:

            model = qs.model

            fields = [
                field.name for field in model._meta.get_fields()
                if field.concrete and field.get_internal_type() in FIELD_TYPES
            ]

            filter_params = {}

            for field in fields:
                field_type = model._meta.get_field(field).get_internal_type()
                if field_type == 'SequenceField' and value.isdigit():
                    filter_params[field + '__exact'] = value
                    break
                else:
                    filter_params[field + '__icontains'] = value

            q_filter = ""
            for key, value in filter_params.items():
                q_filter += "Q({0}='{1}') | ".format(key, value)

            # remove the pipe character at the end of the string
            q_filter_reverse = q_filter[::-1]
            q_filter = q_filter_reverse[3:len(q_filter_reverse) + 1]
            q_filter = q_filter[::-1]
            return qs.filter(eval(q_filter))
コード例 #6
0
ファイル: filters.py プロジェクト: FelixOngati/mfl_api
    def filter(self, qs, value):
        """Override this method in order to search in Elasticsearch index."""
        super(SearchFilter, self).filter(qs, value)
        api = ElasticAPI()
        if api._is_on:

            document_type = qs.model
            index_name = settings.SEARCH.get('INDEX_NAME')
            if self.search_type == 'full_text':
                result = api.search_document(index_name, document_type, value)
            else:
                result = api.search_auto_complete_document(
                    index_name, document_type, value)

            hits = []
            try:
                hits = result.json().get('hits').get('hits') if result.json() \
                    else hits
            except AttributeError:
                hits = hits

            hits_ids_list = [str(hit.get('_id')) for hit in hits]

            pk_list = hits_ids_list

            if qs.model._meta.managed:
                table_name = "{0}_{1}.id".format(
                    qs.model._meta.app_label, qs.model.__name__.lower())
            else:
                table_name = "{}.id".format(qs.model._meta.db_table)

            clauses = ' '.join(
                [
                    "WHEN %s='%s' THEN '%s'" % (
                        table_name, pk, i) for i, pk in enumerate(pk_list)
                ]
            )

            ordering = 'CASE %s END' % clauses
            queryset = qs.filter(pk__in=pk_list).extra(
                select={'ordering': ordering}, order_by=('ordering',))

            return queryset
        else:

            model = qs.model

            fields = [
                field.name for field in model._meta.get_fields()
                if field.concrete and field.get_internal_type() in FIELD_TYPES
            ]

            filter_params = {}

            for field in fields:
                field_type = model._meta.get_field(field).get_internal_type()
                if field_type == 'SequenceField' and value.isdigit():
                    filter_params[field + '__exact'] = value
                    break
                else:
                    filter_params[field + '__icontains'] = value

            q_filter = ""
            for key, value in filter_params.items():
                q_filter += "Q({0}='{1}') | ".format(key, value)

            # remove the pipe character at the end of the string
            q_filter_reverse = q_filter[::-1]
            q_filter = q_filter_reverse[3:len(q_filter_reverse) + 1]
            q_filter = q_filter[::-1]
            return qs.filter(eval(q_filter))