diff --git a/blog/documents.py b/blog/documents.py index e5fa9e8..fa382d5 100644 --- a/blog/documents.py +++ b/blog/documents.py @@ -13,13 +13,12 @@ from elasticsearch_dsl.connections import connections import time from blog.models import Article, Category, Tag -from elasticsearch_dsl import Document, Date, Integer, Keyword, Text, Object, Boolean +from elasticsearch_dsl import Document, Date, Integer, Long, Keyword, Text, Object, Boolean from django.conf import settings ELASTICSEARCH_ENABLED = hasattr(settings, 'ELASTICSEARCH_DSL') - if ELASTICSEARCH_ENABLED: connections.create_connection( hosts=[settings.ELASTICSEARCH_DSL['default']['hosts']]) @@ -27,7 +26,7 @@ if ELASTICSEARCH_ENABLED: class ElapsedTimeDocument(Document): url = Text() - time_taken = Integer() + time_taken = Long() log_datetime = Date() type = Text(analyzer='ik_max_word') useragent = Text() @@ -101,8 +100,7 @@ class ArticleDocument(Document): class ArticleDocumentManager(): def __init__(self): - pass - # ArticleDocument.init() + self.create_index() def create_index(self): ArticleDocument.init() diff --git a/docs/es.md b/docs/es.md new file mode 100644 index 0000000..68ce49e --- /dev/null +++ b/docs/es.md @@ -0,0 +1,28 @@ +# 集成Elasticsearch +如果你已经有了`Elasticsearch`环境,那么可以将搜索从`Whoosh`换成`Elasticsearch`,集成方式也很简单, +首先需要注意如下几点: +1. 你的`Elasticsearch`支持`ik`中文分词 +2. 你的`Elasticsearch`版本>=7.3.0 + +接下来在`settings.py`做如下改动即可: +- 增加es链接,如下所示: +```python +ELASTICSEARCH_DSL = { + 'default': { + 'hosts': '127.0.0.1:9200' + }, +} +``` +- 修改`HAYSTACK`配置: +```python +HAYSTACK_CONNECTIONS = { + 'default': { + 'ENGINE': 'DjangoBlog.elasticsearch_backend.ElasticSearchEngine', + }, +} +``` +然后终端执行: +```shell script +./manage.py build_index +``` +这将会在你的es中创建两个索引,分别是`blog`和`performance`,其中`blog`索引就是搜索所使用的,而`performance`会记录每个请求的响应时间,以供将来优化使用。 \ No newline at end of file