Beispiel #1
0
    def post(self, request, format=None):
        """Performs search."""
        data = request.data.copy()

        # to support legacy paging parameters that can be in query_string
        for legacy_query_param in ('limit', 'offset'):
            if legacy_query_param in request.query_params \
                    and legacy_query_param not in request.data:
                data[legacy_query_param] = request.query_params[
                    legacy_query_param]

        validated_data = self.validate_data(data)
        query = self.get_base_query(request, validated_data)

        limited_query = limit_search_query(
            query,
            offset=validated_data['offset'],
            limit=validated_data['limit'],
        )

        results = execute_search_query(limited_query)

        response = {
            'count': results.hits.total,
            'results': [x.to_dict() for x in results.hits],
        }

        response = self.enhance_response(results, response)

        return Response(data=response)
Beispiel #2
0
def _get_objects(request, limit, search_app, adviser_field):
    if not has_permissions_for_app(request, search_app):
        return []

    query = get_search_by_entity_query(
        term='',
        entity=search_app.es_model,
        filter_data={
            adviser_field: request.user.id,
            'created_on_exists': True,
        },
        ordering='created_on:desc',
    )
    results = limit_search_query(
        query,
        offset=0,
        limit=limit,
    ).execute()

    return [result.to_dict() for result in results.hits]
Beispiel #3
0
    def post(self, request, format=None):
        """Performs search."""
        data = request.data.copy()

        # to support legacy paging parameters that can be in query_string
        for legacy_query_param in ('limit', 'offset'):
            if legacy_query_param in request.query_params \
                    and legacy_query_param not in request.data:
                data[legacy_query_param] = request.query_params[
                    legacy_query_param]

        validated_data = self.validate_data(data)
        query = self.get_base_query(request, validated_data)

        limited_query = limit_search_query(
            query,
            offset=validated_data['offset'],
            limit=validated_data['limit'],
        )

        results = _execute_search_query(limited_query)

        response = {
            'count': results.hits.total,
            'results': [x.to_dict() for x in results.hits],
        }

        if self.include_aggregations:
            aggregations = {}
            for field in self.FILTER_FIELDS:
                es_field = self.REMAP_FIELDS.get(field, field)
                if es_field in results.aggregations:
                    aggregation = results.aggregations[es_field]
                    aggregations[field] = [
                        bucket.to_dict() for bucket in aggregation['buckets']
                    ]

            response['aggregations'] = aggregations

        return Response(data=response)
def test_get_limited_search_by_entity_query():
    """Tests search by entity."""
    date = '2017-06-13T09:44:31.062870'
    filter_data = {
        'name': 'Woodside',
        'address_country.id': ['80756b9a-5d95-e211-a939-e4115bead28a'],
        'archived_before': date,
        'archived_after': date,
    }
    query = get_search_by_entity_query(
        ESContact,
        term='test',
        filter_data=filter_data,
    )
    query = limit_search_query(
        query,
        offset=5,
        limit=5,
    )

    assert query.to_dict() == {
        'query': {
            'bool': {
                'must': [
                    {
                        'term': {
                            '_type': 'contact',
                        },
                    },
                    {
                        'bool': {
                            'should': [
                                {
                                    'match': {
                                        'name.keyword': {
                                            'query': 'test',
                                            'boost': 2,
                                        },
                                    },
                                },
                                {
                                    'multi_match': {
                                        'query':
                                        'test',
                                        'fields': (
                                            'id',
                                            'name',
                                            'name.trigram',
                                            'email',
                                            'email_alternative',
                                            'company.name',
                                            'company.name.trigram',
                                        ),
                                        'type':
                                        'cross_fields',
                                        'operator':
                                        'and',
                                    },
                                },
                            ],
                        },
                    },
                ],
                'filter': [
                    {
                        'bool': {
                            'must': [
                                {
                                    'match': {
                                        'name': {
                                            'query': 'Woodside',
                                            'operator': 'and',
                                        },
                                    },
                                },
                                {
                                    'bool': {
                                        'should': [
                                            {
                                                'match': {
                                                    'address_country.id': {
                                                        'query':
                                                        '80756b9a-5d95-e211-a939-e4115bead28a',
                                                        'operator': 'and',
                                                    },
                                                },
                                            },
                                        ],
                                        'minimum_should_match':
                                        1,
                                    },
                                },
                                {
                                    'range': {
                                        'archived': {
                                            'gte':
                                            '2017-06-13T09:44:31.062870',
                                            'lte':
                                            '2017-06-13T09:44:31.062870',
                                        },
                                    },
                                },
                            ],
                        },
                    },
                ],
            },
        },
        'sort': [
            '_score',
            'id',
        ],
        'from': 5,
        'size': 5,
    }
