文章目录

前言如何搭建文档搜索引擎服务器架构环境准备一、搭建Elasticsearch二、搭建Fscrawler三、搭建SearchUI服务四、定时拉取Git文件五、搭建Nginx文件下载服务器

前言

搭建一套文档搜索引擎。有时候,我们有一批文档,需要在这批文档中查找想要的内容,此时想要找到文档里面的内容就变的很麻烦。本文将介绍如何搭建一套文档搜索引擎。

如何搭建文档搜索引擎

服务器架构

环境准备

一台Cenos 7.x 的服务器 支持Docker NodeJs环境 支持nodejs和npm 安装命令

yum install -y nodejs

yum install -y npm

支持git命令

yum install -y git

如下服务均搭建在同一台服务器上

一、搭建Elasticsearch

下载IK分词器 IK分词器 Elasticsearch默认是没有分词功能的,分词是以插件的形式安装。 默认情况下,Es没有分词器,分词器是以插件包的形式存在。 下载地址 :https://github.com/medcl/elasticsearch-analysis-ik/releases/ 注意: 下载的版本需要与ES版本一致(建议) 解压后,修改文件夹名称为IK,并上传到服务器/home/elasticsearch/plugins 启动ES

docker run -d -v /home/elasticsearch/plugins:/usr/share/elasticsearch/plugins --name myelasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms64m -Xmx512m" elasticsearch:7.17.4

二、搭建Fscrawler

提前在/root/tmp目录准备两个文档用于测试

2.启动Fscrawler,在启动时,系统会询问Do you want to create it(Y/N)?回答Y

docker run -it --rm -v ~/.fscrawler:/root/.fscrawler -v ~/tmp:/tmp/es:ro dadoonet/fscrawler fscrawler mydoc

3配置参数

vim /root/.fscrawler/mydoc/_settings.yaml

(分别设置更新频率,扫描文件类型,es的地址)

update_rate: "60s"

includes:

- "*/*.pdf"

- "*/*.doc"

- "*/*.docx"

- "*/*.xls"

- "*/*.xlsx"

- "*/*.txt"

excludes:

- "*/~*"

elasticsearch:

nodes:

- url: "http://myelasticsearch:9200"

4启动服务(后台运行)

docker run -d -it --rm -v ~/.fscrawler:/root/.fscrawler -v ~/tmp:/tmp/es:ro --link myelasticsearch dadoonet/fscrawler fscrawler mydoc

三、搭建SearchUI服务

下载 SearchUI ,地址: https://github.com/elastic/search-ui 注意,本示例的版本是v1.8.0

修改源码内容search.js、buildRequest.js、buildState.js

2. 将searchui 代码复制到服务器/home目录下,且项目名称更改为了mysearchui 3. 进入该目录,执行启动命令 先设定该目录的执行权限

Chmod -R 777 mysearchui

进入到目录下面,启动命令

npm start > /dev/null 2>&1 &

4 访问,http://服务器ip:3000/,即可搜索

5 不要关闭服务器窗口,使用exit退出命令,这样可以保证服务继续运行,如果直接关闭服务器窗口,可能会导致搜索不可用。

四、定时拉取Git文件

前面的操作,已经可以进行搜索了,那么下面需要将git上面的文档定时拉去到上面约定的路径下,通过定时任务执行git pull命令,拉取文档,那么我们需要配置记住密码,既可以通过配置免密登录,也可以配置记住密码(这里为了简单,我们配置记住密码)。 1.配置记住密码的设置(后面我们只要输入一次账号密码,就不需要再次输入账号密码了),当然也可以配置ssh密钥免密的方式

git config --global credential.helper store

2 进入到/root/tmp目录,克隆项目下来

git clone https://xxxxx/document.git

3.进入项目路径下 执行git pull命令(目的是确定git pull命令没问题)

git pull

4. 编写定时的pull命令脚本

vim documentcron.sh

cd /root/tmp/document

git pull

#设定可执行权限

chmod 777 documentcron.sh

配置定时任务,让拉取命令定期执行

#编辑定时任务

crontab -e

#查看定时任务

crontabl -l

#添加定时任务(设定每3分钟执行一次git pull命令)

*/3 * * * * sh /home/documentcron.sh > /tmp/documentcron.log

6.检查定时任务是否将git的文件拉去到指定路径 直接在其他主机上向仓库push 了一个文档,稍等片刻,发现定时任务将文件拉取下来了。

五、搭建Nginx文件下载服务器

配置nginx.conf (不需要下载nginx,docker部署)

vim nginx.conf

user nginx;

worker_processes auto;

error_log /var/log/nginx/error.log;

pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {

worker_connections 1024;

}

http {

log_format main '$remote_addr - $remote_user [$time_local] "$request" '

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;

sendfile on;

tcp_nopush on;

tcp_nodelay on;

keepalive_timeout 65;

types_hash_max_size 2048;

default_type application/octet-stream;

# Load modular configuration files from the /etc/nginx/conf.d directory.

# See http://nginx.org/en/docs/ngx_core_module.html#include

# for more information.

include /etc/nginx/conf.d/*.conf;

server {

listen 80 default_server;

listen [::]:80 default_server;

server_name _;

# Load configuration files for the default server block.

include /etc/nginx/default.d/*.conf;

location /view {

# root /usr/share/nginx/html/download;

alias /usr/share/nginx/html/download;

autoindex on; #开启索引功能

autoindex_exact_size off; #关闭计算文件确切大小(单位bytes),只显示大概大小(单位kb、mb、gb)

autoindex_localtime on; #显示本机时间而非 GMT 时间

}

error_page 404 /404.html;

location = /40x.html {

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

}

}

}

启动nginx

docker run --name nginx -v /home/nginx/nginx.conf:/etc/nginx/nginx.conf -v /root/tmp/:/usr/share/nginx/html/download -p 3001:80 -d nginx

搜索下载文件,复制下载链接打开即可

参考阅读

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: