下一个云存储 + Elasticsearch 完成了日文全文搜索的笔记

首先

我们在公司内部搭建了一个可以轻松构建私有云的 Nextcloud,并将其作为员工之间的文件共享服务进行运营。
在 Nextcloud 中的“应用程序”中,可以添加功能,其中包括全文搜索,并且可以与 Elasticsearch 配合使用以构建文档和 PDF 的全文搜索。所以,我尝试先让日文全文搜索正常工作。
Nextcloud 本身的构建是使用 Docker + Rancher v1.6 系列完成的,因此 Elasticsearch 也采用相同的方式进行构建。我们已提供了使用的 docker-compose.yml 文件,但由于这里略去了详细的讨论,请谅解。

环境

- Docker + Rancher v1.6.x
- nextcloud:14.0.3-apache
- elasticsearch:5.3-alpine
    - analysis-kuromoji
    - analysis-icu
    - ingest-attachment
- postgres:11-alpine

请根据具体环境进行编辑,以下的 yaml 文件需要在拥有至少6GB实现内存的主机上运行。

version: '2'
services:
  app:
    image: nextcloud:14.0.3-apache
    volumes:
    - nextcloud-data:/var/www/html
    links:
    - postgres:postgres
    - elasticsearch:elasticsearch
  elasticsearch:
    image: elasticsearch:5.3-alpine
    environment:
      ES_JAVA_OPTS: -Xms6G -Xmx6G
    volumes:
    - nextcloud-es-config:/usr/share/elasticsearch/config
    - nextcloud-es-data:/usr/share/elasticsearch/data
    - nextcloud-es-plugins:/usr/share/elasticsearch/plugins
    mem_reservation: 6291456000
    command:
    - sh
    - -c
    - "./bin/elasticsearch-plugin list | grep -q analysis-kuromoji || ./bin/elasticsearch-plugin install analysis-kuromoji; ./bin/elasticsearch-plugin list | grep -q analysis-icu || ./bin/elasticsearch-plugin install analysis-icu; ./bin/elasticsearch-plugin list | grep -q ingest-attachment || ./bin/elasticsearch-plugin install ingest-attachment; /docker-entrypoint.sh elasticsearch"
  postgres:
    image: postgres:11-alpine
    environment:
      POSTGRES_PASSWORD: <パスワード>
      POSTGRES_DB: <DB 名>
      POSTGRES_USER: <ユーザ名>
    volumes:
    - nextcloud-db:/var/lib/postgresql/data
volumes:
  nextcloud-es-plugins:
    external: true
    driver: rancher-nfs
  nextcloud-es-config:
    external: true
    driver: rancher-nfs
  nextcloud-db:
    external: true
    driver: rancher-nfs
  nextcloud-data:
    external: true
    driver: rancher-nfs
  nextcloud-es-data:
    external: true
    driver: rancher-nfs

下一个云的配置

GUI 倚靠

添加应用程序

nextcloud-installed-apps.png

全文搜索的设置

fulltext-search-configulation-gui.png

命令行界面

这次我们将使用提供的 Nextcloud CLI 进行操作(请阅读这里关于 CLI 的内容)。
执行 Nextcloud Docker 容器(用户名设为 www-data)。

[yttm@localhost ~]$ docker exec -it -u www-data <CONTAINER NAME> /bin/bash

您可以使用 fulltextsearch:test 进行整体测试,并确认其与 Elasticsearch 的连接。

www-data@nextcloud-app-1:~/html$ php ./occ fulltextsearch:test

.Testing your current setup:
Creating mocked content provider. ok
Testing mocked provider: get indexable documents. (2 items) ok
Loading search platform. (Elasticsearch) ok
Testing search platform. ok
Locking process ok
Removing test. ok
Pausing 3 seconds 1 2 3 ok
Initializing index mapping. ok
Indexing generated documents. ok
Pausing 3 seconds 1 2 3 ok
Retreiving content from a big index (license). (size: 32386) ok
Comparing document with source. ok
Searching basic keywords:
 - 'test' (result: 1, expected: ["simple"]) ok
 - 'document is a simple test' (result: 2, expected: ["simple","license"]) ok
 - '"document is a test"' (result: 0, expected: []) ok
 - '"document is a simple test"' (result: 1, expected: ["simple"]) ok
 - 'document is a simple -test' (result: 1, expected: ["license"]) ok
 - 'document is a simple +test' (result: 1, expected: ["simple"]) ok
 - '-document is a simple test' (result: 0, expected: []) ok
