1. 개요

Solr에 대해 알아보고 설치해보자.


2. 다운로드

링크 : http://www.apache.org/dyn/closer.lua/lucene/solr/7.2.1

(최신버전은 꼭 확인해보자)


3. 설치

다운로드 후 압축을 풀면 이런 구조가 된다.

$ ls -tlr
total 1508
-rw-r--r--  1 ec2-user ec2-user   7490 Jan  9 21:26 README.txt
-rw-r--r--  1 ec2-user ec2-user  24914 Jan  9 21:26 NOTICE.txt
-rw-r--r--  1 ec2-user ec2-user  12646 Jan  9 21:26 LICENSE.txt
-rw-r--r--  1 ec2-user ec2-user 668112 Jan  9 21:32 LUCENE_CHANGES.txt
-rw-r--r--  1 ec2-user ec2-user 754525 Jan  9 21:32 CHANGES.txt
drwxr-xr-x  3 ec2-user ec2-user   4096 Jan  9 21:32 bin
drwxr-xr-x 11 ec2-user ec2-user   4096 Jan 10 00:54 contrib
drwxr-xr-x  7 ec2-user ec2-user   4096 Apr  1 23:49 example
drwxr-xr-x  2 ec2-user ec2-user  36864 Apr  1 23:49 licenses
drwxr-xr-x 10 ec2-user ec2-user   4096 Apr  1 23:50 server
drwxrwxr-x  4 ec2-user ec2-user   4096 Apr  1 23:50 dist
drwxrwxr-x  3 ec2-user ec2-user   4096 Apr  1 23:50 docs

4. 실행

다짜고짜 실행을 해본다.

$ ./solr
Your current version of Java is too old to run this version of Solr
We found version 1.7.0_161, using command '/usr/lib/jvm/jre/bin/java -version', with response:
java version "1.7.0_161"
OpenJDK Runtime Environment (amzn-2.6.12.0.75.amzn1-x86_64 u161-b00)
OpenJDK 64-Bit Server VM (build 24.161-b00, mixed mode)

Please install latest version of Java 1.8 or set JAVA_HOME properly.

Debug information:
JAVA_HOME: /usr/lib/jvm/jre
Active Path:
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin:/home/ec2-user/.local/bin:/home/ec2-user/bin

PATH상의 자바 버전이 너무 낮아서 안된다.

$ export JAVA_HOME=/sw/jdk1.8.0_161

다시 실행한다.

$ ./solr

Usage: solr COMMAND OPTIONS
       where COMMAND is one of: start, stop, restart, status, healthcheck, create, create_core, create_collection, delete, version, zk, auth, assert

  Standalone server example (start Solr running in the background on port 8984):

    ./solr start -p 8984

  SolrCloud example (start Solr running in SolrCloud mode using localhost:2181 to connect to Zookeeper, with 1g max heap size and remote Java debug options enabled):

    ./solr start -c -m 1g -z localhost:2181 -a "-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=1044"

Pass -help after any COMMAND to see command-specific usage information,
  such as:    ./solr start -help or ./solr stop -help

그럼 위에서 가이드된대로 실행해보겠다.

$ ./solr start -p 8984
Waiting up to 180 seconds to see Solr running on port 8984 [\]
Started Solr server on port 8984 (pid=2933). Happy searching!

8984번으로 잘 실행된 것인가 보자.

$ lsof -i:8984
COMMAND  PID     USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
java    2933 ec2-user  119u  IPv6  12587      0t0  TCP *:8984 (LISTEN)

5. Admin 접속

http://로 Solr 포트로 접속하면 된다.


6. Core

초기에는 Core가 없다. 따라서 Admin에 "No cores available Go and create one"라고 나온다.

다음과 같이 CLI 상에서 core 생성할 수 있다.

$ ./solr create -c solrCore
WARNING: Using _default configset. Data driven schema functionality is enabled by default, which is
         NOT RECOMMENDED for production use.

         To turn it off:
            curl http://localhost:8984/solr/solrCore/config -d '{"set-user-property": {"update.autoCreateFields":"false"}}'

Created new core 'solrCore'

7. DB 연결

7-1.  드라이버 설정

  • MySQL JDBC Driver 다운로드 : 나는 mysql-connector-java-5.1.46.jar 파일
  • solr-7.2.1/server/solr-webapp/webapp/WEB-INF/lib 하위에 해당 파일 복사 (솔라 홈디렉토리는 사람마다 다름)

7-2. solrconfig.xml 파일 설정 (server/solr/[Core명]/conf/solrconfig.xml)

  <lib dir="${solr.install.dir:../../../..}/contrib/dataimporthandler/lib/" regex=".*\.jar" />
  <lib dir="${solr.install.dir:../../../..}/dist/" regex="solr-dataimporthandler-\d.*\.jar" />

  <requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
    <lst name="defaults">
      <str name="config">db-data-config.xml</str>
    </lst>
  </requestHandler>

7-3. 테스트 데이터 생성

우선 테이블을 만든다.

create table mydata (
  id integer,
  subject varchar(100),
  content varchar(100),
  author varchar(100)
);

테스트용 데이터를 넣는다.

insert into mydata values (1, "The Apache News Round-up: week ending 30 March 2018", "Let's bid March farewell with a look back at the many Apache activities over the past week:", "ASF");
insert into mydata values (2, "Fwd: [ANNOUNCE] Apache Kafka 1.1.0 Released", "The Apache Kafka community is pleased to announce the release for", "ASF");
insert into mydata values (3, "[ANNOUNCE] Apache Accumulo 1.7.4", "The Apache Accumulo project is pleased to announce the release of Apache Accumulo 1.7.4! This release", "ASF");
insert into mydata values (4, "CentOS Linux can only come from the CentOS Project", "We didn’t think we would have to say this, but here it is:", "CentOS");
insert into mydata values (5, "CentOS Atomic Host 7.1802 Available for Download", "The CentOS Atomic SIG has released an updated version of CentOS Atomic Host (7.1802), a lean operating", "CentOS");

7-4. db-data-config.xml (server/solr/[Core명]/conf/db-data-config.xml)

<dataConfig>
  <dataSource type="JdbcDataSource"
              driver="com.mysql.jdbc.Driver"
              url="jdbc:mysql://[주소]:[포트]/[DB명]"
              user="[계정]"
              password="[비밀번호]" />
  <document>
    <entity pk="id" name="mydata" query="select * from mydata limit 1000;" >
      <field column="id" name="id"/>
      <field column="subject" name="subject"/>
      <field column="content" name="content"/>
      <field column="author" name="author"/>
    </entity>
  </document>
</dataConfig>


8. Schema

  • Admin 페이지에서 core 선택 후 Schema로 들어간다.
  • Add Field하여 필드 설정을 추가한다. (위의 field 설정은 Dataimport를 위한 필드 설정일 뿐이다)

9. Dataimport

Admin 페이지에서 core 선택 후 Dataimport, 그리고 Execute 실행한다.

Indexing completed. Added/Updated: 5 documents. Deleted 0 documents.
Requests: 1 , Fetched: 5 , Skipped: 0 , Processed: 5 
Started: less than a minute ago 

데이터 임포트 후 Query에서 확인한다.


10. 기타

bin/solr 기동 시 -m 옵션을 주면 메모리 지정이 가능하다. (예: -m 4g)