Exemple #1
0
    def get(self, request, *args, **kwargs):
        if 'q' in request.GET:
            self.search_query = ''.join(request.GET['q'])

        results = []
        if self.index_manager.connected_to_es and self.search_query:
            self.authorized_forums = get_authorized_forums(self.request.user)

            search_queryset = Search()
            query = Match(_type='topic') \
                & Terms(forum_pk=self.authorized_forums) \
                & MultiMatch(query=self.search_query, fields=['title', 'subtitle', 'tags'])

            functions_score = [
                {'filter': Match(is_solved=True), 'weight': settings.ZDS_APP['search']['boosts']['topic']['if_solved']},
                {'filter': Match(is_sticky=True), 'weight': settings.ZDS_APP['search']['boosts']['topic']['if_sticky']},
                {'filter': Match(is_locked=True), 'weight': settings.ZDS_APP['search']['boosts']['topic']['if_locked']}
            ]

            scored_query = FunctionScore(query=query, boost_mode='multiply', functions=functions_score)
            search_queryset = search_queryset.query(scored_query)[:10]

            # Build the result
            for hit in search_queryset.execute():
                result = {'id': hit.pk, 'url': str(hit.get_absolute_url), 'title': str(hit.title)}
                results.append(result)

        data = {'results': results}
        return HttpResponse(json.dumps(data), content_type='application/json')
Exemple #2
0
    def get(self, request, *args, **kwargs):
        if "q" in request.GET:
            self.search_query = "".join(request.GET["q"])
        excluded_content_ids = request.GET.get("excluded", "").split(",")
        results = []
        if self.index_manager.connected_to_es and self.search_query:
            self.authorized_forums = get_authorized_forums(self.request.user)

            search_queryset = Search()
            if len(excluded_content_ids) > 0 and excluded_content_ids != [""]:
                search_queryset = search_queryset.exclude(
                    "terms", content_pk=excluded_content_ids)
            query = Match(_type="publishedcontent") & MultiMatch(
                query=self.search_query, fields=["title", "description"])

            functions_score = [
                {
                    "filter":
                    Match(content_type="TUTORIAL"),
                    "weight":
                    settings.ZDS_APP["search"]["boosts"]["publishedcontent"]
                    ["if_tutorial"],
                },
                {
                    "filter":
                    Match(content_type="ARTICLE"),
                    "weight":
                    settings.ZDS_APP["search"]["boosts"]["publishedcontent"]
                    ["if_article"],
                },
                {
                    "filter":
                    Match(content_type="OPINION"),
                    "weight":
                    settings.ZDS_APP["search"]["boosts"]["publishedcontent"]
                    ["if_opinion"],
                },
            ]

            scored_query = FunctionScore(query=query,
                                         boost_mode="multiply",
                                         functions=functions_score)
            search_queryset = search_queryset.query(scored_query)[:10]

            # Build the result
            for hit in search_queryset.execute():
                result = {
                    "id": hit.content_pk,
                    "pubdate": hit.publication_date,
                    "title": str(hit.title),
                    "description": str(hit.description),
                }
                results.append(result)

        data = {"results": results}

        return HttpResponse(json_handler.dumps(data),
                            content_type="application/json")
Exemple #3
0
    def get(self, request, *args, **kwargs):
        if "q" in request.GET:
            self.search_query = "".join(request.GET["q"])

        results = []
        if self.index_manager.connected_to_es and self.search_query:
            self.authorized_forums = get_authorized_forums(self.request.user)

            search_queryset = Search()
            query = (Match(_type="topic")
                     & Terms(forum_pk=self.authorized_forums)
                     & MultiMatch(query=self.search_query,
                                  fields=["title", "subtitle", "tags"]))

            functions_score = [
                {
                    "filter":
                    Match(is_solved=True),
                    "weight":
                    settings.ZDS_APP["search"]["boosts"]["topic"]["if_solved"]
                },
                {
                    "filter":
                    Match(is_sticky=True),
                    "weight":
                    settings.ZDS_APP["search"]["boosts"]["topic"]["if_sticky"]
                },
                {
                    "filter":
                    Match(is_locked=True),
                    "weight":
                    settings.ZDS_APP["search"]["boosts"]["topic"]["if_locked"]
                },
            ]

            scored_query = FunctionScore(query=query,
                                         boost_mode="multiply",
                                         functions=functions_score)
            search_queryset = search_queryset.query(scored_query)[:10]

            # Build the result
            for hit in search_queryset.execute():
                result = {
                    "id": hit.pk,
                    "url": str(hit.get_absolute_url),
                    "title": str(hit.title),
                    "subtitle": str(hit.subtitle),
                    "forumTitle": str(hit.forum_title),
                    "forumUrl": str(hit.forum_get_absolute_url),
                    "pubdate": str(hit.pubdate),
                }
                results.append(result)

        data = {"results": results}
        return HttpResponse(json_handler.dumps(data),
                            content_type="application/json")
