Help us understand the problem. What is going on with this article?

DockerとSolrでサクッと全文検索

More than 3 years have passed since last update.

必要環境

  • Linux
  • Docker

Solrの起動

DockerHubの公式リポジトリを使います。
https://hub.docker.com/_/solr/

設定などはコンテナ外で管理したいので、-vでボリュームを用意しておきます。

mkdir ~/solr-cores
docker run --name solr -d -p 8983:8983 -v ~/solr-cores:/var/solr/cores -t solr
docker exec -t -u root solr chown solr:solr /var/solr/cores

コアの作成

Dockerコンテナに入ってコアを作成します。
bashを起動するとsolrユーザーで/opt/serverがワーキングディレクトリになった状態になります。
設定テンプレート(server/solr/configsets/data_driven_schema_configs)をコピーして設定を作成した後にコアを作成するAPIを実行します。

docker exec -ti solr /bin/bash
cp -R server/solr/configsets/data_driven_schema_configs /var/solr/cores/main
curl "http://localhost:8983/solr/admin/cores?action=CREATE&name=main&instanceDir=/var/solr/cores/main"

ドキュメントの登録

./bin/postコマンドでドキュメントを登録できます。
試しに Solr の README.txt を登録してみます。

./bin/post -c main README.txt

curlで検索

curl "http://localhost:8983/solr/main/select?indent=on&q=Software&wt=json"

{
  "responseHeader":{
    "status":0,
    "QTime":0,
    "params":{
      "q":"Software",
      "indent":"on",
      "wt":"json"}},
  "response":{"numFound":1,"start":0,"docs":[
      {
        "id":"/opt/solr/README.txt",
        "stream_size":[7242],
        "x_parsed_by":["org.apache.tika.parser.DefaultParser",
          "org.apache.tika.parser.txt.TXTParser"],
        "stream_content_type":["text/plain"],
        "content_encoding":["ISO-8859-1"],
        "resourcename":["/opt/solr/README.txt"],
        "content_type":["text/plain; charset=ISO-8859-1"],
        "_version_":1534201179174600704}]
  }}

PHPで検索

PHPからSolrにアクセスするには、peclのsolr拡張をインストールしておく必要があります。

solr_test.php
<?php

$client = new \SolrClient([
  'hostname' => 'localhost',
  'path' => 'solr/main',
  'port' => 8983
]);

$query = new \SolrQuery('*:*');
$response = $client->query($query);
$result = $response->getResponse();
var_dump($result);
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした