Ejemplo n.º 1
0
def search_project(request, query, language=None):

    body = {
        "query": {
            "bool": {
                "should": [
                    {"match": {"name": {"query": query, "boost": 10}}},
                    {"match": {"description": {"query": query}}},
                ]
            },
        },
        "facets": {
            "language": {
                "terms": {"field": "lang"},
            },
        },
        "highlight": {
            "fields": {
                "name": {},
                "description": {},
            }
        },
        "fields": ["name", "slug", "description", "lang", "url"],
        "size": 50  # TODO: Support pagination.
    }

    if language:
        body['facets']['language']['facet_filter'] = {"term": {"lang": language}}
        body['filter'] = {"term": {"lang": language}}

    before_project_search.send(request=request, sender=ProjectIndex, body=body)

    return ProjectIndex().search(body)
Ejemplo n.º 2
0
def search_project(request, query, language=None):
    """Search index for projects matching query."""
    body = {
        'query': {
            'bool': {
                'should': [
                    {
                        'match': {
                            'name': {
                                'query': query,
                                'boost': 10
                            }
                        }
                    },
                    {
                        'match': {
                            'description': {
                                'query': query
                            }
                        }
                    },
                ],
            },
        },
        'facets': {
            'language': {
                'terms': {
                    'field': 'lang'
                },
            },
        },
        'highlight': {
            'fields': {
                'name': {},
                'description': {},
            },
        },
        'fields': ['name', 'slug', 'description', 'lang', 'url'],
        'size': 50,  # TODO: Support pagination.
    }

    if language:
        body['facets']['language']['facet_filter'] = {
            'term': {
                'lang': language
            },
        }
        body['filter'] = {'term': {'lang': language}}

    before_project_search.send(request=request, sender=ProjectIndex, body=body)

    return ProjectIndex().search(body)