• Stars
    star
    357
  • Rank 115,190 (Top 3 %)
  • Language
    Python
  • License
    BSD 3-Clause "New...
  • Created almost 15 years ago
  • Updated over 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A transparent layer for full-text search using Sphinx and Django

This project is no longer maintained

This is a layer that functions much like the Django ORM does except it works on top of the Sphinx (http://www.sphinxsearch.com) full-text search engine.

Please Note: You will need to create your own sphinx indexes and install sphinx on your server to use this app.

There will no longer be release packages available. Please use SVN to checkout the latest trunk version, as it should always be stable and current.

Installation

To install the latest stable version:

sudo easy_install django-sphinx

To install the latest development version (updated quite often):

git clone git://github.com/dcramer/django-sphinx.git
cd django-sphinx
sudo python setup.py install

Note: You will need to install the sphinxapi.py package into your Python Path or use one of the included versions. To use the included version, you must specify the following in your settings.py file:

# Sphinx 0.9.9
SPHINX_API_VERSION = 0x116

# Sphinx 0.9.8
SPHINX_API_VERSION = 0x113

# Sphinx 0.9.7
SPHINX_API_VERSION = 0x107

Usage

The following is some example usage:

from djangosphinx.models import SphinxSearch

class MyModel(models.Model):
    search = SphinxSearch() # optional: defaults to db_table
    # If your index name does not match MyModel._meta.db_table
    # Note: You can only generate automatic configurations from the ./manage.py script
    # if your index name matches.
    search = SphinxSearch('index_name')

    # Or maybe we want to be more.. specific
    searchdelta = SphinxSearch(
        index='index_name delta_name',
        weights={
            'name': 100,
            'description': 10,
            'tags': 80,
        },
        mode='SPH_MATCH_ALL',
        rankmode='SPH_RANK_NONE',
    )

queryset = MyModel.search.query('query')
results1 = queryset.order_by('@weight', '@id', 'my_attribute')
results2 = queryset.filter(my_attribute=5)
results3 = queryset.filter(my_other_attribute=[5, 3,4])
results4 = queryset.exclude(my_attribute=5)[0:10]
results5 = queryset.count()

# as of 2.0 you can now access an attribute to get the weight and similar arguments
for result in results1:
    print result, result._sphinx
# you can also access a similar set of meta data on the queryset itself (once it's been sliced or executed in any way)
print results1._sphinx

Some additional methods: * count() * extra() (passed to the queryset) * all() (does nothing) * select_related() (passed to the queryset) * group_by(field, field, field) * set_options(index='', weights={}, weights=[], mode='SPH_MODE_*', rankmode='SPH_MATCH_*')

The django-sphinx layer also supports some basic querying over multiple indexes. To use this you first need to understand the rules of a UNION. Your indexes must contain exactly the same fields. These fields must also include a content_type selection which should be the content_type id associated with that table (model).

You can then do something like this:

from djangosphinx.models import SphinxSearch

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

This will return a list of all matches, ordered by weight, from all indexes. This performs one SQL query per index with matches in it, as Django's ORM does not support SQL UNION.

Config Generation

django-sphinx now includes a tool to create sample configuration for your models. It will generate both a source, and index configuration for a model class. You will still need to manually tweak the output, and insert it into your configuration, but it should aid in initial setup.

To use it:

from djangosphinx.utils import *

from myproject.myapp.models import MyModel

output = generate_config_for_model(MyModel)

print output

If you have multiple models which you wish to use the UNION searching:

model_classes = (ModelOne, ModelTwoWhichResemblesModelOne)

output = generate_config_for_models(model_classes)

You can also now output configuration from the command line:

./manage.py generate_sphinx_config <appname>

This will loop through all models in <appname> and attempt to find any with a SphinxSearch instance that is using the default index name (db_table).

Using the Config Generator

New in 2.2

django-sphinx now includes a simply python script to generate a config using your default template renderer. By default, we mean that if coffin is included in your INSTALLED_APPS, it uses it, otherwise it uses Django.

Two variables directly relate to the config generation:

# The base path for sphinx files. Sub directories will include data, log, and run. SPHINX_ROOT = '/var/sphinx-search/'

# Optional, defaults to 'conf/sphinx.html'. This should be configuration template. # See the included templates/sphinx.conf for an example. SPHINX_CONFIG_TEMPLATE = 'conf/sphinx.html'

Once done, your config can be passed via any sphinx command like so:

# Index your stuff DJANGO_SETTINGS_MODULE=myproject.settings indexer --config /path/to/djangosphinx/config.py --all --rotate

# Start the daemon DJANGO_SETTINGS_MODULE=myproject.settings searchd --config /path/to/djangosphinx/config.py

# Query the daemon DJANGO_SETTINGS_MODULE=myproject.settings search --config /path/to/djangosphinx/config.py my query

# Kill the daemon kill -9 $(cat /var/sphinx-search/run/searchd.pid)

For now, we recommend you setup some basic bash aliases or scripts to deal with this. This is just the first step in embedded config generation, so stay tuned!

  • Note: Make sure your PYTHON_PATH is setup properly!

Using Sphinx in Admin

Sphinx includes it's own ModelAdmin class to allow you to use it with Django's built-in admin app.

To use it, see the following example:

from djangosphinx.admin import SphinxModelAdmin

class MyAdmin(SphinxModelAdmin):
        index = 'my_index_name' # defaults to Model._meta.db_table
        weights = {'field': 100}

Limitations? You know it.

  • Only shows your max sphinx results (defaults to 1000)
  • Filters currently don't work.
  • This is a huge hack, so it may or may not continue working when Django updates.

Frequent Questions

How do I run multiple copies of Sphinx using django-sphinx?

The easiest way is to just run a different SPHINX_PORT setting in your settings.py. If you are using the above config generation, just modify the PORT, and start up the daemon

Resources

More Repositories

1

django-devserver

A drop-in replacement for Django's runserver.
Python
1,272
star
2

mangodb

A database that operates at CLOUD SCALE
Python
877
star
3

taskmaster

A simple distributed queue designed for handling one-off tasks with large sets of tasks
Python
442
star
4

django-ratings

Pluggable rating fields in Django.
Python
409
star
5

django-uuidfield

A UUIDField for Django
Python
264
star
6

mock-django

Python
225
star
7

logan

Logan is a toolkit for building standalone Django applications
Python
205
star
8

django-db-log

This project is no longer updated. Please see https://sentry.io/ for its successor
Python
122
star
9

wp-lifestream

Lifestreaming plugin for Wordpress
PHP
121
star
10

django-paging

Sexy pagination in Django
Python
108
star
11

django-orm-cache

A caching layer for Django
87
star
12

decruft

python-readability, but faster (mirror-ish)
Python
83
star
13

django-view-as

A Django middleware which allows you to view the site on behalf of a user.
Python
81
star
14

django-idmapper

An identify mapper for the Django ORM
Python
72
star
15

piplint

Piplint validates your current environment against requirements files
Python
62
star
16

python-tools-tmbundle

Adds support for automated PyFlakes linting in TextMate
Python
61
star
17

django-static-compiler

Python
56
star
18

pdbinject

A Python utility which uses GDB to inject a telnet-able PDB session into an existing process
Python
54
star
19

py-wikimarkup

A MediaWiki-to-HTML parser for Python.
Python
53
star
20

feedreader

An RSS/Atom feed parsing layer for lxml.objectify in Python
Python
52
star
21

django-sentry

This repo has been moved!
49
star
22

django-indexer

A simple key/value store for indexing meta data on JSON-type fields
Python
46
star
23

chardet

Forked version of chardet
Python
41
star
24

sentry-old

(In Development) Sentry 2.x is a realtime event logging and aggregation platform
Python
40
star
25

peated

HTML
40
star
26

django-data-tools

Python
38
star
27

django-compositepks

Composite Primary Keys fork
Python
38
star
28

django-oursql

Django database backend for MySQL via oursql.
Python
37
star
29

dotfiles

My personal dotfiles
Shell
36
star
30

quickunit

A Nose plugin which enables determining which tests to run based on the current git diff
Python
34
star
31

hive

My home infrastructure
Jinja
33
star
32

nibbits-shared

Some shared libraries which we have created at Nibbits
Python
28
star
33

numbers

Python Numbers for Humans
Python
26
star
34

sexytime

Python
26
star
35

nexus-memcache

Memcache statistics plugin for Nexus
Python
23
star
36

sentry

THIS REPOSITORY HAS BEEN MOVED
22
star
37

django-notices

A message notification system for Django
Python
22
star
38

django-db-routes

work in progress
Python
20
star
39

peek

Take a peek at whats slowing down your Python application
Python
20
star
40

ghostplusplus

Git Mirror of GHost++
C
20
star
41

kleenex

A Nose plugin designed to detect coverage and only run the tests that matter.
Python
18
star
42

git-owners

Python
16
star
43

nexus-redis

Redis stats in Nexus
Python
16
star
44

pastethat

A Django Pastebin (Pastethat.com)
Python
15
star
45

dateminer

Extract dates from webpages
Python
13
star
46

pmp

Python
11
star
47

selenium-saucelabs-python

Selenium driver for Sauce OnDemand
Python
11
star
48

pytest-django-lite

The bare minimum to integrate py.test with Django.
Python
11
star
49

SublimeFlakes

Inline PyFlakes in Sublime Text 2
Python
11
star
50

objtrack

Generic object 'viewed' status tracking in Django
Python
11
star
51

gitstats

Unofficial fork of GitStats with some bugfixes
Python
10
star
52

php-httplib

A port of Python's httplib in PHP
PHP
10
star
53

panelkit

WIP: A kit for building a tablet-focused Home Assistant UI.
TypeScript
9
star
54

europython-2011

9
star
55

django-bbcode

I'm tired of bitbucket
Python
9
star
56

gitboard

Python
8
star
57

nose-json

Python
8
star
58

wiki-to-overview

Redmine Plugin: Forward overview to Wiki
Ruby
8
star
59

anti-spam

8
star
60

pyconsg-tutorial-bootstrap

Python
7
star
61

lovelace-nextbus-card

A card giving richer public transit display using NextBus sensors.
TypeScript
7
star
62

tabletop-server

Python
6
star
63

muskrats

TypeScript
6
star
64

nexus-celery

6
star
65

php-database

A simple database library for MySQL and PGSQL.
PHP
6
star
66

djangospot

DjangoSpot.com Source
JavaScript
6
star
67

nose-bisect

Flush out bad tests with easy bisection in Python/Nose
Python
6
star
68

redmine-improved-revisions

Redmine Plugin: Improved revisions in Redmine
Ruby
5
star
69

nibbits-maploader

Nibbits automated map and replay installer
C#
5
star
70

forward-to-diffs

Redmine plugin: Forward revisions to diffs
Ruby
5
star
71

unifi-mqtt

Python
5
star
72

redmine_hudson

Ruby
5
star
73

soundbot

Audio player extension for Phenny
Python
4
star
74

minecraft-tools

Python
4
star
75

site

JavaScript
4
star
76

rss-to-tumblr

Allows importing an rss under a specific set of tags
Python
4
star
77

hass-luxor

FXLuminaire Luxor integration for Home Assistant
Python
4
star
78

reraise

Python
4
star
79

nexus-postgresql

4
star
80

jinja1-djangosupport

Jinja 1 with updated Django Support
Python
4
star
81

notsetuptools

Python
4
star
82

protobufs

Google Protocal Buffers
C++
4
star
83

redmine-home-to-projects

Forward a Redmine user to a the project listing when visiting the Home page.
Ruby
4
star
84

djangospot2

DjangoSpot using Pylons and Redis
Python
3
star
85

flask-redis

Redis support for Flask
Python
3
star
86

tabletop-mobile

JavaScript
3
star
87

pyconsg-tutorial-example

Python
3
star
88

raven

THIS PROJECT HAS BEEN MOVED
3
star
89

scmap

Python
3
star
90

homeline

very wip
TypeScript
2
star
91

gochatter

2
star
92

galaxyvalidator

galaxyvalidator.com source
Python
2
star
93

ghostplusplus-nibbits

Nibbit's version of GHost++
C
2
star
94

cask-server

Python
2
star
95

davidcramer-redirect

Redirects links on davidcramer.net to JustCramer.com
Python
2
star
96

redmine_disqus_ci

Disqus CI for Redmine
Ruby
2
star
97

ad-alarm-manager

Python
1
star
98

cask-web

TypeScript
1
star
99

gobot

Go
1
star
100

test-repo

So I can haz tests
1
star