Updating documents access. ok
Pausing 3 seconds 1 2 3 ok
Searching with group access rights:
 - 'license' - [] -  (result: 0, expected: []) ok
 - 'license' - ["group_1"] -  (result: 1, expected: ["license"]) ok
 - 'license' - ["group_1","group_2"] -  (result: 1, expected: ["license"]) ok
 - 'license' - ["group_3","group_2"] -  (result: 1, expected: ["license"]) ok
 - 'license' - ["group_3"] -  (result: 0, expected: []) ok
Searching with share rights:
 - 'license' - notuser -  (result: 0, expected: []) ok
 - 'license' - user2 -  (result: 1, expected: ["license"]) ok
 - 'license' - user3 -  (result: 1, expected: ["license"]) ok
Removing test. ok
Unlocking process ok

如果在这里执行fulltextsearch:index,将会为已经上传到Nextcloud的文件创建搜索索引。

www-data@nextcloud-app-1:~/html$ php ./occ fulltextsearch:index
Options: []
Memory: 10 MB
┌─ Indexing  ────
│ Action: fillDocument
│ Provider: Files                Account: ********
│ Document: 3322
│ Info: httpd/unix-directory
│ Title: ********************************
│ Content size: 0
│ Progress:    all/127
└──
┌─ Results ────
│ Result:      0/0
│ Index:
│ Status:
│ Message:
│
│
└──
┌─ Errors ────
│ Error:     0/0
│ Index: 
│ Exception: 
│ Message: 
└──
## x:first result ## c/v:prec/next result ## b:last result
## f:first error ## h/j:prec/next error ## d:delete error ## l:last error
## q:quit ## p:pause

然而,当添加文件时,需要每次手动执行 fulltextsearch:index,所以我们将自动创建索引。

自动创建索引

如果将fulltextsearch:live作为守护程序来运行,上传的文件将会随时进行索引化,因此我们在nextcloud容器内创建这个机制(如果有更好的方法就更好了…)。
在本次使用的nextcloud容器中,可以使用start-stop-daemon来操作守护程序,因此我们将创建一个操作守护程序的脚本(参考了这个)。

#!/bin/sh

# Quick start-stop-daemon example, derived from Debian /etc/init.d/ssh
set -e

# Must be a valid filename
NAME=fulltextsearch-daemon
PIDFILE=/var/run/$NAME.pid
#This is the command to be run, give the full pathname
DAEMON=/usr/local/bin/php
DAEMON_OPTS="/var/www/html/occ fulltextsearch:live"

export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"

case "$1" in
  start)
        echo -n "Starting daemon: "$NAME
        start-stop-daemon --start --background --quiet --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_OPTS
        echo "."
        ;;
  stop)
        echo -n "Stopping daemon: "$NAME
        start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE
        echo "."
        ;;
  restart)
        echo -n "Restarting daemon: "$NAME
        start-stop-daemon --stop --quiet --oknodo --retry 30 --pidfile $PIDFILE
        start-stop-daemon --start --background --quiet --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_OPTS
        echo "."
        ;;

  *)
        echo "Usage: "$1" {start|stop|restart}"
        exit 1
esac

exit 0

然后将其在容器中执行。

www-data@nextcloud-app-1:~/html$ ./ssd.sh start
Starting daemon: fulltextsearch-daemon.

做完了!

done.gif

请参阅

请查看以下链接以了解更多详细信息,由于公式在GitHub Wiki中有记录。
https://github.com/nextcloud/fulltextsearch/wiki/Basic-Installation
※ 公式在实时索引中使用systemd,但在容器环境中不能正常运行。

bannerAds