http://www.sphinxsearch.com/docs/current.html
http://www.sphinxsearch.com/wiki/doku.php?id=charset_tables
http://code.google.com/p/sphinxsearch/source/browse/
http://code.google.com/p/sphinxsearch/source/browse/branches/rel099/api/sphinxapi.php
http://code.google.com/p/sphinxsearch/source/browse/branches/rel099/api/test.php


1. 설치

정도는 알아서...-_-;
emerge app-misc/sphinx 하면 끝나는 환경에 있는 사람이 이 이상 자세하겐 무리~



2. sphinx.conf

다국어 검색을 위해 각각의 문자범위를 일일히 conf 에 적어줘야 한다.
게다가 한줄 길이 제한까지 있어서 정규식으로 대충 정리해 넣었다.
원본 테이블은
http://www.sphinxsearch.com/wiki/doku.php?id=charset_tables
요기 맨 앞의 txt 링크이다.
conf 파일은 첨부한다.



3. query

스핑크스는 인덱싱 할 데이터가 많을 경우를 위해 범위를 나눠 쿼리해 오는 것이 가능하고,
새 범위만 인덱싱을 한 후 기존 인덱스 정보에 병합하는 것이 가능하다.
본 예제는 새 범위 인덱싱 후 병합으로 설명한다.

스핑크스의 쿼리문은 맨 앞의 것이 반드시 고유번호(sequence) 여야 하며,
그 필드명이 무엇이 되었든 간에 스핑크스 내에서는 무조건 id 이다.


source main 중
sql_query = \
SELECT seq, uid, author, subject, contents \
FROM xenobb_sphinx_view \
WHERE sphinx_indexed = TRUE

테이블에 sphinx_indexed 라는 필드를 추가했고, main 은 이미 인덱싱 된것만 쿼리하게 했다.

source delta : main 중
sql_query = \
SELECT seq, uid, author, subject, contents \
FROM xenobb_sphinx_view \
WHERE sphinx_indexed = FALSE

sql_query_post = UPDATE xenobb SET sphinx_indexed = TRUE WHERE sphinx_indexed = FALSE

인덱싱 되지 않은 것을 쿼리해 온 후 인덱싱 되었다고 표기해 준다.



4. indexer

conf 를 다 작성했으면 인덱스를 생성한다.
$ indexer main
$ indexer delta



5. crontab

searchd 가 대몬이라 알아서 계속 인덱싱 해 줄줄 알았는데 안되나보다.

#!/bin/bash
cd /home/xenoside/sphinx
if [ -f need_indexing ]; then
    rm need_indexing
    indexer delta --rotate
    indexer --merge main delta --rotate
    #if [[ "x`date +%H%M`" == "x0400" ]]; then
    #    indexer main --rotate
    #fi
fi

* * * * * (매분) 으로 걸어주자.

게시판에서 insert, update, delete 가 발생하면 /home/xenoside/sphinx/need_indexing 파일을 빈 파일로 생성해 주면 1분 내에 인덱싱 된다.

주석 부분은 찔끔찔끔 병합되는 main 인덱스가 혹 느려지면 새로 인덱싱 시키기 위한건데 전체가 재 인덱싱 되므로 굳이 자동으로 할 필요 없이 느려졌다 생각되면 한번 해 주면 된다.



6. searchd

/etc/init.d/searchd start
rc-update add searchd default
배포판 별로 알아서 시작 스크립트에 넣어주자~



7. php
http://code.google.com/p/sphinxsearch/source/browse/branches/rel099/api/sphinxapi.php
요거를 이용한다.
자세한 예제는
http://code.google.com/p/sphinxsearch/source/browse/branches/rel099/api/test.php
요기잉다.

아래는 랭킹이고 순서고 다 필요 없이 걍 검색해 오는 간단 예제이다.

if(isset($_GET['q'])) {
$_GET['q'] = trim($_GET['q']);
if($_GET['q'] != '') {
require_once 'sphinxapi.php';
$cl = new SphinxClient();
$cl->SetServer('/var/run/searchd.sock', 5433);
$cl->SetConnectTimeout(1);
$cl->SetArrayResult(true);
$cl->SetWeights(array(100, 1));
$cl->SetMatchMode(SPH_MATCH_ALL);
$cl->SetLimits(0, 100);
$cl->SetRankingMode(SPH_RANK_NONE);
if(false !== ($res = $cl->Query($_GET['q'], '*'))) {
$seqs = array();
foreach($res['matches'] as $row) {
$seqs[] = $row['id'];
}
if(count($seqs) != 0) {
$sel->wheres[] = 'bb.seq IN ('.implode(',', $seqs).')';
}
}
}
}


매뉴얼 잘 보면 실제 쿼리문 처럼 순서 정하고 limit 해 오고 하는 것 다 된다.



Written by Song Hyo-Jin (shj at xenosi.de)
License : Creative Commons - Attribution (CC-BY)
출처 : http://www.phpschool.com/link/tipntech/71173

'Search Engine > Sphinx' 카테고리의 다른 글

스핑크스 설정  (0) 2012.06.05
Sphinx  (0) 2012.02.13
sphinx 검색엔진 한글 검색 설정  (0) 2012.02.13
스핑크스(sphinx) 검색 엔진 reindex  (1) 2012.02.13
스핑크스(sphinx) 검색엔진 설치 및 유니코드 셋팅  (0) 2012.02.13

+ Recent posts