Install the required packages
1 The first step:
full-Text search is different from the specific fields of fuzzy query, the use of Full-text search more efficient, and can be used for Chinese word processing.
Haystack: A full text search framework that supports whoosh, SOLR, Xapian, Elasticsearc four Full-text search engines
whoosh: Full-Text search engines written in pure python for small sites, Whoosh has been enough to use
Jieba: a free chinese word breaker
1) in a virtual environment to install the required package in turn.
pip install django-haystack
pip install whoosh
pip install Jieba
2 Registered app
Installed_apps = (
...
') Haystack ',
)
The Models
class Goodinfo (models) created. Model): Message
= models. Charfield (max_length=100)
content = models. TextField ()
def __str__ (self): return
Self.message
3 Configuring Search engines in Settings
haystack_connections = {
' default ': {
# using whoosh engine
' ENGINE ': ' Haystack.backend.whoosh_cn_backend. Whooshengine ',
# Index file path
' path ': Os.path.join (Base_dir, ' Whoosh_index '),
}
}
#当添加, modify, delete data, Auto-Generate index
haystack_signal_processor = ' haystack.signals.RealtimeSignalProcessor '
4 Add the search configuration to the project's urls.py.
URL (r ' ^search/', include (' Haystack.urls ')),
5 Create search_indexes.py in the created app directory write program from
haystack Import indexes from
. Models Import Goodinfo
# an Index
class Goodinfoindex (indexes) is indexed for certain data of a specified class. Searchindex, indexes. indexable):
text = indexes. Charfield (Document=true, use_template=true)
def Get_model (self): return
goodinfo
def index_queryset (Self, Using=none): Return
Self.get_model (). Objects.all ()
6 Create a "search/indexes/app06/" directory under the Templates directory. (App06 the name of the app that you created) to create a "goodinfo_text.txt" file in the folder. (Goodinfo the name of the database you created for yourself) #指定索引的属性 {{object.content}}} (content specifies this field as an indexed field in a field for the table you created)
7 Find the Virtual environment Django installed under the Haystack directory.
/home/python/.virtualenvs/django/lib/python2.7/site-packages/haystack/backends/creates the chineseanalyzer.py file in the above directory. Import Jieba from whoosh.analysis import Tokenizer, Token class Chinesetokenizer (tokenizer): Def __call__
(Self, value, Positions=false, Chars=false, Keeporiginal=false, Removestops=true, Start_pos=0, start_char=0, mode= ', **kwargs): t = Token (positions, chars, Removestops=removestops, Mode=mod
E, **kwargs) seglist = jieba.cut (value, cut_all=true) for W in Seglist: t.original = T.text = W T.boost = 1.0 If Positions:t.pos
= Start_pos + value.find (w) If Chars:t.startchar = Start_char + value.find (w)
T.endchar = Start_char + value.find (w) + len (w) yield T def chineseanalyzer (): Return Chinesetokenizer ()
8 Copy whoosh_backend.py file, change to the following name:
Note: The copied file name, there will be a space at the end, remember to delete this space.
whoosh_cn_backend.py
9) to open the new copy of the file, the introduction of Chinese analysis class, the internal use of Jieba participle. From
. Chineseanalyzer import Chineseanalyzer
10) Change the word analysis class.
find
Analyzer=stemminganalyzer ()
instead
of Analyzer=chineseanalyzer () to
initialize the index data.
python manage.py rebuild_index generates an index when prompted to enter Y a whoosh_index folder where Three index files are placed in the home directory
It 's time to start using the configuration.
By configuration, when you add data to admin management, you automatically create indexes for data, you can search directly, and you can create some test data first.
1 Define view query in app06/views.py.
def query (Request): Return render (Request, ' booktest/query.html ') 2) is configured in app06/urls.py.
The URL (r ' ^query/', Views.query), 3) creates the template query.html in the templates/app06/directory.
The parameter q represents the search content, and the data passed to the template is query.