Detailed Explanation of django Using Global Search Function

  • 2021-07-22 10:12:49
  • OfStack

Install required packages

1 Step 1:

Full-text retrieval is different from fuzzy query of specific fields, and it is more efficient to use full-text retrieval, and can be used for Chinese word segmentation.

haystack: Framework for full-text retrieval, supporting whoosh, solr, Xapian, Elasticsearc

whoosh: Full-text search engine written by pure Python for small sites, whoosh is enough

jieba: A free Chinese word segmentation package

1) Install the required packages in turn in the virtual environment.


pip install django-haystack
pip install whoosh
pip install jieba

2 Register app


INSTALLED_APPS = (
  ...
  'haystack',
)

Created models


  class GoodInfo(models.Model):
    message = models.CharField(max_length=100)
    content = models.TextField()

    def __str__(self):
      return self.message

3 Configuring search engine in settings


#  Configuration of full-text search engine 
HAYSTACK_CONNECTIONS = {
  'default': {
    #  Use whoosh Engine 
    'ENGINE': 'haystack.backend.whoosh_cn_backend.WhooshEngine',
    #  Index file path 
    'PATH': os.path.join(BASE_DIR, 'whoosh_index'),
  }
}

# Automatically generate indexes when adding, modifying and deleting data 
HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'

4 Add the search configuration in urls. py of the project.


  url(r'^search/', include('haystack.urls')),

5 Create search_indexes. py program under the created app directory


from haystack import indexes
from .models import GoodInfo

#  Indexing certain data of a specified class 
class GoodInfoIndex(indexes.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 the "search/indexes/app06/" directory under the templates directory. (The name of the app that app06 created for itself)

Create the "goodinfo_text. txt" file in the folder. (The name of the database that goodinfo created for itself)


  # Specifies the properties of the index 
    {{object.content}} (content  For the table you created for yourself 1 Fields   Specify this field as an index field )

7 Find the haystack directory installed under the virtual environment django.


/home/python/.virtualenvs/django/lib/python2.7/site-packages/haystack/backends/

Create the ChineseAnalyzer. py file in the directory above.


  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=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()

8) Copy the whoosh_backend. py file and change it to the following name:

Note: There will be 1 space at the end of the copied file name. Remember to delete this space.

whoosh_cn_backend.py

9) Open copied out of the new file, the introduction of Chinese analysis class, internal use of jieba word segmentation.

from .ChineseAnalyzer import ChineseAnalyzer

10) Change the word analysis class.

Find


analyzer=StemmingAnalyzer()

Replace with


INSTALLED_APPS = (
  ...
  'haystack',
)
0

11) Initialize index data.

python manage.py rebuild_index

Enter y according to the prompt and generate index. 1 whoosh_index folder will be generated under the home directory, in which 3 index files will be placed

Once configured, it's time to start using it

According to the configuration, after adding data in admin management, it will automatically create indexes for the data, which can be searched directly, and some test data can be created first.

1) Define the view query in app06/views. py.


INSTALLED_APPS = (
  ...
  'haystack',
)
1

2) Configured in app06/urls. py.


INSTALLED_APPS = (
  ...
  'haystack',
)
2

3) Create the template query. html in the templates/app06/directory.

The parameter q represents the search content, and the data passed into the template is query.


<html>
<head>
  <title> Full-text retrieval </title>
</head>
<body>
<form method='get' action="/search/" target="_blank">(  The path of submission is   In urls  Configured in the )
  <input type="text" name="q"> # ( Note here  input Adj. name Attribute   Must be  q  Can't be changed )
  <br>
  <input type="submit" value=" Query ">
</form>
</body>
</html>

4) Customize the search results template: Create search. html in the templates/search/directory.

Search results are paged, and the context passed by the view into the template is as follows:

(This context is automatically returned by the search engine. We don't need to write the view function ourselves to return it and use it directly.)

query: Search keywords

page: The page object for the current page

paginator: Paging paginator Object

The view receives the following parameters:

The parameter q indicates the search content, and the data passed to the template is query

The parameter page represents the current page number


INSTALLED_APPS = (
  ...
  'haystack',
)
4

5) Run the server and enter the following address in the browser:

http://127.0.0.1:8000/query/


Related articles: