Esempio n. 1
0
def livesearch(request, template_name="lfs/search/livesearch_results.html"):
    """Renders the results for the live search.
    """
    # if not SOLR_ENABLED, urls.py does not call this view
    rows = 10
    q = request.GET.get("q", "")

    if q == "":
        result = simplejson.dumps({
            "state": "failure",
        })
    else:
        conn = _get_solr_connection()

        params = {
            'rows': rows,
        }

        results = conn.search(q.lower(), **params)

        # Products
        products = []
        for doc in results.docs:
            product = Product.objects.get(pk=doc["id"])
            products.append(product)

        products = render_to_string(
            template_name,
            RequestContext(request, {
                "products": products,
                "q": q,
                "total": results.hits,
            }))

        result = simplejson.dumps({
            "state": "success",
            "products": products,
        })

    return HttpResponse(result)
Esempio n. 2
0
def livesearch(request, template_name="lfs/search/livesearch_results.html"):
    """Renders the results for the live search.
    """
    # if not SOLR_ENABLED, urls.py does not call this view
    rows = 10
    q = request.GET.get("q", "")

    if q == "":
        result = simplejson.dumps({
            "state": "failure",
        })
    else:
        conn = _get_solr_connection()

        params = {
          'rows': rows,
        }

        results = conn.search(solr_escape(q.lower()), **params)

        # Products
        products = []
        for doc in results.docs:
            product = Product.objects.get(pk=doc["id"])
            products.append(product)

        products = render_to_string(template_name, RequestContext(request, {
            "products": products,
            "q": q,
            "total": results.hits,
        }))

        result = simplejson.dumps({
            "state": "success",
            "products": products,
        })

    return HttpResponse(result, mimetype="application/json")
Esempio n. 3
0
def search(request, template_name="lfs/search/search_results.html"):
    """Provides form and result for search via Solr.
    """
    # if not SOLR_ENABLED, urls.py does not call this view
    if request.GET.get("reset") or request.GET.get("livesearch"):
        try:
            del request.session["solr_filter"]
        except KeyError:
            pass

    rows = 10
    q = request.GET.get("q")

    try:
        page = int(request.GET.get("page", 1))
    except ValueError:
        page = 1

    if q:
        conn = _get_solr_connection()

        params = {
          'facet': 'on',
          'facet.field': ['categories', 'manufacturer'],
          'facet.mincount': 1,
          'rows': rows,
          "start": (page - 1) * rows,
        }

        # Sorting
        sorting = ''
        sorting_value = request.session.get("sorting")
        if sorting_value:
            # check validity of sort param.
            # Since SOLR uses different sorting keys, we must find
            # correct sorting key
            # Example: session contains -price, we need to sort by 'price desc'
            for item in SORTING_MAP:
                if item['default'] == sorting_value:
                    sorting = item.get('ftx')  # returns correct sort key
                    break
            if sorting:
                params["sort"] = sorting
            else:
                del request.session['sorting']

        if request.session.has_key("solr_filter"):
            params["fq"] = []
            for field, value in request.session["solr_filter"].items():
                params["fq"].append("%s:%s" % (field.encode("utf-8"), value.encode("utf-8")))

        results = conn.search(solr_escape(q.lower()), **params)

        # Products
        products = []
        for doc in results.docs:
            try:
                product = Product.objects.get(pk=doc["id"])
            except Product.DoesNotExist:
                pass
            else:
                products.append(product)

        fake_results = SolrResults(products, results.hits, rows)

        # Facets
        categories = []
        temp = results.facets["facet_fields"]["categories"]
        for i in range(1, len(temp), 2):
            name = temp[i - 1]
            if name.find(" "):
                url = '"%s"' % name
            else:
                url = name
            categories.append({
                "url": url,
                "name": name,
                "amount": temp[i],
            })

        manufacturers = []
        temp = results.facets["facet_fields"]["manufacturer"]
        for i in range(1, len(temp), 2):
            name = temp[i - 1]
            if name:
                if name.find(" "):
                    url = '"%s"' % name
                else:
                    url = name
                manufacturers.append({
                    "url": url,
                    "name": name,
                    "amount": temp[i],
                })

        paginator = Paginator(fake_results, rows)
        page_obj = Page(fake_results, page, paginator)

        return render_to_response(template_name, RequestContext(request, {
            "products": products,
            "results": results,
            "categories": categories,
            "manufacturers": manufacturers,
            "categories_reset": "categories" in request.session.get("solr_filter", []),
            "manufacturer_reset": "manufacturer" in request.session.get("solr_filter", []),
            "total": results.hits,
            "q": q,
            "sorting": sorting_value,
            'paginator': paginator,
            'page_obj': page_obj,
        }))
    else:
        return render_to_response(template_name, RequestContext(request, {}))
Esempio n. 4
0
def search(request, template_name="lfs/search/search_results.html"):
    """Provides form and result for search via Solr.
    """
    # if not SOLR_ENABLED, urls.py does not call this view
    if request.GET.get("reset") or request.GET.get("livesearch"):
        try:
            del request.session["solr_filter"]
        except KeyError:
            pass

    rows = 1000
    q = request.GET.get("q")

    try:
        page = int(request.GET.get("page", 1))
    except ValueError:
        page = 1

    if q:
        conn = _get_solr_connection()

        params = {
            'facet': 'on',
            'facet.field': ['categories', 'manufacturer'],
            'facet.mincount': 1,
            'rows': rows,
            "start": (page - 1) * rows,
        }

        # Sorting
        sorting = ''
        sorting_value = request.session.get("sorting")
        if sorting_value:
            # check validity of sort param.
            # Since SOLR uses different sorting keys, we must find
            # correct sorting key
            # Example: session contains -price, we need to sort by 'price desc'
            for item in SORTING_MAP:
                if item['default'] == sorting_value:
                    sorting = item.get('ftx')  # returns correct sort key
                    break
            if sorting:
                params["sort"] = sorting
            else:
                del request.session['sorting']

        if "solr_filter" in request.session.keys():
            params["fq"] = []
            for field, value in request.session["solr_filter"].items():
                params["fq"].append(
                    "%s:%s" % (field.encode("utf-8"), value.encode("utf-8")))

        results = conn.search(q.lower(), **params)

        # Products
        products = []
        for doc in results.docs:
            try:
                product = Product.objects.get(pk=doc["id"])
            except Product.DoesNotExist:
                pass
            else:
                products.append(product)

        fake_results = SolrResults(products, results.hits, rows)

        # Facets
        categories = []
        temp = results.facets["facet_fields"]["categories"]
        for i in range(1, len(temp), 2):
            name = temp[i - 1]
            if name.find(" "):
                url = '"%s"' % name
            else:
                url = name
            categories.append({
                "url": url,
                "name": name,
                "amount": temp[i],
            })

        manufacturers = []
        temp = results.facets["facet_fields"]["manufacturer"]
        for i in range(1, len(temp), 2):
            name = temp[i - 1]
            if name:
                if name.find(" "):
                    url = '"%s"' % name
                else:
                    url = name
                manufacturers.append({
                    "url": url,
                    "name": name,
                    "amount": temp[i],
                })

        paginator = Paginator(fake_results, rows)
        page_obj = Page(fake_results, page, paginator)

        return render_to_response(
            template_name,
            RequestContext(
                request, {
                    "products":
                    products,
                    "results":
                    results,
                    "categories":
                    categories,
                    "manufacturers":
                    manufacturers,
                    "categories_reset":
                    "categories" in request.session.get("solr_filter", []),
                    "manufacturer_reset":
                    "manufacturer" in request.session.get("solr_filter", []),
                    "total":
                    results.hits,
                    "q":
                    q,
                    "sorting":
                    sorting_value,
                    'paginator':
                    paginator,
                    'page_obj':
                    page_obj,
                }))
    else:
        return render_to_response(template_name, RequestContext(request, {}))