<?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/jp/search-labs/blog/category/php-programming</link>
    </image>
    <link>https://www.elastic.co/jp/search-labs/blog/category/php-programming</link>
    <atom:link href="https://www.elastic.co/jp/search-labs/rss/category/php-programming.xml" rel="self" type="application/rss+xml"/>
    <language><![CDATA[jp]]></language>
    <lastBuildDate>Tue, 22 Sep 2026 10:50:53 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> ）を使用して、データを段階的に操作および変換します。このアプローチにより、ユーザーは一連の操作を構成でき、1 つの操作の出力が次の操作の入力となり、複雑なデータ変換と分析が可能になります。</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.4 MB) の<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 Books Reviews データセット</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.9 GB の<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 秒と 28 MB の RAM が必要でした。提案されたマッピングでは、Elasticsearch のインデックス サイズは約 62 MB になります。</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>以下は、ユーザーのランキングレビュー順に並べられた、Stephen King が書いたトップ 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 件の結果 (すべて Stephen King の書籍) があります。</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> 2 番目の本 (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>