반응형

콘솔에서 입력하는 방식말고 PHP를 이용해 엘라스틱에 저장돼 있는 데이터를 불러와 보자

 

<?php

$json='
{
"query": { 
            "bool": { 
                "filter": [ 
                    { 
                        "terms":  { 
                                "site_name": "tistory"  
                                }
                    },
                    { 
                        "range": { 
                            "site_cnt": { "gt": "0" }
                            }
                    } 
                ],
                 "must": [
                      { "term": { "multi" : "free" }}
                 ],
                "must_not": [ 
                    { 
                        "range": { 
                            "reg_date": { "lt": "2020-10-01" } 
                        } 
                    } 
                ]
            }
        },
"size": 50,
"from": 1,
"sort": {"reg_date":"desc"} 
}
';

 

 

$url="localhost:9200/test/_search?pretty";

  $ch = curl_init();  
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json'
  ));
 
  $output = curl_exec($ch); // 데이터 요청 후 수신
  $output=json_decode($output);
  curl_close($ch);  // 리소스 해제

echo "<pre>";

print_r($output);

?>

 

이렇게 데이타를 만들어서 curl을 이용해 서버로 던져주면 output을 준다. 그걸 가공해서 화면에 뿌려주면 된다.

 

데이타에 대해 설명을 하자면...

 

site_name이 tisotry이고 site_cnt가 0보다 크고 multi가 free이며 reg_date가 2020-10-01보다 작은건 제외한다 이다.

 

그리고 현재화면 출력갯수는 50개, 페이지는 1페이지, 정렬은 reg_date를 기준으로 큰것을 우선 보여준다.

 

이렇게 하면 된다. 쉽다.

반응형

+ Recent posts