djangofull-text-searchdjango-sphinx

Querying multiple index in django-sphinx


The django-sphinx documentation shows that django-sphinx layer also supports some basic querying over multiple indexes.

http://github.com/dcramer/django-sphinx/blob/master/README.rst

from djangosphinx.models import SphinxSearch

SphinxSearch('index1 index2 index3').query('hello')

It seems SphinxSearch does not contain the function query(). I also tried to include content_type in sphinx.conf sql_query configuration as mentioned in the django-sphinx documentation. Nothing has worked.

Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'SphinxSearch' object has no attribute 'query'

Can anybody throw light on how I could get ranked results from multiple indexes in sphinx


Solution

  • instead of using SphinxSearch, you want to use SphinxQuerySet

    for example, if i wanted to query three indexes, weigh the results using the title, tags, and content fields, and set custom matching (SPH_MATCH_EXTENDED2) and ranking (SPH_RANK_NONE) modes:

    from djangosphinx.models import SphinxQuerySet
    
    search = SphinxQuerySet(
        index = "index_1 index_2 index_n",
        weights = {
            'title': 100,
            'tags': 80,
            'content': 20
        },
        mode = 'SPH_MATCH_EXTENDED2',
        rankmode = 'SPH_RANK_NONE')
    
    results = search.query('what is the answer to life, the universe, and everything?')
    
    for result in results:
        print result