|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Elasticsearch PHP Client |
| 4 | + * |
| 5 | + * @link https://github.com/elastic/elasticsearch-php |
| 6 | + * @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co) |
| 7 | + * @license https://opensource.org/licenses/MIT MIT License |
| 8 | + * |
| 9 | + * Licensed to Elasticsearch B.V under one or more agreements. |
| 10 | + * Elasticsearch B.V licenses this file to you under the MIT License. |
| 11 | + * See the LICENSE file in the project root for more information. |
| 12 | + */ |
| 13 | +declare(strict_types = 1); |
| 14 | + |
| 15 | +namespace Elastic\Elasticsearch\Tests\Helper\Esql; |
| 16 | + |
| 17 | +use Elastic\Elasticsearch\Helper\Esql\Query; |
| 18 | +use Elastic\Elasticsearch\Helper\Esql\Expression; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | + |
| 21 | +class EsqlTest extends TestCase |
| 22 | +{ |
| 23 | + public function testFrom(): void |
| 24 | + { |
| 25 | + $query = Query::from("employees"); |
| 26 | + $this->assertEquals( |
| 27 | + "FROM employees\n", |
| 28 | + $query->__toString() |
| 29 | + ); |
| 30 | + |
| 31 | + $query = Query::from("<logs-{now/d}>"); |
| 32 | + $this->assertEquals( |
| 33 | + "FROM <logs-{now/d}>\n", |
| 34 | + $query->__toString() |
| 35 | + ); |
| 36 | + |
| 37 | + $query = Query::from("employees-00001", "other-employees-*"); |
| 38 | + $this->assertEquals( |
| 39 | + "FROM employees-00001, other-employees-*\n", |
| 40 | + $query->__toString() |
| 41 | + ); |
| 42 | + |
| 43 | + $query = Query::from("cluster_one:employees-00001", "cluster_two:other-employees-*"); |
| 44 | + $this->assertEquals( |
| 45 | + "FROM cluster_one:employees-00001, cluster_two:other-employees-*\n", |
| 46 | + $query->__toString() |
| 47 | + ); |
| 48 | + |
| 49 | + $query = Query::from("employees")->metadata("_id"); |
| 50 | + $this->assertEquals( |
| 51 | + "FROM employees METADATA _id\n", |
| 52 | + $query->__toString() |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + public function testRow(): void |
| 57 | + { |
| 58 | + $query = Query::row(a: 1, b: "two", c: null); |
| 59 | + $this->assertEquals( |
| 60 | + "ROW a = 1, b = \"two\", c = null\n", |
| 61 | + $query->__toString() |
| 62 | + ); |
| 63 | + |
| 64 | + $query = Query::row(a: [2, 1]); |
| 65 | + $this->assertEquals( |
| 66 | + "ROW a = [2,1]\n", |
| 67 | + $query->__toString() |
| 68 | + ); |
| 69 | + |
| 70 | + // $query = Query::row(a: Expression("ROUND(1.23, 0)")); |
| 71 | + // $this->assertEquals( |
| 72 | + // "ROW a = ROUND(1.23, 0)\n", |
| 73 | + // $query->__toString() |
| 74 | + // ); |
| 75 | + } |
| 76 | + |
| 77 | + public function testShow(): void |
| 78 | + { |
| 79 | + $query = Query::show("INFO"); |
| 80 | + $this->assertEquals( |
| 81 | + "SHOW INFO\n", |
| 82 | + $query->__toString() |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + public function testChangePoint(): void |
| 87 | + { |
| 88 | + $query = Query::row(key: range(1, 25)) |
| 89 | + ->mv_expand("key") |
| 90 | + ->eval(value: "CASE(key < 13, 0, 42)") |
| 91 | + ->change_point("value") |
| 92 | + ->on("key") |
| 93 | + ->where("type IS NOT NULL"); |
| 94 | + $this->assertEquals(<<<ESQL |
| 95 | + ROW key = [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] |
| 96 | + | MV_EXPAND key |
| 97 | + | EVAL value = CASE(key < 13, 0, 42) |
| 98 | + | CHANGE_POINT value ON key |
| 99 | + | WHERE type IS NOT NULL\n |
| 100 | + ESQL, |
| 101 | + $query->__toString() |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + public function testCompletion(): void |
| 106 | + { |
| 107 | + $query = Query::row(question: "What is Elasticsearch?") |
| 108 | + ->completion("question")->with("test_completion_model") |
| 109 | + ->keep("question", "completion"); |
| 110 | + $this->assertEquals(<<<ESQL |
| 111 | + ROW question = "What is Elasticsearch?" |
| 112 | + | COMPLETION question WITH {"inference_id":"test_completion_model"} |
| 113 | + | KEEP question, completion\n |
| 114 | + ESQL, |
| 115 | + $query->__toString() |
| 116 | + ); |
| 117 | + |
| 118 | + $query = Query::row(question: "What is Elasticsearch?") |
| 119 | + ->completion(answer: "question")->with("test_completion_model") |
| 120 | + ->keep("question", "answer"); |
| 121 | + $this->assertEquals(<<<ESQL |
| 122 | + ROW question = "What is Elasticsearch?" |
| 123 | + | COMPLETION answer = question WITH {"inference_id":"test_completion_model"} |
| 124 | + | KEEP question, answer\n |
| 125 | + ESQL, |
| 126 | + $query->__toString() |
| 127 | + ); |
| 128 | + |
| 129 | + $query = Query::from("movies") |
| 130 | + ->sort("rating DESC") |
| 131 | + ->limit(10) |
| 132 | + ->eval(prompt: "CONCAT(\n" . |
| 133 | + " \"Summarize this movie using the following information: \\n\",\n" . |
| 134 | + " \"Title: \", title, \"\\n\",\n" . |
| 135 | + " \"Synopsis: \", synopsis, \"\\n\",\n" . |
| 136 | + " \"Actors: \", MV_CONCAT(actors, \", \"), \"\\n\",\n" . |
| 137 | + " )" |
| 138 | + ) |
| 139 | + ->completion(summary: "prompt")->with("test_completion_model") |
| 140 | + ->keep("title", "summary", "rating"); |
| 141 | + $this->assertEquals(<<<ESQL |
| 142 | + FROM movies |
| 143 | + | SORT rating DESC |
| 144 | + | LIMIT 10 |
| 145 | + | EVAL prompt = CONCAT( |
| 146 | + "Summarize this movie using the following information: \\n", |
| 147 | + "Title: ", title, "\\n", |
| 148 | + "Synopsis: ", synopsis, "\\n", |
| 149 | + "Actors: ", MV_CONCAT(actors, ", "), "\\n", |
| 150 | + ) |
| 151 | + | COMPLETION summary = prompt WITH {"inference_id":"test_completion_model"} |
| 152 | + | KEEP title, summary, rating\n |
| 153 | + ESQL, |
| 154 | + $query->__toString() |
| 155 | + ); |
| 156 | + } |
| 157 | +} |
| 158 | + |
0 commit comments