Full Text Search
- Fuzzy full-text search query than the specific field, the higher the efficiency of the use of full-text search, and word processing can be performed for the Chinese
- haystack: a package django, you can easily model for content inside the index, search, designed to support whoosh, solr, Xapian, Elasticsearc four kinds of full-text search engine backend, is a framework for full-text search
- whoosh: written in pure Python full-text search engine, although the performance is not as sphinx, xapian, Elasticsearc, etc., but no binary package, the program does not inexplicable collapse, for small sites, whoosh enough to use
- jieba: a free Chinese word package, if that does not work well can use some fee-based products
- Add Application
- Add Search Engine
- Stored in the haystack installation folder, such as the path
- C:\Users\言\AppData\Local\Programs\Python\Python37\Lib\site-packages\haystack\backends
- “/Library/Frameworks/Python.framework/Version/3.6/lib/python3.6/ /site-packages/haystack/backends/”
- Note: Copy the file name out, there will be a space at the end, remember to delete this space
- Initialization index data
operating
1. In order to install a virtual environment package
pip install django-haystack
pip install whoosh
pip install jieba
2. Modify the file settings.py
INSTALLED_APPS = (
...
'haystack',
)
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.whoosh_cn_backend.WhooshEngine',
'PATH': os.path.join(BASE_DIR, 'whoosh_index'),
}
}
#
自动生成索引
HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'
3. Add the url in urls.py project
urlpatterns = [
...
Re_path(r'^search/', include('haystack.urls')),
]
4. Establish search_indexes.py file in the application directory
# coding=utf-8
from haystack import indexes
from shopadmin import Good
class GoodIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
def get_model(self):
return GoodsInfo
def index_queryset(self, using=None):
return self.get_model().objects.all()
5. Create a "model class name _text.txt" file in the directory "templates / search / indexes / shopadmin /"
#good_text.txt
,这里列出了要对哪些列的内容进行检索
{{ object.name }}
{{ object.description }}
6. Establish search.html in the directory "templates / search /"
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
{% if query %}
<h3>
搜索结果如下:
</h3>
{% for result in page.object_list %}
<a href="/{{ result.object.id }}/">{{ result.object.name }}</a><br/>
{% empty %}
<p>
啥也没找到
</p>
{% endfor %}
{% if page.has_previous or page.has_next %}
<div>
{% if page.has_previous %}<a href="?q={{ query }}&page={{ page.previous_page_number }}">{% endif %}«
上一页
{% if page.has_previous %}</a>{% endif %}
|
{% if page.has_next %}<a href="?q={{ query }}&page={{ page.next_page_number }}">{% endif %}
下一页
»{% if page.has_next %}</a>{% endif %}
</div>
{% endif %}
{% endif %}
</body>
</html>
7. Establish ChineseAnalyzer.py file (python to find the root path)
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 whoosh_backend.py file, renamed whoosh_cn_backend.py
from .ChineseAnalyzer import ChineseAnalyzer
查找
analyzer=StemmingAnalyzer()
改为
analyzer=ChineseAnalyzer()
9. Generate Index
python manage.py rebuild_index
10. Create a search bar in the template
<form method='get' action="/search/" target="_blank">
<input type="text" name="q">
<input type="submit" value="
查询
">
</form>