-1

I read this article http://eshlox.net/en/2012/09/13/sphinxsearch-and-django-ubuntu/

In view i get error for code: total = query_results['total']

error: 'NoneType' object has no attribute 'getitem'

def search(request):
    if request.GET:
        form = SearchForm(request.GET)
        query = request.GET.get('q', '')
        s = SphinxClient()
        s.SetServer('localhost', 9312)
        s.SetLimits(0, 16777215)
        if s.Status():
            query_results = s.Query(query)
            total = query_results['total']
            pages_id = [page['id'] for page in query_results['matches']]
            if pages_id:
                results = Page.objects.filter(id__in=pages_id)
            else:
                results = None
            if results:
                paginator = Paginator(results, 25)
                page = request.GET.get('page')
                try:
                    results = paginator.page(page)
                except PageNotAnInteger:
                    results = paginator.page(1)
                except EmptyPage:
                    results = paginator.page(paginator.num_pages)
            return render(request, 'wiki/search.html',
                          {'results': results,'total': total,
                           'query': query, 'form': form})
        else:
            logger = logging.getLogger('helper')
            logger.error('Sphinxsearch Error! %s' % s.GetLastError())
            messages.add_message(request, messages.ERROR, 'Search server is '
                                 'not responding. Administrator '
                                 'has been informed.')
            form = SearchForm()
            return render(request, 'wiki/search.html', {'form': form})
    else:
        form = SearchForm()
        return render(request, 'wiki/search.html', {'form': form})

Traceback Switch to copy-and-paste view

/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py in

get_response

                        response = wrapped_callback(request, *callback_args, **callback_kwargs)

    ...
▶ Local vars
/home/max/askmoiseev/ask/views.py in search

              total = query_results['total']

    ...
▶ Local vars

Please tell me what could be the error?

russianstudent
  • 129
  • 2
  • 12

2 Answers2

4

I imagine your query results are None

query_results = s.Query(query)

so when you try to access

total = query_results['total']

you get the __getitem__ error because None is not a List.

Here's an example from the interpreter.

>>> n = None
>>> n['b']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object has no attribute '__getitem__'
>>> 

Try running the query in the shell - django-admin.py shell then

s = SphinxClient()
s.SetServer('localhost', 9312)
s.SetLimits(0, 16777215)
query_results = s.Query(query)

Good luck, Sam

Nostradamnit
  • 862
  • 1
  • 10
  • 20
0

You should check GetLastError and maybe GetLastWarning before you try using query_results, to make sure the query succeeded.

The bonus you get to find out WHY the query failed. It would perhaps be better if the api raised proper exceptions on failures, but well, it doesn't.

As another point, your setLimits is absurdly high, its quite possible its failing from trying to obtain too many results.

Really you should do paging directly in sphinx, only get the ids for the current page, rather than get all ids, and then page.

barryhunter
  • 20,886
  • 3
  • 30
  • 43