관리 메뉴

IT.FARMER

search Engine - sphinx 본문

JAVA

search Engine - sphinx

아이티.파머 2013. 5. 21. 13:32
반응형

1. 개요

본 문서는 open source lib sphinx 라이브러리를 다운 받아 linux에 설치하여 검색엔진 환경을 셋팅 하는 것을 목적으로 한다.

mysql/php/java

2. 사전 준비

아래의 사전에 준비되어야 한다.

n Linux 설치(cent OS, ubuntu)

n Jdk 설치 최신 jdk 를 다운받아 설치한다.

3. 접속정보

서버 정보 1

IP

 

Ssh

 

Path

 

서버 정보 2

IP

 

Ssh

 

Path

 

4. Sphinx 설치

4.1. 스핑크스(sphinx) 다운로드-

 http://sphinxsearch.com/downloads/ 에서 sphinx-2.0.6 tar.gz 파일 다운로드

 

 

4.2. 파일 이동 및 설치

n sphinx 홈페에지에서 파일을 다운받아 /usr/local/{  } 으로 파일을 이동시킨다.

n Install the packages needed

apt-get install gcc make libmysqlclient15-dev libmysql++-dev

n Decompress it

tar xvfz sphinx-*.tar.gz

n Enter its directory

cd sphinx-*

n Run configure

./configure --prefix=/usr/local/sphinx --with-mysql

n Compile Sphinx

make

n Install Sphinx (as root)

 make install

참조 : http://acidborg.wordpress.com/2009/11/22/how-to-install-and-configure-sphinx-in-ubuntu-9-10-with-mysql-support/

 

n 스핑크스를 설치하게 되면 다음 위치에 설치된다.

# /usr/local/sphinx                         :  ${sphinxhome}

 

5. 스핑크스 환경설정

5.1. 샘플파일 확인 및 설치파일 복사

스핑크스가 설치된 디렉토리로 이동한다.

 # cd /usr/local/sphinx/ext          

 

5.2 ext폴더의 파일 확인

n example.sql - 샘플로 사용할 테이블 정보 및 데이터 정보 쿼리가 들어 있습니다.

n sphinx-min-conf.dist

n sphinx.conf.dist - DB 정보, 캐릭터 셋 정보, 인덱스 정보 등의 설정 정보를 담고 있습니다.

 

위 파일 중 sphinx.conf.dist의 데이터를 기반으로 파일을 sphinx.conf 로 복사여 사용합니다

 

# cp sphinx.conf.dist   sphinx.conf

# vi sphinx.conf

 

5.2. 환경설정 파일 수정

#####################################################################
     ## SQL settings (for 'mysql' and 'pgsql' types)
     #####################################################################

     # some straightforward parameters for SQL source types
     sql_host          = 
localhost

     sql_user          = 
mycup
     sql_pass          = 
1234
     sql_db             = 
test
     sql_port           = 
3306     # optional, default is 3306


     # pre-query, executed before the main fetch query
     # multi-value, optional, default is empty list of queries
     #
     

     sql_query_pre          = SET NAMES utf8   #block 되어있으니 풀어준다.



     # main document fetch query
     # mandatory, integer document ID field MUST be the first selected column
  
   sql_query          = \
          
SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content \
          FROM documents


     #스핑크스에서 한글을 사용하기 위해서는 sphinx.conf 아래와같이 수정해야 합니다.

     # charset encoding type
     # optional, default is 'sbcs'
     # known types are 'sbcs' (Single Byte CharSet) and 'utf-8'
     
charset_type          =  utf-8

     # charset definition and case folding rules "table"
     # optional, default value depends on charset_type
     #
     # defaults are configured to include English and Russian characters only
     # you need to change the table to include additional ones
     # this behavior MAY change in future versions
     #
     # 'sbcs' default value is
     # charset_table          = 0..9, A..Z->a..z, _, a..z, U+A8->U+B8, U+B8, U+C0..U+DF->U+E0..U+FF, U+E0..U+FF
     #
     # 'utf-8' default value is
     # charset_table          = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F,

 

 

