<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  <channel>
    <title><![CDATA[PHP - Elasticsearch Labs]]></title>
    <description><![CDATA[Articles and tutorials from the Search team at Elastic]]></description>
    <copyright><![CDATA[© 2026. Elasticsearch B.V. All Rights Reserved]]></copyright>
    <image>
      <title><![CDATA[PHP - Elasticsearch Labs]]></title>
      <url>https://static-www.elastic.co/v3/assets/bltefdd0b53724fa2ce/blt1121c0bf0e8a6e65/6a88da6340a1841030ef456f/search-labs-thumbnail.png</url>
      <link>https://www.elastic.co/kr/search-labs/blog/category/php-programming</link>
    </image>
    <link>https://www.elastic.co/kr/search-labs/blog/category/php-programming</link>
    <atom:link href="https://www.elastic.co/kr/search-labs/rss/category/php-programming.xml" rel="self" type="application/rss+xml"/>
    <language><![CDATA[kr]]></language>
    <lastBuildDate>Mon, 28 Sep 2026 03:04:59 GMT</lastBuildDate>
  <item>
    <title><![CDATA[ES|QL에서 PHP 객체로]]></title>
    <description><![CDATA[PHP에서 ES|QL 쿼리를 실행하고 관리하는 방법을 알아보세요. 이 가이드에 따라 ES|QL 결과를 PHP 객체 또는 사용자 정의 클래스에 매핑하세요.]]></description>
    <content:encoded><![CDATA[<p>elasticsearch-php <a href="https://github.com/elastic/elasticsearch-php/releases/tag/v8.13.0">v8.13.0부터는</a> <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/esql.html">ES|QL</a> 쿼리를 실행하고 그 결과를 <a href="https://www.php.net/manual/en/class.stdclass.php">stdClass</a> 또는 사용자 정의 클래스의 PHP 객체에 매핑할 수 있습니다.</p><h2>ES|QL</h2><p><a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/esql.html">ES|QL은</a> Elasticsearch 8.11.0에 도입된 새로운 Elasticsearch 쿼리 언어입니다. 현재는 기술 미리보기 버전으로 제공됩니다. Elasticsearch에 저장된 데이터를 필터링, 변환, 분석할 수 있는 강력한 방법을 제공합니다.</p><p>"파이프" (<code>|</code>)를 사용하여 단계별 방식으로 데이터를 조작하고 변환합니다. 이 접근 방식을 통해 사용자는 한 작업의 출력이 다음 작업의 입력이 되는 일련의 작업을 구성하여 복잡한 데이터 변환 및 분석을 수행할 수 있습니다.</p><p>예를 들어 다음 쿼리는 <code>sample_data</code> 인덱스의 처음 3개 문서(행)를 반환합니다:</p>FROM sample_data
| LIMIT 3
<img src="https://static-www.elastic.co/v3/assets/bltefdd0b53724fa2ce/blt8b0310cb335872b7/6a17d7eab1e113258679f0df/c23aee777bacdf90c63b717fd9458207dfc0511d-864x284.png" alt="ES|QL은 테이블을 생성합니다." /><h2>사용 사례: 공식 PHP 클라이언트의 ES|QL 기능</h2><p>공식 PHP 클라이언트에서 개발된 ES|QL 기능을 설명하기 위해 다음 정보를 포함한 81,828권의 책(54.4MB)으로 구성된 <a href="https://github.com/elastic/elasticsearch-php-examples/blob/main/examples/ESQL/data/books.csv">CSV 파일을</a> Elasticsearch에 저장했습니다:</p>Title;Descrition;Author;Year;Publisher;Ratings
<p>이 목록은 공개적으로 사용 가능한 <a href="https://www.kaggle.com/datasets/mohamedbakhet/amazon-books-reviews">Amazon 서평 데이터 세트에서</a> 추출했습니다.</p><p>다음 Elasticsearch 매핑을 사용하여 <code>books</code> 인덱스를 생성했습니다:</p>'mappings' : {
    'properties': {
        'title': {
            'type': 'text'
        },
        'description': {
            'type': 'text'
        },
        'author': {
            'type': 'text'
        },
        'year': {
            'type': 'short'
        },
        'publisher': {
            'type': 'keyword'
        },
        'rating': {
            'type': 'half_float'
        }
    }
}
<p><code>rating</code> 값은 2.9GB의 <a href="https://www.kaggle.com/datasets/mohamedbakhet/amazon-books-reviews?select=Books_rating.csv">Books_rating.csv</a> 파일에서 가져온 순위 리뷰의 평균입니다.</p><p><a href="https://github.com/elastic/elasticsearch-php-examples/blob/main/examples/ESQL/bulk.php">여기에서</a> Elasticsearch에서 모든 도서를 일괄 가져오기 위해 사용한 PHP 스크립트를 찾을 수 있습니다. 대량 작업은 PHP 8.2.17을 사용하여 7초와 28MB RAM이 소요되었습니다. 제안된 매핑을 사용하면 Elasticsearch의 인덱스 크기는 약 62MB입니다.</p><h2>ES|QL 결과를 PHP 객체 또는 사용자 정의 클래스에 매핑하기</h2><p><code>esql()-&gt;query()</code> 엔드포인트를 사용하여 PHP에서 ES|QL 쿼리를 실행할 수 있습니다. 이 쿼리의 결과는 테이블 데이터 구조입니다. 이는 <code>columns</code> 및 <code>values</code> 필드를 사용하여 JSON으로 표현됩니다. <code>columns</code> 필드에는 <code>name</code> 및 <code>type</code> 정의가 있습니다.</p><p>다음은 사용자 순위 리뷰에 따라 스티븐 킹이 쓴 상위 10권의 책을 검색하는 ES|QL 쿼리의 예입니다:</p>$query = &lt;&lt;&lt;EOD
    FROM books
    | WHERE author == "Stephen King"
    | SORT rating DESC
    | LIMIT 10
EOD;

$result = $client-&gt;esql()-&gt;query([
    'body' =&gt; ['query' =&gt; $query]
]);
<p>Elasticsearch의 JSON 결과는 다음과 같습니다:</p>{
    "columns": [
        { "name": "author", "type": "text" },
        { "name": "description", "type": "text" },
        { "name": "publisher", "type": "keyword" },
        { "name": "rating", "type": "double" },
        { "name": "title", "type": "text" },
        { "name": "year", "type": "integer" }
    ],
    "values": [
        [
            "Stephen King",
            "The author ...",
            "Turtleback",
            5.0,
            "How writers write",
            2002
        ],
        [
            "Stephen King",
            "In Blockade Billy, a retired coach...",
            "Simon and Schuster",
            5.0,
            "Blockade",
            2010
        ],
        [
            "Stephen King",
            "A chilling collection of twenty horror stories.",
            "Signet Book",
            4.55859375,
            "Night Shift (Signet)",
            1979
        ],
        ...
    ]
}
<p>이 예에서는 책과 관련된 6개의 속성(저자, 설명, 출판사, 등급, 제목, 연도)과 10개의 결과(모두 스티븐 킹의 책)가 있습니다.</p><p>ES|QL에서 지원되는 모든 유형 목록은 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/esql-limitations.html#esql-supported-types">여기에</a> 나와 있습니다.</p><p><code>$result</code> 응답 개체는 배열, 문자열 또는 객체로 액세스할 수 있습니다(자세한 내용은 <a href="https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/connecting.html#client-usage">여기를</a> 참조하세요).</p><p>객체 인터페이스를 사용하면 속성 및 인덱스를 사용하여 값에 액세스할 수 있습니다. 예를 들어 <code>$result-&gt;values[0][4]</code> 은 목록에서 첫 번째 책(0)의 제목(4)을 반환하고 <code>$result-&gt;values[1][3]</code> 은 두 번째 책(1)의 순위 점수(3) 등을 반환합니다. PHP에서 배열의 인덱스는 0부터 시작한다는 점을 기억하세요.</p><p>이 인터페이스는 일부 사용 사례에는 충분할 수 있지만 대부분의 경우 결과물로 여러 개의 객체를 원합니다.</p><p>결과를 객체 배열로 매핑하려면 elasticsearch-php의 새로운 <a href="https://github.com/elastic/elasticsearch-php/issues/1398">mapTo()</a> 기능을 사용할 수 있습니다.</p><p>이 기능은 <a href="https://github.com/elastic/elasticsearch-php/blob/main/src/Response/Elasticsearch.php">Elasticsearch 응답 개체에서</a> 직접 사용할 수 있습니다. 즉, 다음과 같이 액세스할 수 있습니다:</p>$books = $result-&gt;mapTo(); // Array of stdClass
foreach ($books as $book) {
    printf(
        "%s, %s, %d, Rating: %.2f\n",
        $book-&gt;author,
        $book-&gt;title,
        $book-&gt;year,
        $book-&gt;rating
    );
}
<p>사용자 지정 Book 클래스가 있는 경우 다음과 같이 이를 사용하여 결과를 매핑할 수 있습니다:</p>class Book
{
    public string $author;
    public string $title;
    public string $description;
    public int $year;
    public float $rating;
}

$books = $result-&gt;mapTo(Book::class); // Array of Book
<p>클래스에 ES|QL 결과에 포함된 속성 외에 다른 속성이 있는 경우 이 방법도 작동합니다. <code>mapTo()</code> 함수는 ES|QL 결과의 열로 반환된 속성만 사용합니다.</p><p>이 문서에 보고된 모든 예제는 <a href="https://github.com/elastic/elasticsearch-php-examples/tree/main/examples/ESQL">여기에서</a> 다운로드할 수 있습니다.</p>]]></content:encoded>
    <link>https://www.elastic.co/search-labs/blog/esql-php-map-object-class</link>
    <guid isPermaLink="true">https://www.elastic.co/search-labs/blog/esql-php-map-object-class</guid>
    <category><![CDATA[ES|QL]]></category>
    <category><![CDATA[PHP]]></category>
    <dc:creator><![CDATA[Enrico Zimuel]]></dc:creator>
    <enclosure url="https://static-www.elastic.co/v3/assets/bltefdd0b53724fa2ce/blt99f5ab85c0713977/6a17d7ebfbc5f8072b491918/aea56270f48cb64130d1b515b983434e0960dc2f-500x500.png" length="0" type="image/png"/>
    <pubDate>Mon, 08 Apr 2024 00:00:00 GMT</pubDate>
  </item>
  </channel>
</rss>