167 lines
4.0 KiB
Markdown
167 lines
4.0 KiB
Markdown
## 目录结构
|
||
|
||
- pgsql-gis/
|
||
- pgsql-admin/
|
||
- docker-compose.yaml
|
||
|
||
```shell
|
||
mkdir pgsql-gis pgsql-admin
|
||
sudo chown 5050 ./pgsql-admin
|
||
```
|
||
|
||
## docker-compose.yaml
|
||
|
||
```yaml
|
||
version: '3.8'
|
||
services:
|
||
# PGSQL
|
||
pgsql-gis-16:
|
||
# 不支持全文检索
|
||
image: postgis/postgis:16-master
|
||
# 支持全文检索的自编译镜像
|
||
#image: pgsql-gis-fts:latest
|
||
container_name: pgsql-gis-16
|
||
ports:
|
||
- "35430:5432" # 替换
|
||
volumes:
|
||
- ./pgsql-gis:/var/lib/postgresql/data
|
||
environment:
|
||
POSTGRES_USER: # 替换
|
||
POSTGRES_PASSWORD: # 替换
|
||
POSTGRES_DB: # 替换
|
||
POSTGRES_INITDB_ARGS: --encoding=UTF8
|
||
restart: on-failure:3
|
||
# PG Admin(HTTP)
|
||
pgsql-admin:
|
||
image: dpage/pgadmin4:latest # 替换
|
||
container_name: pgsql-admin
|
||
ports:
|
||
- "18906:80" # 替换
|
||
volumes:
|
||
- ./pgsql-admin:/var/lib/pgadmin
|
||
environment:
|
||
PGADMIN_DEFAULT_EMAIL: demo@email.com
|
||
PGADMIN_DEFAULT_PASSWORD: # 替换
|
||
PGADMIN_CONFIG_CHECK_EMAIL_DELIVERABILITY: 'False'
|
||
restart: on-failure:3
|
||
# PG Admin(HTTPS)
|
||
# pgsql-admin:
|
||
# image: dpage/pgadmin4:latest
|
||
# container_name: pgsql-admin
|
||
# ports:
|
||
# - "18907:443"
|
||
# volumes:
|
||
# - ./pgsql-admin:/var/lib/pgadmin
|
||
# - ./certs:/certs
|
||
# environment:
|
||
# PGADMIN_DEFAULT_EMAIL: demo@email.com
|
||
# PGADMIN_DEFAULT_PASSWORD: # 替换
|
||
# PGADMIN_CONFIG_CHECK_EMAIL_DELIVERABILITY: 'False'
|
||
# PGADMIN_ENABLE_TLS: 'True'
|
||
# PGADMIN_SERVER_CERT_FILE: /certs/server.cert
|
||
# PGADMIN_SERVER_KEY_FILE: /certs/server.key
|
||
# restart: on-failure:3
|
||
```
|
||
|
||
## 添加全文索引支持
|
||
|
||
需要从 git 克隆 zhparser 添加中文分词支持,执行 docker build 命令的目录结构长这样:
|
||
|
||
- Dockerfile
|
||
- zhparser/
|
||
|
||
```bash
|
||
git clone https://github.com/amutu/zhparser.git
|
||
```
|
||
|
||
## Dockerfile
|
||
|
||
```bash
|
||
FROM postgis/postgis:16-master
|
||
|
||
ENV DEBIAN_FRONTEND=noninteractive
|
||
|
||
RUN apt-get update && apt-get install -y \
|
||
build-essential \
|
||
git \
|
||
libcurl4-openssl-dev \
|
||
libxml2-dev \
|
||
wget \
|
||
postgresql-server-dev-16 \
|
||
&& rm -rf /var/lib/apt/lists/*
|
||
|
||
RUN pg_config --version
|
||
|
||
WORKDIR /tmp
|
||
|
||
RUN wget http://www.xunsearch.com/scws/down/scws-1.2.3.tar.bz2 \
|
||
&& tar xjf scws-1.2.3.tar.bz2 \
|
||
&& cd scws-1.2.3 \
|
||
&& ./configure \
|
||
&& make && make install \
|
||
&& cd ..
|
||
|
||
COPY zhparser zhparser
|
||
|
||
RUN cd zhparser \
|
||
&& export PG_CONFIG=$(which pg_config) \
|
||
&& echo "Using pg_config: $PG_CONFIG" \
|
||
&& make USE_PGXS=1 \
|
||
&& make USE_PGXS=1 install \
|
||
&& cd ..
|
||
|
||
RUN rm -rf /tmp/*
|
||
|
||
WORKDIR /
|
||
```
|
||
|
||
## 编译
|
||
|
||
```bash
|
||
docker build -t pgsql-gis-fts .
|
||
```
|
||
|
||
## 启用中文分词扩展
|
||
|
||
```sql
|
||
CREATE EXTENSION IF NOT EXISTS zhparser;
|
||
|
||
CREATE TEXT SEARCH CONFIGURATION chinese_mix (PARSER = zhparser);
|
||
-- DROP TEXT SEARCH CONFIGURATION IF EXISTS chinese_mix;
|
||
|
||
ALTER TEXT SEARCH CONFIGURATION chinese_mix
|
||
ADD MAPPING FOR n,v,a,i,e,l,d,j,m,q,r,t,u,w,x,z WITH zhparser;
|
||
|
||
SELECT to_tsvector('chinese_mix', 'Hello world, 你好世界,PostgreSQL 很棒');
|
||
```
|
||
|
||
## 测试
|
||
|
||
```sql
|
||
|
||
-- 创建测试表
|
||
CREATE TABLE articles (
|
||
id SERIAL PRIMARY KEY,
|
||
title TEXT,
|
||
content TEXT
|
||
);
|
||
|
||
-- 插入测试数据 (中英混合)
|
||
INSERT INTO articles (title, content) VALUES
|
||
('Introduction to AI', 'Artificial Intelligence is changing the world.'),
|
||
('人工智能简介', '人工智能正在改变世界,PostgreSQL 是很好的存储工具。'),
|
||
('PG 16 New Features', 'PostgreSQL 16 brings better performance and json features.');
|
||
|
||
-- 创建 GIN 索引 (加速检索的关键)
|
||
CREATE INDEX idx_articles_content ON articles USING GIN (to_tsvector('chinese_mix', content));
|
||
|
||
-- 执行搜索查询
|
||
-- 搜索 "人工智能"
|
||
SELECT title FROM articles WHERE to_tsvector('chinese_mix', content) @@ to_tsquery('chinese_mix', '人工智能');
|
||
|
||
-- 搜索 "PostgreSQL" (英文)
|
||
SELECT title FROM articles WHERE to_tsvector('chinese_mix', content) @@ to_tsquery('chinese_mix', 'PostgreSQL');
|
||
|
||
-- 搜索混合条件
|
||
SELECT title FROM articles WHERE to_tsvector('chinese_mix', content) @@ to_tsquery('chinese_mix', '数据库 & 改变');
|
||
``` |