Beispiel #1
0
    def search(self, query_string, model_or_queryset, fields=None, filters=None, prefetch_related=None):
        # Find model/queryset
        if isinstance(model_or_queryset, QuerySet):
            model = model_or_queryset.model
            queryset = model_or_queryset
        else:
            model = model_or_queryset
            queryset = model_or_queryset.objects.all()

        # Model must be a class that is in the index
        if not class_is_indexed(model):
            return []

        # Normalise query string
        if query_string is not None:
            query_string = normalise_query_string(query_string)

        # Check that theres still a query string after the clean up
        if query_string == "":
            return []

        # Apply filters to queryset
        if filters:
            queryset = queryset.filter(**filters)

        # Prefetch related
        if prefetch_related:
            for prefetch in prefetch_related:
                queryset = queryset.prefetch_related(prefetch)

        # Search
        return self._search(queryset, query_string, fields=fields)
Beispiel #2
0
    def search(self, query_string, model_or_queryset, fields=None, filters=None, prefetch_related=None):
        # Find model/queryset
        if isinstance(model_or_queryset, QuerySet):
            model = model_or_queryset.model
            queryset = model_or_queryset
        else:
            model = model_or_queryset
            queryset = model_or_queryset.objects.all()

        # Model must be a descendant of Indexed and be a django model
        if not issubclass(model, Indexed) or not issubclass(model, models.Model):
            return []

        # Normalise query string
        if query_string is not None:
            query_string = normalise_query_string(query_string)

        # Check that theres still a query string after the clean up
        if query_string == "":
            return []

        # Apply filters to queryset
        if filters:
            queryset = queryset.filter(**filters)

        # Prefetch related
        if prefetch_related:
            for prefetch in prefetch_related:
                queryset = queryset.prefetch_related(prefetch)

        # Search
        return self._search(queryset, query_string, fields=fields)
def chooser(request, get_results=False):
    # Get most popular queries
    queries = models.Query.get_most_popular()

    # If searching, filter results by query string
    query_string = None
    if 'q' in request.GET:
        searchform = SearchForm(request.GET)
        if searchform.is_valid():
            query_string = searchform.cleaned_data['q']
            queries = queries.filter(query_string__icontains=normalise_query_string(query_string))
    else:
        searchform = SearchForm()

    paginator, queries = paginate(request, queries, per_page=10)

    # Render
    if get_results:
        return render(request, "wagtailsearch/queries/chooser/results.html", {
            'queries': queries,
        })
    else:
        return render_modal_workflow(
            request, 'wagtailsearch/queries/chooser/chooser.html', 'wagtailsearch/queries/chooser/chooser.js', {
                'queries': queries,
                'searchform': searchform,
            }
        )
Beispiel #4
0
def chooser(request, get_results=False):
    # Get most popular queries
    queries = models.Query.get_most_popular()

    # If searching, filter results by query string
    query_string = None
    if "q" in request.GET:
        searchform = SearchForm(request.GET)
        if searchform.is_valid():
            query_string = searchform.cleaned_data["q"]
            queries = queries.filter(query_string__icontains=normalise_query_string(query_string))
    else:
        searchform = SearchForm()

    paginator, queries = paginate(request, queries, per_page=10)

    # Render
    if get_results:
        return render(request, "wagtailsearch/queries/chooser/results.html", {"queries": queries})
    else:
        return render_modal_workflow(
            request,
            "wagtailsearch/queries/chooser/chooser.html",
            "wagtailsearch/queries/chooser/chooser.js",
            {"queries": queries, "searchform": searchform},
        )
Beispiel #5
0
    def search(self,
               query_string,
               model,
               fields=None,
               filters=None,
               prefetch_related=None):
        # Get fields
        if fields is None:
            fields = [
                field.field_name
                for field in model.get_searchable_search_fields()
            ]

        # Start with all objects
        query = model.objects.all()

        # Apply filters
        if filters:
            query = query.filter(**filters)

        if query_string is not None:
            # Normalise query string
            query_string = normalise_query_string(query_string)

            # Get terms
            terms = query_string.split()
            if not terms:
                return model.objects.none()

            # Filter by terms
            for term in terms:
                term_query = models.Q()
                for field_name in fields:
                    # Check if the field exists (this will filter out indexed callables)
                    try:
                        model._meta.get_field_by_name(field_name)
                    except:
                        continue

                    # Filter on this field
                    term_query |= models.Q(
                        **{'%s__icontains' % field_name: term})

                query = query.filter(term_query)

            # Distinct
            query = query.distinct()

        # Prefetch related
        if prefetch_related:
            for prefetch in prefetch_related:
                query = query.prefetch_related(prefetch)

        return query
Beispiel #6
0
    def search(self, query_string, model, fields=None, filters={}, prefetch_related=[]):
        # Model must be a descendant of Indexed and be a django model
        if not issubclass(model, Indexed) or not issubclass(model, models.Model):
            return []

        # Normalise query string
        query_string = normalise_query_string(query_string)

        # Check that theres still a query string after the clean up
        if not query_string:
            return []

        # Return search results
        return ElasticSearchResults(self, ElasticSearchQuery(model, query_string, fields=fields, filters=filters), prefetch_related=prefetch_related)
Beispiel #7
0
    def search(self, query_string, model, fields=None, filters={}, prefetch_related=[]):
        # Normalise query string
        query_string = normalise_query_string(query_string)

        # Get terms
        terms = query_string.split()
        if not terms:
            return model.objects.none()

        # Get fields
        if fields is None:
            fields = model.indexed_get_indexed_fields().keys()

        # Start will all objects
        query = model.objects.all()

        # Apply filters
        if filters:
            query = query.filter(**filters)

        # Filter by terms
        for term in terms:
            term_query = None
            for field_name in fields:
                # Check if the field exists (this will filter out indexed callables)
                try:
                    model._meta.get_field_by_name(field_name)
                except:
                    continue

                # Filter on this field
                field_filter = {'%s__icontains' % field_name: term}
                if term_query is None:
                    term_query = models.Q(**field_filter)
                else:
                    term_query |= models.Q(**field_filter)
            query = query.filter(term_query)

        # Distinct
        query = query.distinct()

        # Prefetch related
        if prefetch_related:
            for prefetch in prefetch_related:
                query = query.prefetch_related(prefetch)

        return query
Beispiel #8
0
def chooser(request, get_results=False):
    # Get most popular queries
    queries = models.Query.get_most_popular()

    # If searching, filter results by query string
    query_string = None
    if 'q' in request.GET:
        searchform = SearchForm(request.GET)
        if searchform.is_valid():
            query_string = searchform.cleaned_data['q']
            queries = queries.filter(
                query_string__icontains=normalise_query_string(query_string))
    else:
        searchform = SearchForm()

    # Pagination
    p = request.GET.get('p', 1)

    paginator = Paginator(queries, 10)
    try:
        queries = paginator.page(p)
    except PageNotAnInteger:
        queries = paginator.page(1)
    except EmptyPage:
        queries = paginator.page(paginator.num_pages)

    # Render
    if get_results:
        return render(request, "wagtailsearch/queries/chooser/results.html", {
            'queries': queries,
            'query_string': query_string,
        })
    else:
        return render_modal_workflow(
            request, 'wagtailsearch/queries/chooser/chooser.html',
            'wagtailsearch/queries/chooser/chooser.js', {
                'queries': queries,
                'searchform': searchform,
                'query_string': query_string,
            })
Beispiel #9
0
    def search(self,
               query_string,
               model_or_queryset,
               fields=None,
               filters=None,
               prefetch_related=None):
        # Find model/queryset
        if isinstance(model_or_queryset, QuerySet):
            model = model_or_queryset.model
            queryset = model_or_queryset
        else:
            model = model_or_queryset
            queryset = model_or_queryset.objects.all()

        # Model must be a descendant of Indexed and be a django model
        if not issubclass(model, Indexed) or not issubclass(
                model, models.Model):
            return []

        # Normalise query string
        if query_string is not None:
            query_string = normalise_query_string(query_string)

        # Check that theres still a query string after the clean up
        if query_string == "":
            return []

        # Apply filters to queryset
        if filters:
            queryset = queryset.filter(**filters)

        # Prefetch related
        if prefetch_related:
            for prefetch in prefetch_related:
                queryset = queryset.prefetch_related(prefetch)

        # Return search results
        return ElasticSearchResults(
            self, ElasticSearchQuery(queryset, query_string, fields=fields))
Beispiel #10
0
def chooser(request, get_results=False):
    # Get most popular queries
    queries = models.Query.get_most_popular()

    # If searching, filter results by query string
    query_string = None
    if 'q' in request.GET:
        searchform = SearchForm(request.GET)
        if searchform.is_valid():
            query_string = searchform.cleaned_data['q']
            queries = queries.filter(query_string__icontains=normalise_query_string(query_string))
    else:
        searchform = SearchForm()

    # Pagination
    p = request.GET.get('p', 1)

    paginator = Paginator(queries, 10)
    try:
        queries = paginator.page(p)
    except PageNotAnInteger:
        queries = paginator.page(1)
    except EmptyPage:
        queries = paginator.page(paginator.num_pages)

    # Render
    if get_results:
        return render(request, "wagtailsearch/queries/chooser/results.html", {
            'queries': queries,
            'query_string': query_string,
        })
    else:
        return render_modal_workflow(request, 'wagtailsearch/queries/chooser/chooser.html', 'wagtailsearch/queries/chooser/chooser.js', {
            'queries': queries,
            'searchform': searchform,
            'query_string': query_string,
        })
Beispiel #11
0
 def get(cls, query_string):
     return cls.objects.get_or_create(query_string=normalise_query_string(query_string))[0]
Beispiel #12
0
    def save(self, *args, **kwargs):
        # Normalise query string
        self.query_string = normalise_query_string(self.query_string)

        super(Query, self).save(*args, **kwargs)
Beispiel #13
0
 def test_no_truncation(self):
     test_querystring = 'a' * 10
     result = normalise_query_string(test_querystring)
     self.assertEqual(len(result), 10)
Beispiel #14
0
 def get(cls, query_string):
     return cls.objects.get_or_create(query_string=normalise_query_string(query_string))[0]
Beispiel #15
0
    def save(self, *args, **kwargs):
        # Normalise query string
        self.query_string = normalise_query_string(self.query_string)

        super(Query, self).save(*args, **kwargs)
Beispiel #16
0
 def test_no_truncation(self):
     test_querystring = 'a' * 10
     result = normalise_query_string(test_querystring)
     self.assertEqual(len(result), 10)