아래의 이미지와 같이 검색화면을 구성해 보았다.
날짜와 키워드로 검색을 하고, 검색 옵션으로는 최신순-정확도순을 넣었다.
검색을 하면 날짜, id, 타이틀이 화면에 뿌려지고
20개씩 페이징 처리 하였다.
검색어를 넣으면 상위 키워드가 검색리스트 아래 뿌려진다.
search.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | <?php require 'vendor/autoload.php'; require 'function.php'; // selectbox 생성함수, 페이징처리 함수 // 인스턴트 생성, Elasticsearch 가 9200 가 아닌 다른 포트를 사용할 경우 포트를 별도로 지정해야 한다. $hosts = [ 'localhost:9201' ]; use Elasticsearch\ClientBuilder; $client = ClientBuilder::create() // Instantiate a new ClientBuilder ->setHosts($hosts) // Set the hosts ->build(); $q = $_POST['q']; $page = $_POST['page']; $fromdate = $_POST['fromdate']; $todate = $_POST['todate']; $sort = $_POST['sort']; $index = $_POST['index']; $type = $_POST['type']; // 키워드 카운트를 뿌릴때 제외하고 싶은 단어 $arrayExcept = array('amp', 'nbsp', 'url'); if(!$fromdate) { $fromdate = date("Y-m-d"); $todate = date("Y-m-d"); } // 검색옵션 $arraySort = array("최신순","정확도순"); $comboSort = select_box('sort', 1, $arraySort, $disabled='', $sort); ?> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Text ElasticSearch</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"> <script src="//code.jquery.com/jquery-1.10.2.js"></script> <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script> <style type="text/css"> body { font-family:'Malgun Gothic'; font-size:10pt; } </style> <script> $(function() { $( "#fromdate" ).datepicker({ dateFormat: "yy-mm-dd" }); $( "#todate" ).datepicker({ dateFormat: "yy-mm-dd" }); }); </script> <script> function goPage(page) { document.forms[0].page.value = page; document.forms[0].submit(); } </script> </head> <body> <form method="post" action="<?php echo $_SERVER['PHP_SELF']?>"> <input type="text" name="q" value="<?=$_POST['q']?>"> <input type="text" name="fromdate" id="fromdate" value="<?=$fromdate?>"> <input type="text" name="todate" id="todate" value="<?=$todate?>"> <input type="hidden" name="page" value="<?=$page?>"> <input type="hidden" name="index" value="index-name"> <input type="hidden" name="type" value="type-name"> <?=$comboSort?> <input type="submit" value="검색"> </form> <?php /****************** 페이지 출력 조건 **************/ $num_per_page = 20; // 페이지당 출력갯수 $page_per_block = 10; // 블럭당 페이지 갯수 if (!$page) $page = 1; //특별히 지정하지 않으면 첫페이지 출력 $first = ($page - 1) * $num_per_page; // 시작레코드 /****************** 페이지 출력 조건 **************/ $fromstamp = mktime(0, 0, 0, substr($fromdate, 5, 2), substr($fromdate, 8, 2), substr($fromdate, 0, 4)); // 2015-05-01 $tostamp = mktime(23, 59, 59, substr($todate, 5, 2), substr($todate, 8, 2), substr($todate, 0, 4)); if($q) { $params = [ 'index' => $index, 'type' => $type, 'from' => $first, 'size' => $num_per_page, 'body' => [ 'query' => [ 'bool' => [ 'must' => [ [ 'multi_match' => [ 'fields' => [ "title" ], 'query' => $q, 'operator' => "and" ] ], [ 'range' => [ 'timestamp' => [ 'gte' => $fromstamp, 'lte' => $tostamp ] ] ], ] ] ], 'aggs' => [ 'title' => [ 'terms' => [ 'field' => "title", 'size' => 200 ] ] ], ] ]; }else { $params = [ 'index' => $index, 'type' => $type, 'from' => $first, 'size' => $num_per_page, 'body' => [ 'query' => [ 'range' => [ 'timestamp' => [ 'gte' => $fromstamp, 'lte' => $tostamp ] ] ], 'aggs' => [ 'significantCrimeTypes' => [ 'significant_terms' => [ 'field' => "title", 'size' => 70 ] ] ], ] ]; } if($sort != 1) { $params['sort'] = array('timestamp:desc'); } $results = $client->search($params); // echo "<pre>"; // var_dump($results); // echo "</pre>"; // exit; $all_total = $results['hits']['total']; echo "총갯수: ".$all_total."개<p>"; $arrayData = $results['hits']['hits']; $cntDarta = count($arrayData); for($i=0; $i<$cntDarta; $i++) { echo $arrayData[$i]['_score']." "; echo $arrayData[$i]['_source']['datetime']." "; echo $arrayData[$i]['_id']." "; echo "<a href='".$arrayData[$i]['_source']['link']."' target='_blank'>".mb_substr ($arrayData[$i]['_source']['title'], 0, 200)."</a><br />"; } if($all_total > 0) { echo "<p>"; $url = $PHP_SELF; echo pagegen($page,$all_total,$num_per_page,$url,$page_per_block); } echo "<p>"; echo "<table border=1 cellspacing=0 cellpadding=3> <tr> <td>Keyword</td> <td>doc_count</td> <td>score</td> </tr>"; //$arrayBuckets = $results['aggregations']['significantCrimeTypes']['buckets']; $arrayBuckets = $results['aggregations']['title']['buckets']; if($arrayBuckets) { foreach($arrayBuckets as $nkey=>$arrayTerm) { if(mb_strlen($arrayTerm['key']) > 1) { $exceptCheck = array_search($arrayTerm['key'], $arrayExcept); if($exceptCheck === false) { // echo "[key] => ".$arrayTerm['key']."<br>"; // echo "[doc_count] => ".$arrayTerm['doc_count']."<br>"; // echo "[score] => ".$arrayTerm['score']."<p>"; echo " <tr> <td>".$arrayTerm['key']."</td> <td>".$arrayTerm['doc_count']."</td> <td>".$arrayTerm['score']."</td> </tr>"; } } } } echo "</table>"; ?> </body> </html> | cs |
function.php
0.0MB
'Elasticsearch6' 카테고리의 다른 글
elasticsearch 6.* 기본기-2 Index (0) | 2018.10.22 |
---|---|
elasticsearch 6.* 기본기-1 Search (0) | 2018.10.19 |
elasticsearch 6.* elasticsearch-php 를 이용한 검색엔진 개발 - 3 (bulk insert) (0) | 2018.10.17 |
elasticsearch 6.* elasticsearch-php 를 이용한 검색엔진 개발 - 2 (php 설정) (0) | 2018.10.16 |
elasticsearch 6.* elasticsearch-php 를 이용한 검색엔진 개발 - 1 (환경구성) (0) | 2018.09.19 |