Exemple #4
0
    def get(self, request, *args, **kwargs):
        if 'q' in request.GET:
            self.search_query = ''.join(request.GET['q'])
        excluded_content_ids = request.GET.get('excluded', '').split(',')
        results = []
        if self.index_manager.connected_to_es and self.search_query:
            self.authorized_forums = get_authorized_forums(self.request.user)

            search_queryset = Search()
            if len(excluded_content_ids) > 0 and excluded_content_ids != ['']:
                search_queryset = search_queryset.exclude(
                    'terms', content_pk=excluded_content_ids)
            query = Match(_type='publishedcontent') & MultiMatch(
                query=self.search_query, fields=['title', 'description'])

            functions_score = [{
                'filter':
                Match(content_type='TUTORIAL'),
                'weight':
                settings.ZDS_APP['search']['boosts']['publishedcontent']
                ['if_tutorial']
            }, {
                'filter':
                Match(content_type='ARTICLE'),
                'weight':
                settings.ZDS_APP['search']['boosts']['publishedcontent']
                ['if_article']
            }, {
                'filter':
                Match(content_type='OPINION'),
                'weight':
                settings.ZDS_APP['search']['boosts']['publishedcontent']
                ['if_opinion']
            }]

            scored_query = FunctionScore(query=query,
                                         boost_mode='multiply',
                                         functions=functions_score)
            search_queryset = search_queryset.query(scored_query)[:10]

            # Build the result
            for hit in search_queryset.execute():
                result = {
                    'id': hit.content_pk,
                    'pubdate': hit.publication_date,
                    'title': str(hit.title),
                    'description': str(hit.description)
                }
                results.append(result)

        data = {'results': results}

        return HttpResponse(json_handler.dumps(data),
                            content_type='application/json')
Exemple #5
0
    def get_queryset(self):
        if not self.index_manager.connected_to_es:
            messages.warning(self.request, _(u'Impossible de se connecter à Elasticsearch'))
            return []

        if self.search_query:

            # find forums the user is allowed to visit
            self.authorized_forums = get_authorized_forums(self.request.user)

            search_queryset = Search()

            # setting the different querysets (according to the selected models, if any)
            part_querysets = []
            chosen_groups = self.search_form.cleaned_data['models']

            if chosen_groups:
                models = []
                for group in chosen_groups:
                    if group in settings.ZDS_APP['search']['search_groups']:
                        models.append(settings.ZDS_APP['search']['search_groups'][group][1])
            else:
                models = [v[1] for k, v in settings.ZDS_APP['search']['search_groups'].iteritems()]

            models = reduce(operator.concat, models)

            for model in models:
                part_querysets.append(getattr(self, 'get_queryset_{}s'.format(model))())

            queryset = part_querysets[0]
            for query in part_querysets[1:]:
                queryset |= query

            # weighting:
            weight_functions = []
            for _type, weights in settings.ZDS_APP['search']['boosts'].items():
                if _type in models:
                    weight_functions.append({'filter': Match(_type=_type), 'weight': weights['global']})

            scored_queryset = FunctionScore(query=queryset, boost_mode='multiply', functions=weight_functions)
            search_queryset = search_queryset.query(scored_queryset)

            # highlighting:
            search_queryset = search_queryset.highlight_options(
                fragment_size=150, number_of_fragments=5, pre_tags=['[hl]'], post_tags=['[/hl]'])
            search_queryset = search_queryset.highlight('text').highlight('text_html')

            # executing:
            return self.index_manager.setup_search(search_queryset)

        return []
Exemple #6
0
    def get(self, request, *args, **kwargs):
        if 'q' in request.GET:
            self.search_query = ''.join(request.GET['q'])

        results = []
        if self.index_manager.connected_to_es and self.search_query:
            self.authorized_forums = get_authorized_forums(self.request.user)

            search_queryset = Search()
            query = Match(_type='topic') \
                & Terms(forum_pk=self.authorized_forums) \
                & MultiMatch(query=self.search_query, fields=['title', 'subtitle', 'tags'])

            functions_score = [
                {'filter': Match(is_solved=True), 'weight': settings.ZDS_APP['search']['boosts']['topic']['if_solved']},
                {'filter': Match(is_sticky=True), 'weight': settings.ZDS_APP['search']['boosts']['topic']['if_sticky']},
                {'filter': Match(is_locked=True), 'weight': settings.ZDS_APP['search']['boosts']['topic']['if_locked']}
            ]

            scored_query = FunctionScore(query=query, boost_mode='multiply', functions=functions_score)
            search_queryset = search_queryset.query(scored_query)[:10]

            # Build the result
            for hit in search_queryset.execute():
                result = {'id': hit.pk,
                          'url': str(hit.get_absolute_url),
                          'title': str(hit.title),
                          'subtitle': str(hit.subtitle),
                          'forumTitle': str(hit.forum_title),
                          'forumUrl': str(hit.forum_get_absolute_url),
                          'pubdate': str(hit.pubdate),
                          }
                results.append(result)

        data = {'results': results}
        return HttpResponse(json_handler.dumps(data), content_type='application/json')