Beispiel #5
0
def test_limited_get_search_by_entity_query():
    """Tests search by entity."""
    date = '2017-06-13T09:44:31.062870'
    filter_data = {
        'address_town': ['Woodside'],
        'trading_address_country.id': ['80756b9a-5d95-e211-a939-e4115bead28a'],
        'estimated_land_date_before': date,
        'estimated_land_date_after': date,
    }
    query = get_search_by_entity_query(
        term='test',
        filter_data=filter_data,
        entity=ESCompany,
    )
    query = limit_search_query(
        query,
        offset=5,
        limit=5,
    )

    assert query.to_dict() == {
        'query': {
            'bool': {
                'must': [
                    {
                        'term': {
                            '_type': 'company',
                        },
                    },
                    {
                        'bool': {
                            'should': [
                                {
                                    'match_phrase': {
                                        'name_keyword': {
                                            'query': 'test',
                                            'boost': 2,
                                        },
                                    },
                                },
                                {
                                    'match_phrase': {
                                        'id': 'test',
                                    },
                                },
                                {
                                    'multi_match': {
                                        'query': 'test',
                                        'fields': (
                                            'name',
                                            'name_trigram',
                                            'company_number',
                                            'trading_name',
                                            'trading_name_trigram',
                                            'reference_code',
                                            'registered_address_country.name_trigram',
                                            'registered_address_postcode_trigram',
                                            'trading_address_country.name_trigram',
                                            'trading_address_postcode_trigram',
                                            'uk_region.name_trigram',
                                        ),
                                        'type': 'cross_fields',
                                        'operator': 'and',
                                    },
                                },
                            ],
                        },
                    },
                ],
            },
        },
        'post_filter': {
            'bool': {
                'must': [
                    {
                        'bool': {
                            'should': [
                                {
                                    'match': {
                                        'address_town': {
                                            'query': 'Woodside',
                                            'operator': 'and',
                                        },
                                    },
                                },
                            ],
                            'minimum_should_match': 1,
                        },
                    },
                    {
                        'bool': {
                            'should': [
                                {
                                    'match_phrase': {
                                        'trading_address_country.id':
                                            '80756b9a-5d95-e211-a939-e4115bead28a',
                                    },
                                },
                            ],
                            'minimum_should_match': 1,
                        },
                    },
                    {
                        'range': {
                            'estimated_land_date': {
                                'gte': '2017-06-13T09:44:31.062870',
                                'lte': '2017-06-13T09:44:31.062870',
                            },
                        },
                    },
                ],
            },
        },
        'from': 5,
        'size': 5,
        'sort': [
            '_score',
            'id',
        ],
    }