U+430..U+44F                                                                      #제공되는 원본

        charset_table          = 0..9, A..Z->a..z, _, a..z, U+AC00..U+D7A3, U+1100..U+1159, U+1161..U+11A2, U+11A8..U+11F9                 #한글을 사용하기위한 charset_table

 

     # n-gram length to index, for CJK indexing
     # only supports 0 and 1 for now, other lengths to be implemented
     # optional, default is 0 (disable n-grams)
     #
     
ngram_len          = 1



     # n-gram characters list, for CJK indexing
     # optional, default is empty
     #
     # ngram_chars          = U+3000..U+2FA1F

        ngram_chars          = U+AC00..U+D7A3




보라색은 사용자 환경에 따라 변경해야 부분
빨간색은 한글을 사용할  변경해야 부분

 

 

 

6. Sphinx Sample Test

6.1. example.sql 파일 sample db으로 dump 이동

# cd /usr/local/sphinx/etc

# mysql -u root -p test < /usr/local/sphinx/etc/example.sql

 

 

6.2. indexer 인덱스 생성

# /usr/local/sphinx/bin/indexer  또는 #/usr/local/sphinx/bin/indexer --config /usr/local/sphinx/etc/sphinx.conf --all

 

6.2.1. 전체 인덱싱

# /usr/local/sphinx/bin/indexer --all

 

6.2.2. 실행 전체 인덱싱

# /usr/local/sphinx/bin/indexer --all --rotate

 

6.2.3. PHP Test

PHP 스크립트에서 인덱스 쿼리를 사용할 경우

 

search데몬을 실행해야 한다.

# cd /usr/local/sphinx/etc

# /usr/local/sphinx/bin/searchd

 (데몬 종료시 #pkill -9 searched)

 

샘플파일 폴더에 접근하여 파일 실행

# cd /usr/local/sphinx-2.0.6-release/api

# php test.php 국어       

# php test.php [검색어]

 

검색결과 Sample

 

root@skan-ubuntu:/usr/local/sphinx-2.0.6-release/api# php test.php sun

Query 'un ' retrieved 18 of 18 matches in 0.000 sec.

Query stats:

    'sun' found 20 times in 18 documents

    'un' found 0 times in 0 documents

 

Matches:

1. doc_id=985844, weight=1

2. doc_id=985845, weight=1

3. doc_id=985846, weight=1

4. doc_id=985847, weight=1

5. doc_id=985848, weight=1

6. doc_id=985849, weight=1

7. doc_id=985850, weight=1

8. doc_id=985851, weight=1

9. doc_id=991116, weight=1

10. doc_id=991117, weight=1

11. doc_id=991118, weight=1

12. doc_id=991119, weight=1

13. doc_id=1900893, weight=1

14. doc_id=1901086, weight=1

15. doc_id=1901091, weight=1

16. doc_id=1901095, weight=1

17. doc_id=1967642, weight=1

18. doc_id=1967706, weight=1

 

 

 

5.5.4 shell 테스트

./search [검색어

 

http://system.neulwon.com/xe/?document_srl=873&mid=linux_os&sort_index=readed_count&order_type=desc

 

http://blog.daum.net/ezway/9

http://sphinxsearch.com/docs/current.html#installing     - 공식 스핑크스 사이트


 

6.3. JAVA Test

6.3.1. 실행 준비

n Run Configurations 설정

l  Arguments 값으로 검색 하고자 하는 값을 입력 합니다. ex:) 국어

l  Run 버튼을 클릭하여 실행

 

반응형

'JAVA' 카테고리의 다른 글

대용량 검색 방법 Mybatis ResultHander  (2) 2015.01.29
JAVA custom listener  (0) 2015.01.15
Apache HttpClient - Rest call Client  (0) 2013.05.03
Resfull 종류  (0) 2013.05.03
eclipse maven war  (0) 2012.07.24