Exemple #7
0
    def get_queryset(self):
        if not self.index_manager.connected_to_es:
            messages.warning(self.request,
                             _("Impossible de se connecter à Elasticsearch"))
            return []

        if self.search_query:

            # Searches forums the user is allowed to visit
            self.authorized_forums = get_authorized_forums(self.request.user)

            search_queryset = Search()

            # Restrict (sub)category if any
            if self.search_form.cleaned_data["category"]:
                self.content_category = self.search_form.cleaned_data[
                    "category"]
            if self.search_form.cleaned_data["subcategory"]:
                self.content_subcategory = self.search_form.cleaned_data[
                    "subcategory"]

            # Mark that contents must come from library if required
            self.from_library = False
            if self.search_form.cleaned_data["from_library"] == "on":
                self.from_library = True

            # Setting the different querysets (according to the selected models, if any)
            part_querysets = []
            chosen_groups = self.search_form.cleaned_data["models"]

            if chosen_groups:
                models = []
                for group in chosen_groups:
                    if group in settings.ZDS_APP["search"]["search_groups"]:
                        models.append(settings.ZDS_APP["search"]
                                      ["search_groups"][group][1])
            else:
                models = [
                    v[1] for k, v in settings.ZDS_APP["search"]
                    ["search_groups"].items()
                ]

            models = reduce(operator.concat, models)

            for model in models:
                part_querysets.append(
                    getattr(self, f"get_queryset_{model}s")())

            queryset = part_querysets[0]
            for query in part_querysets[1:]:
                queryset |= query

            # Weighting:
            weight_functions = []
            for _type, weights in list(
                    settings.ZDS_APP["search"]["boosts"].items()):
                if _type in models:
                    weight_functions.append({
                        "filter": Match(_type=_type),
                        "weight": weights["global"]
                    })

            scored_queryset = FunctionScore(query=queryset,
                                            boost_mode="multiply",
                                            functions=weight_functions)
            search_queryset = search_queryset.query(scored_queryset)

            # Highlighting:
            search_queryset = search_queryset.highlight_options(
                fragment_size=150,
                number_of_fragments=5,
                pre_tags=["[hl]"],
                post_tags=["[/hl]"])
            search_queryset = search_queryset.highlight("text").highlight(
                "text_html")

            # Executing:
            return self.index_manager.setup_search(search_queryset)

        return []
Exemple #8
0
    def get_queryset(self):
        if not self.index_manager.connected_to_es:
            messages.warning(self.request, _('Impossible de se connecter à Elasticsearch'))
            return []

        if self.search_query:

            # Searches forums the user is allowed to visit
            self.authorized_forums = get_authorized_forums(self.request.user)

            search_queryset = Search()

            # Restrict (sub)category if any
            if self.search_form.cleaned_data['category']:
                self.content_category = self.search_form.cleaned_data['category']
            if self.search_form.cleaned_data['subcategory']:
                self.content_subcategory = self.search_form.cleaned_data['subcategory']

            # Mark that contents must come from library if required
            self.from_library = False
            if self.search_form.cleaned_data['from_library'] == 'on':
                self.from_library = True

            # Setting the different querysets (according to the selected models, if any)
            part_querysets = []
            chosen_groups = self.search_form.cleaned_data['models']

            if chosen_groups:
                models = []
                for group in chosen_groups:
                    if group in settings.ZDS_APP['search']['search_groups']:
                        models.append(settings.ZDS_APP['search']['search_groups'][group][1])
            else:
                models = [v[1] for k, v in settings.ZDS_APP['search']['search_groups'].items()]

            models = reduce(operator.concat, models)

            for model in models:
                part_querysets.append(getattr(self, 'get_queryset_{}s'.format(model))())

            queryset = part_querysets[0]
            for query in part_querysets[1:]:
                queryset |= query

            # Weighting:
            weight_functions = []
            for _type, weights in list(settings.ZDS_APP['search']['boosts'].items()):
                if _type in models:
                    weight_functions.append({'filter': Match(_type=_type), 'weight': weights['global']})

            scored_queryset = FunctionScore(query=queryset, boost_mode='multiply', functions=weight_functions)
            search_queryset = search_queryset.query(scored_queryset)

            # Highlighting:
            search_queryset = search_queryset.highlight_options(
                fragment_size=150, number_of_fragments=5, pre_tags=['[hl]'], post_tags=['[/hl]'])
            search_queryset = search_queryset.highlight('text').highlight('text_html')

            # Executing:
            return self.index_manager.setup_search(search_queryset)

        return []