第一次接触elasticsearch(ES),极简单的入门。花了一天的时间从了解到完成,这里列举一些action list,方便自己的后续查阅
背景
希望有一个比较简单的内容平台,实现内容的快速录入以及快速检出,单条类目内容不大,类目的种类较多,对搜索有较高的要求。可以理解为code snip 或者 tip snip
部署ES
1. docker 部署ES
没有使用分布式的单节点
docker run -d --rm --name elasticsearch -p 9200:9200 -p 9300:9300 -e discovery.type=single-node -e http.cors.enabled=true -e http.cors.allow-origin=* -e http.cors.allow-headers=X-Requested-With,X-Auth-Token,Content-Type,Content-Length,Authorization -e http.cors.allow-credentials=true docker.elastic.co/elasticsearch/elasticsearch:7.9.2
2. 安装中文分词器
用于中文的快速搜索
docker exec -it elasticsearch /bin/bash ./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.9.2/elasticsearch-analysis-ik-7.9.2.zip
3. 重启docker容器
es 部署到此完成,ES占用内容很大,对于一个1G内存的机器,如果想跑Kibana要当心,很容易内存溢出
4. 新增Index
需要声明搜索解析,方便后续搜索
POST xxxx:9200/web/_mapping { "properties": { "title": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word" }, "desc": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word" }, "content": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_smart" } } }
借着就可以直接通过api调用增删改查
例如:
GET xxxx:9200/web/_search // 搜索 POST xxxx:9200/web/_create/1 创建记录
以上存储端完成
Egg 封装
借助egg-es
config/plugin.js
'use strict'; /** @type Egg.EggPlugin */ module.exports = { // had enabled by egg // static: { // enable: true, // } elasticsearch: { enable: true, package: 'egg-es', }, }, };
config/config.default.js
/* eslint valid-jsdoc: "off" */ 'use strict'; /** * @param {Egg.EggAppInfo} appInfo app info */ module.exports = appInfo => { /** * built-in config * @type {Egg.EggAppConfig} **/ const config = exports = {}; // use for cookie sign key, should change to your own and keep security config.keys = appInfo.name + '_1602827637255_7891'; // add your middleware config here config.middleware = []; // add your user config here const userConfig = { // myAppName: 'egg', elasticsearch: { host: '47.110.48.184:9200', apiVersion: '7.x', }, }; return { config, userConfig, }; };
service/es.js
'use strict'; const Service = require('egg').Service; class EsService extends Service { async search() { const { app, ctx } = this; const searchTitle = ctx.query.title; // console.log(searchTitle); // const pageNum = this.ctx.params.page; // const perPage = this.ctx.params.per_page; // const userQuery = this.ctx.request.body.search_query; // const userId = this.ctx.session.userId; const response = await app.elasticsearch.search({ index: 'web', body: { query: { match: { title: searchTitle } }, highlight: { pre_tags: [ '<em>', '<em>' ], post_tags: [ '</em>', '</em>' ], fields: { title: {}, }, }, }, }); return response; } } module.exports = EsService;
前端
几个比较重要的包
@toast-ui/editor @toast-ui/editor-plugin-code-syntax-highlight highlight.js codemirror