sphinx에서는 searchd 를 이용한 검색 쿼리문과 php 등을 이용한 api 를 제공하고 있습니다.
sphinx 설치를 위해 소스 파일을 압축해제했던 /usr/local/src/sphinx-2.1.4-release/api 경로로
이동해보면 php 등의 api 사용 예제 파일이 존재합니다. juso.sir.co.kr 에서는 php api 를 이용했습니다.
아래는 juso.sir.co.kr 에서 사용자의 검색 요청을 받아 검색 요청을 하고 결과를 처리하는 파일의 코드입니다.
[code]
<?php
include_once('./_common.php');
if(!$q) {
$juso['error'] = '검색어를 입력해 주십시오.';
echo $_GET['callback'].'('.json_encode($juso).')';
exit;
}
// spninx api load
require ( G5_LIB_PATH.'/sphinx/sphinxapi.php' );
$cl = new SphinxClient ();
$host = $config['cf_sphinx_host'];
$port = $config['cf_sphinx_port'];
$index = $config['cf_sphinx_index'];
$rows = $config['cf_page_rows'];
$cl->SetServer ( $host, $port );
$cl->SetConnectTimeout ( 1 );
$cl->SetArrayResult ( true );
$cl->SetWeights ( array ( 100, 1 ) );
$cl->SetMatchMode ( SPH_MATCH_EXTENDED );
//$cl->setSortMode( SPH_SORT_ATTR_ASC, 'sn' );
if ($page == '') $page = 1;
$offset = ($page - 1) * $rows;
$cl->SetLimits ( $offset, $rows, $config['cf_max_rows']);
$query = '';
if($sido)
$query .= ' @sido "'.$sido.'" ';
if($gugun)
$query .= ' @gugun "'.$gugun.'" ';
$sword = explode(' ', trim($q));
$field = '(doro,jibeon)';
foreach($sword as $val) {
$word = trim($val);
if(!$word)
continue;
$query .= ' @'.$field.' "'.$word.'*" ';
}
$res = $cl->Query ( $query, $index );
$error = '';
if ($res === false) {
$error = '검색실패 : ' . $cl->GetLastError();
} else {
$total_count = $res['total'];
$total_page = ceil($total_count / $rows); // 전체 페이지 계산
$count = count($res['matches']);
if($_GET['link'] != 'false')
$link = true;
else
$link = false;
ob_start();
?>
<div class="result_msg">
검색결과 <b><?php echo number_format($total_count); ?></b>
</div>
<?php
for($i=0; $i<$count; $i++) {
$data = $res['matches'][$i]['attrs'];
$eupmyeon = false;
$zipcode = preg_replace('/([0-9]{3})([0-9]{3})/', '\\1-\\2', $data['zipcode']);
$zip = explode('-', $zipcode);
$zip1 = $zip[0];
$zip2 = $zip[1];
$addr1 = $data['sido'].' '.$data['gugun'];
if($data['beopname'] && preg_match('/(읍|면)$/', $data['beopname'])) {
$addr1 .= ' '.$data['beopname'];
$eupmyeon = true;
}
$addr1 .= ' '.$data['doroname'];
if($data['jiha'])
$jiha = ' 지하';
else
$jiha = ' ';
$addr1 .= $jiha.$data['geonbon'];
if($data['geonbu'])
$addr1 .= '-'.$data['geonbu'];
$addr3 = '';
if($data['beopname'] && !$eupmyeon)
$addr3 = ' ('.$data['beopname'];
if($data['geonname'] || $data['geonsangse']) {
if($addr3)
$addr3 .= ', ';
else
$addr3 = ' (';
}
if($data['geonname']) {
$addr3 .= $data['geonname'];
} else {
if($data['geonsangse'])
$addr3 .= $data['geonsangse'];
else {
if($data['daryang'] && !$data['geonname2']) {
if($addr3)
$addr3 .= ', '.$data['daryang'];
else
$addr3 .= ' ('.$data['daryang'];
} else if(!$data['daryang'] && $data['geonname2']) {
if($addr3)
$addr3 .= ', '.$data['geonname2'];
else
$addr3 .= ' ('.$data['geonname2'];
}
}
}
if($addr3)
$addr3 .= ')';
$addr_ji = $data['sido'].' '.$data['gugun'];
if($data['beopname'])
$addr_ji .= ' '.$data['beopname'];
if($data['ri'])
$addr_ji .= ' '.$data['ri'];
if($data['san'])
$san = ' 산';
else
$san = ' ';
$addr_ji .= $san.$data['jibon'];
if($data['jibu'])
$addr_ji .= '-'.$data['jibu'];
if($data['geonname']) {
$addr_ji .= ' '.$data['geonname'];
} else {
if($data['geonsangse'])
$addr_ji .= ' '.$data['geonsangse'];
}
$addr3 = htmlentities($addr3, ENT_QUOTES, "UTF-8");
$addr_ji = htmlentities($addr_ji, ENT_QUOTES, "UTF-8");
if($i == 0)
echo '<ul>'.PHP_EOL;
echo '<li>'.PHP_EOL;
echo '<span></span>';
if($link)
echo "<a href='#' onclick='put_data(\"".$zip1."\", \"".$zip2."\", \"".trim($addr1)."\", \"".trim($addr3)."\", \"".trim($addr_ji)."\"); return false;'>";
echo '<strong>'.$zipcode.'</strong>';
echo ' '.$addr1;
echo $addr3;
if($link)
echo '</a>';
echo '<div>(지번주소) '.$addr_ji.'</div>';
echo '</li>'.PHP_EOL;
}
if($i > 0)
echo '</ul>';
else
echo '<div class="result_msg result_fail">검색결과가 없습니다.</div>';
//echo '<p>실행시간 : '.$res['time'].'</p>';
$pagelist = get_paging($is_mobile ? $config['cf_mobile_list_pages'] : $config['cf_list_pages'], $page, $total_page);
echo $pagelist;
$contents = ob_get_contents();
ob_end_clean();
}
$jusu = array();
$juso['error'] = $error;
$juso['juso'] = $contents;
echo $_GET['callback'].'('.json_encode($juso).')';
?>
[/code]
sphinx의 api 관련 자료는 http://sphinxsearch.com/docs/archives/2.1.4/api-reference.html 를 참고하시면 됩니다.
그외 검색관련 query 는 http://sphinxsearch.com/docs/archives/2.1.4/searching.html 를 참고하시면 됩니다.
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 6630 |
차가운바람
|
12년 전 | 1162 | |
| 6629 | 12년 전 | 1157 | ||
| 6628 |
put777
|
12년 전 | 642 | |
| 6627 |
|
12년 전 | 698 | |
| 6626 | 12년 전 | 1616 | ||
| 6625 |
|
12년 전 | 718 | |
| 6624 |
미켈란젤로의왼손
|
12년 전 | 484 | |
| 6623 |
|
12년 전 | 1044 | |
| 6622 | 12년 전 | 1392 | ||
| 6621 |
차가운바람
|
12년 전 | 631 | |
| 6620 | 12년 전 | 732 | ||
| 6619 |
|
12년 전 | 969 | |
| 6618 | 12년 전 | 1741 | ||
| 6617 | 12년 전 | 639 | ||
| 6616 |
차가운바람
|
12년 전 | 894 | |
| 6615 | 12년 전 | 2853 | ||
| 6614 | 12년 전 | 614 | ||
| 6613 |
강명구베드로
|
12년 전 | 530 | |
| 6612 |
|
12년 전 | 426 | |
| 6611 | 12년 전 | 1206 | ||
| 6610 | 12년 전 | 1022 | ||
| 6609 | 12년 전 | 3679 | ||
| 6608 |
|
12년 전 | 831 | |
| 6607 | 12년 전 | 694 | ||
| 6606 | 12년 전 | 566 | ||
| 6605 | 12년 전 | 457 | ||
| 6604 | 12년 전 | 976 | ||
| 6603 |
geektoo
|
12년 전 | 703 | |
| 6602 | 12년 전 | 842 | ||
| 6601 | 12년 전 | 364 | ||
| 6600 | 12년 전 | 407 | ||
| 6599 | 12년 전 | 554 | ||
| 6598 | 12년 전 | 938 | ||
| 6597 | 12년 전 | 940 | ||
| 6596 | 12년 전 | 425 | ||
| 6595 | 12년 전 | 794 | ||
| 6594 | 12년 전 | 4576 | ||
| 6593 | 12년 전 | 2580 | ||
| 6592 | 12년 전 | 829 | ||
| 6591 | 12년 전 | 588 | ||
| 6590 |
|
12년 전 | 1397 | |
| 6589 | 12년 전 | 770 | ||
| 6588 |
GINUSSOFT
|
12년 전 | 5130 | |
| 6587 | 12년 전 | 6215 | ||
| 6586 | 12년 전 | 1049 | ||
| 6585 | 12년 전 | 807 | ||
| 6584 | 12년 전 | 468 | ||
| 6583 |
|
12년 전 | 985 | |
| 6582 | 12년 전 | 871 | ||
| 6581 | 12년 전 | 841 | ||
| 6580 | 12년 전 | 619 | ||
| 6579 |
알랑가몰라
|
12년 전 | 932 | |
| 6578 | 12년 전 | 1335 | ||
| 6577 | 12년 전 | 1503 | ||
| 6576 |
경dragon
|
12년 전 | 774 | |
| 6575 | 12년 전 | 1890 | ||
| 6574 | 12년 전 | 690 | ||
| 6573 | 12년 전 | 989 | ||
| 6572 |
|
12년 전 | 1669 | |
| 6571 |
CTOMAN
|
12년 전 | 1965 | |
| 6570 | 12년 전 | 1733 | ||
| 6569 | 12년 전 | 1894 | ||
| 6568 | 12년 전 | 2409 | ||
| 6567 | 12년 전 | 1033 | ||
| 6566 |
lainfox
|
12년 전 | 1536 | |
| 6565 | 12년 전 | 3657 | ||
| 6564 |
제주프라이스
|
12년 전 | 1542 | |
| 6563 | 12년 전 | 1576 | ||
| 6562 |
프로프리랜서
|
12년 전 | 1351 | |
| 6561 |
프로프리랜서
|
12년 전 | 971 | |
| 6560 |
프로프리랜서
|
12년 전 | 1225 | |
| 6559 |
프로프리랜서
|
12년 전 | 1143 | |
| 6558 |
프로프리랜서
|
12년 전 | 1360 | |
| 6557 |
프로프리랜서
|
12년 전 | 1984 | |
| 6556 |
프로프리랜서
|
12년 전 | 1531 | |
| 6555 |
프로프리랜서
|
12년 전 | 1372 | |
| 6554 |
프로프리랜서
|
12년 전 | 3895 | |
| 6553 |
프로프리랜서
|
12년 전 | 1516 | |
| 6552 | 12년 전 | 847 | ||
| 6551 |
왕초보sasa
|
12년 전 | 1543 | |
| 6550 |
왕초보sasa
|
12년 전 | 640 | |
| 6549 |
왕초보sasa
|
12년 전 | 913 | |
| 6548 | 12년 전 | 1302 | ||
| 6547 | 12년 전 | 1200 | ||
| 6546 | 12년 전 | 5162 | ||
| 6545 | 12년 전 | 2521 | ||
| 6544 |
AnnieK
|
12년 전 | 1785 | |
| 6543 |
베르무트7
|
12년 전 | 648 | |
| 6542 |
오늘도망했다
|
12년 전 | 2236 | |
| 6541 | 12년 전 | 819 | ||
| 6540 | 12년 전 | 1139 | ||
| 6539 | 12년 전 | 853 | ||
| 6538 |
senseme
|
12년 전 | 3380 | |
| 6537 | 12년 전 | 781 | ||
| 6536 | 12년 전 | 3670 | ||
| 6535 | 12년 전 | 1351 | ||
| 6534 | 12년 전 | 1624 | ||
| 6533 | 12년 전 | 2246 | ||
| 6532 |
냐옹이사범
|
12년 전 | 2308 | |
| 6531 | 12년 전 | 571 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기