Python django uses haystack: A Framework for full-text search,

Source: Internet
Author: User
Tags install django pip install django virtual environment

Python django uses haystack: A Framework for full-text search,


Haystack: Full-text retrieval framework
Whoosh: full-text search engine written in Python only
Jieba: a free Chinese Word Segmentation package

First install these three packages

Pip install django-haystack
Pip install whoosh
Pip install jieba

1. Modify the settings. py file and install the application haystack,
2. Configure the search engine in the settings. py file

HAYSTACK_CONNECTIONS = {'default': {# Use the whoosh ENGINE 'engine': 'haystack. backends. whoosh_cn_backend.WhooshEngine ', # index file PATH 'path': OS. path. join (BASE_DIR, 'whoosh _ Index'), }}# the indexes HAYSTACK_SIGNAL_PROCESSOR = 'haystack. signals. realtimeSignalProcessor'
View Code

 

HAYSTACK_CONNECTIONS = {
'Default ':{
# Using the whoosh Engine
'Engine ': 'haystack. backends. whoosh_cn_backend.WhooshEngine ',
# Index file path
'Path': OS. PATH. join (BASE_DIR, 'whoosh _ Index '),
}
}
# An index is automatically generated when data is added, modified, or deleted.
HAYSTACK_SIGNAL_PROCESSOR = 'haystack. signals. RealtimeSignalProcessor'

 

 

 

3. Create the "search/indexes/blog/" directory under the templates directory and create a file blog_text.txt
# Specifying index attributes

{Object. title }}
{Object. text }}
{Object. keywords }}

 

 

4. Create search_indexes under the application to be searched

From haystack import indexesfrom models import Post # specify the index class GoodsInfoIndex (indexes. searchIndex, indexes. indexable): text = indexes. charField (document = True, use_template = True) def get_model (self): return Post # search model class def index_queryset (self, using = None): return self. get_model (). objects. all ()
View Code

 

 

5.
1. Modify the haystack File
2. Find the haystack directory under the virtual environment py_django. The path varies depending on the python environment you are using.
3. site-packages/haystack/backends/create a file named ChineseAnalyzer. py and write the following code for Chinese Word Segmentation:

    import jiebafrom 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=mode,                  **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()
View Code

6.
1. Copy the whoosh_backend.py file and change it to the following name:
Whoosh_cn_backend.py
Import the Chinese word segmentation module to the copied file.
From. ChineseAnalyzer import ChineseAnalyzer
2. Change the word analysis class to Chinese.
Change analyzer = StemmingAnalyzer () to analyzer = ChineseAnalyzer ()

7. The last step is to create and initialize the index data.
Python manage. py rebuild_index

8. Create a search template in templates/indexes/create a search.html Template
The search results are paginated. The context passed by the view to the template is as follows:
Query: Search Keyword
Page: The page Object of the current page.
Paginator: paginator object

9. Import the module in your application view
From haystack. generic_views import SearchView
Define a class to override the get_context_data method, so that you can pass the custom context to the template.
Class GoodsSearchView (SearchView ):
Def get_context_data (self, * args, ** kwargs ):
Context = super (). get_context_data (* args, ** kwargs)
Context ['iscart'] = 1
Context ['qwjs'] = 2
Return context

Add this url to the urls file of the application to use the. as_view () class as a view method ()
Url ('^ search/$', views. BlogSearchView. as_view ())

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.