juso.sir.co.kr 도로명 주소 검색 시스템 구축 가이드 - 주소검색
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 를 참고하시면 됩니다.
게시글 목록
| 번호 | 제목 |
|---|---|
| 27980 | |
| 7164 | |
| 31729 | |
| 31726 | |
| 31725 | |
| 31720 | |
| 31711 | |
| 7159 | |
| 27974 | |
| 19734 | |
| 19730 | |
| 19729 |
PHP
[알고리즘] 배열 역순
|
| 27969 | |
| 7142 | |
| 19728 |
jQuery
remove()를 이용하여 엘리먼트 삭제하기
|
| 19725 | |
| 7130 | |
| 19722 |
jQuery
동일 엘리먼트 순번 알아내기
2
|
| 19719 | |
| 7124 | |
| 19718 | |
| 19717 | |
| 19716 |
jQuery
position() 메서드 객체에 대한 좌표값
|
| 19715 | |
| 7122 | |
| 30959 | |
| 19714 | |
| 31710 |
jQuery Mobile
jQuery Mobile 강좌 19. Select Menus
|
| 31709 |
jQuery Mobile
jQuery Mobile 강좌 18. Text Inputs
|
| 19713 | |
| 7117 | |
| 19712 | |
| 7111 | |
| 31708 |
jQuery Mobile
jQuery Mobile 강좌 17. Form Basic
|
| 31707 |
jQuery Mobile
jQuery Mobile 강좌 16. Filters
|
| 19710 | |
| 31706 |
jQuery Mobile
jQuery Mobile 강좌 15. List Content
|
| 31705 |
jQuery Mobile
jQuery Mobile 강좌 14. List Views
|
| 31704 |
jQuery Mobile
jQuery Mobile 강좌 13. Layout Grids
|
| 19709 | |
| 19707 | |
| 31703 |
jQuery Mobile
jQuery Mobile 강좌 12. Tables
|
| 19706 |
jQuery
class가 있는지 없는지 체크하기
|
| 31702 |
jQuery Mobile
jQuery Mobile 강좌 11. Collapsibles
|
| 19705 |
PHP
배열에서 중복 값 없애기
|
| 31701 |
jQuery Mobile
jQuery Mobile 강좌 10. Panels
|
| 19704 | |
| 31700 |
jQuery Mobile
jQuery Mobile 강좌 9. Navigation Bars
|
| 31699 |
jQuery Mobile
jQuery Mobile 강좌 8. Toolbars
|
| 31698 |
jQuery Mobile
jQuery Mobile 강좌 7. Popups
|
| 7107 | |
| 19703 |
JavaScript
str_pad 자릿수만큼 특정문자로 채우기
|
| 31697 |
jQuery Mobile
jQuery Mobile 강좌 6. Icons
|
| 31696 |
jQuery Mobile
jQuery Mobile 강좌 5. Buttons
|
| 19702 | |
| 19701 | |
| 31695 |
jQuery Mobile
jQuery Mobile 강좌 4. Transitions
|
| 27965 | |
| 31694 |
jQuery Mobile
jQuery Mobile 강좌 3. Pages
|
| 19700 | |
| 31693 |
jQuery Mobile
jQuery Mobile 강좌 2. Install
|
| 19699 | |
| 31692 |
jQuery Mobile
jQuery Mobile 강좌 1. Introduction
|
| 31691 |
jQuery Mobile
jQuery Mobile 강좌 0.home
|
| 19698 | |
| 19697 | |
| 19696 |
jQuery
마우스 오버 위치에 따라 툴팁 생성 및 자동 이동
|
| 19694 | |
| 19693 |
jQuery
제이쿼리 동적으로 생성된 객체에 이벤트 생성하기
|
| 19692 | |
| 19691 | |
| 19690 |
JavaScript
shuffle 배열섞기
|
| 19689 |
jQuery
제이쿼리 모음 사이트
|
| 19688 |
PHP
1원팁] IP 대역 비교
|
| 19687 | |
| 7105 | |
| 30955 | |
| 7100 | |
| 19681 |
PHP
input 쉽게 관리하기
5
|
| 7097 | |
| 19678 |
JavaScript
서버시간을 사용한 전자시계
2
|
| 7089 | |
| 7086 | |
| 7084 | |
| 7082 | |
| 19677 | |
| 30953 | |
| 7080 | |
| 7077 | |
| 7071 | |
| 7070 | |
| 7066 | |
| 19676 |
JavaScript
구글 웹사이트 번역기를 내 사이트에 달기
|
| 19674 | |
| 27961 | |
| 7063 | |
| 7061 | |
| 19669 | |
| 7060 | |
| 20842 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기