def test_limited_get_search_by_entity_query():
    """Tests search by entity."""
    date = '2017-06-13T09:44:31.062870'
    filter_data = {
        'investor_company_country.id':
        ['80756b9a-5d95-e211-a939-e4115bead28a'],
        'estimated_land_date_after': date,
        'estimated_land_date_before': date,
    }
    query = get_search_by_entities_query(
        [ESInvestmentProject],
        term='test',
        filter_data=filter_data,
    )
    query = limit_search_query(
        query,
        offset=5,
        limit=5,
    )

    assert query.to_dict() == {
        'query': {
            'bool': {
                'must': [
                    {
                        'bool': {
                            'should': [
                                {
                                    'match': {
                                        'name.keyword': {
                                            'query': 'test',
                                            'boost': 2,
                                        },
                                    },
                                },
                                {
                                    'multi_match': {
                                        'query':
                                        'test',
                                        'fields': (
                                            'id',
                                            'name',
                                            'name.trigram',
                                            'uk_company.name',
                                            'uk_company.name.trigram',
                                            'investor_company.name',
                                            'investor_company.name.trigram',
                                            'project_code.trigram',
                                        ),
                                        'type':
                                        'cross_fields',
                                        'operator':
                                        'and',
                                    },
                                },
                            ],
                        },
                    },
                ],
                'filter': [
                    {
                        'bool': {
                            'must': [
                                {
                                    'bool': {
                                        'should': [
                                            {
                                                'match': {
                                                    'investor_company_country.id':
                                                    {
                                                        'query':
                                                        '80756b9a-5d95-e211-a939-e4115bead28a',
                                                        'operator': 'and',
                                                    },
                                                },
                                            },
                                        ],
                                        'minimum_should_match':
                                        1,
                                    },
                                },
                                {
                                    'range': {
                                        'estimated_land_date': {
                                            'gte':
                                            '2017-06-13T09:44:31.062870',
                                            'lte':
                                            '2017-06-13T09:44:31.062870',
                                        },
                                    },
                                },
                            ],
                        },
                    },
                ],
            },
        },
        'sort': [
            '_score',
            'id',
        ],
        'from': 5,
        'size': 5,
    }
Beispiel #7
0
def test_limited_get_search_by_entity_query():
    """Tests search by entity."""
    expected_query = {
        'query': {
            'bool': {
                'must': [
                    {
                        'bool': {
                            'should': [
                                {
                                    'match': {
                                        'name.keyword': {
                                            'query': 'test',
                                            'boost': 2,
                                        },
                                    },
                                },
                                {
                                    'multi_match': {
                                        'query':
                                        'test',
                                        'fields': (
                                            'id',
                                            'name',
                                            'name.trigram',
                                            'company_number',
                                            'trading_names',
                                            'trading_names.trigram',
                                            'reference_code',
                                            'sector.name',
                                            'address.line_1.trigram',
                                            'address.line_2.trigram',
                                            'address.town.trigram',
                                            'address.county.trigram',
                                            'address.area.name.trigram',
                                            'address.postcode',
                                            'address.country.name.trigram',
                                            'registered_address.line_1.trigram',
                                            'registered_address.line_2.trigram',
                                            'registered_address.town.trigram',
                                            'registered_address.county.trigram',
                                            'registered_address.area.name.trigram',
                                            'registered_address.postcode',
                                            'registered_address.country.name.trigram',
                                        ),
                                        'type':
                                        'cross_fields',
                                        'operator':
                                        'and',
                                    },
                                },
                            ],
                        },
                    },
                ],
                'filter': [
                    {
                        'bool': {
                            'must': [
                                {
                                    'match': {
                                        'name': {
                                            'query': 'Woodside',
                                            'operator': 'and',
                                        },
                                    },
                                },
                                {
                                    'bool': {
                                        'should': [
                                            {
                                                'match': {
                                                    'address.country.id': {
                                                        'query':
                                                        '80756b9a-5d95-e211-a939-e4115bead28a',
                                                        'operator': 'and',
                                                    },
                                                },
                                            },
                                        ],
                                        'minimum_should_match':
                                        1,
                                    },
                                },
                                {
                                    'range': {
                                        'archived': {
                                            'lte':
                                            '2017-06-13T09:44:31.062870',
                                            'gte':
                                            '2017-06-13T09:44:31.062870',
                                        },
                                    },
                                },
                            ],
                        },
                    },
                ],
            },
        },
        'sort': [
            '_score',
            'id',
        ],
        'track_total_hits': True,
        'from': 5,
        'size': 5,
    }

    date = '2017-06-13T09:44:31.062870'
    filter_data = {
        'name': 'Woodside',
        'address.country.id': ['80756b9a-5d95-e211-a939-e4115bead28a'],
        'archived_before': date,
        'archived_after': date,
    }
    query = get_search_by_entities_query(
        [SearchCompany],
        term='test',
        filter_data=filter_data,
    )
    query = limit_search_query(
        query,
        offset=5,
        limit=5,
    )

    assert query.to_dict() == expected_query