[Samba] Spotlight検索の効くSambaの構築をする

https://blog.tndl.net/2021/12/14/1121/spotlight%E6%A4%9C%E7%B4%A2%E3%81%AE%E5%8A%B9%E3%81%8Fsamba%E3%81%AE%E6%A7%8B%E7%AF%89%E3%82%92%E3%81%99%E3%82%8B/

2021年12月14日 by える*

Fedoraに含まれているSambaが、いつの間にかElasticsearchをバックエンドとして使うmacOS向けのSpotlight検索向けのビルドになっていたので、きちんと動くのか検証するためにあれこれした記録。
インストール

公式の手順に従ってElasticsearchのリポジトリを追加し、インストールをする。

$ cat << EOF > /etc/yum.repos.d/elasticsearch.repo
[elasticsearch]
name=Elasticsearch repository for 7.x packages
baseurl=https://artifacts.elastic.co/packages/7.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
EOF
$ dnf install -y samba elasticsearch poetry

設定

Elasticsearchはローカルで使うだけなので、セキュリティなしでとりあえず運用する。(というか、SambaがID/PW認証対応してないように見える)

$ cat << EOF >> /etc/elasticsearch/elasticsearch.yml
xpack.security.enabled: false
discovery.type: single-node
EOF

SambaからSpotlightを使えるようにする。

[global]
workgroup = WORKGROUP
unix extensions = no
spotlight backend = elasticsearch
elasticsearch:index = files

# support macOS
vfs objects = acl_xattr catia fruit streams_xattr
fruit:locking = netatalk
fruit:encoding = native
fruit:wipe_intentionally_left_blank_rfork = yes
fruit:delete_empty_adfiles = yes
veto files = /._*/.DS_Store/
delete veto files = yes

[share]
path = /PATH_TO_SHARE
writable = yes
wide links = yes
follow symlinks = yes
spotlight = yes

それぞれのサービスの有効化と起動

$ systemctl enable nmb.service –now
$ systemctl enable smb.service –now
$ systemctl enable elasticsearch.service –now

Elasticsearchのindexの作成

Sambaのwikiの該当ページには、fscrawlerかfs2es-indexerが使えると書いてあるが、どちらもElasticsearchの知識なしで動かしただけでは、期待するような動作はしない。
今回一番ハマったポイントだったが、楽するのを諦めてElasticsearchのドキュメント、Sambaの該当部分の実装を確認しつつ、fs2-indexerをフォークして動くようにした。

フォークして変更した部分は、ほぼElasticsearchのmapping部分のみで、元々の状態ではSpotlight検索時に期待するような結果が得られるようなトークンが生成されていなかった。

しっかりとした変更内容はコミットログを見てもらえればわかるが、ファイルパスをpath_hierarchyのtokenizerをつかったanalyzerで、ファイル名をsimpleのanalyzerでそれぞれ処理するように設定しつつ、設定ファイルを外部ファイルにできるようにした。

これを読んでくれてる人は、そんなことを悩む必要はなく以下の手順でサクッとindexが作成できる。

$ git clone https://github.com/eru/fs2es-indexer.git
$ cd fs2es-indexer
$ poetry install
$ mkdir -p /etc/fs2es-indexer
$ cp config.dist.yml /etc/fs2es-indexer/config.yml
$ cp setting.json mapping.json /etc/fs2es-indexer/

/etc/fs2es-indexer/config.yml の変更内容は以下の通り

— config.dist.yml 2021-12-14 03:04:46.849276941 +0900
+++ /etc/fs2es-indexer/config.yml 2021-12-14 03:11:11.902831085 +0900
@@ -5,6 +5,7 @@

# The directories which should be indexed
directories:
+ – “/PATH_TO_SHARE”
# – “/my-storage-directory”

@@ -12,13 +13,14 @@
# TODO Papierkorb

# (Optional) Exclude directories / files from the index
-#exclusions:
+exclusions:
# Exclusion via a simple string search in the full path of the file / directory.
# If any of the given strings are found in the full path, it wont be added to the index.
# Usually faster than a regular expression!
-# partial_paths:
-# – “.DS_Store”
-# – “._.DS_Store”
+ partial_paths:
+ – “.DS_Store”
+ – “._.DS_Store”
+ – “lost+found”

# Exclusion via testing if a regular expression matches the full path of the file / directory.
# If any of the regular expression matches, it wont be added to the index.

終わりに

Sambaのwikiに解説らしい解決もなく、SambaでSpotlightを使っている人があまりに少ないため参考になる情報もでてこないため、これがSambaでSpotlightを使いたい奇特な人の参考になれば嬉しく思う。

コメント