@@ -103,6 +103,38 @@ class Query extends Component implements QueryInterface
103103 * @see source
104104 */
105105 public $ scriptFields ;
106+ /**
107+ * @var array An array of runtime fields evaluated at query time
108+ * Example:
109+ * ```php
110+ * $query->$runtimeMappings = [
111+ * 'value_times_two' => [
112+ * 'type' => 'double',
113+ * 'script' => "emit(doc['my_field_name'].value * 2)",
114+ * ],
115+ * 'value_times_factor' => [
116+ * 'type' => 'double',
117+ * 'script' => "emit(doc['my_field_name'].value * factor)",
118+ * 'params' => [
119+ * 'factor' => 2.0
120+ * ],
121+ * ],
122+ * ]
123+ * ```
124+ *
125+ * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/runtime-mapping-fields.html
126+ * @see runtimeMappings()
127+ * @see source
128+ */
129+ public $ runtimeMappings ;
130+ /**
131+ * @var array Use the fields parameter to retrieve the values of runtime fields. Runtime fields won’t display in
132+ * _source, but the fields API works for all fields, even those that were not sent as part of the original _source.
133+ * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/runtime-retrieving-fields.html
134+ * @see fields()
135+ * @see fields
136+ */
137+ public $ fields ;
106138 /**
107139 * @var array this option controls how the `_source` field is returned from
108140 * the documents. For example, `['id', 'name']` means that only the `id`
@@ -450,7 +482,7 @@ public function count($q = '*', $db = null)
450482 if ($ this ->emulateExecution ) {
451483 return 0 ;
452484 }
453-
485+
454486 $ command = $ this ->createCommand ($ db );
455487
456488 // performing a query with return size of 0, is equal to getting result stats such as count
@@ -732,6 +764,38 @@ public function scriptFields($fields)
732764 return $ this ;
733765 }
734766
767+ /**
768+ * Sets the runtime mappings for this query
769+ * @param $mappings
770+ * @return $this the query object itself
771+ * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/runtime.html
772+ */
773+ public function runtimeMappings ($ mappings )
774+ {
775+ if (is_array ($ mappings ) || $ mappings === null ) {
776+ $ this ->runtimeMappings = $ mappings ;
777+ } else {
778+ $ this ->runtimeMappings = func_get_args ();
779+ }
780+ return $ this ;
781+ }
782+
783+ /**
784+ * Sets the runtime fields to retrieve from the documents.
785+ * @param array $fields the fields to be selected.
786+ * @return $this the query object itself
787+ * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/runtime-retrieving-fields.html
788+ */
789+ public function fields ($ fields )
790+ {
791+ if (is_array ($ fields ) || $ fields === null ) {
792+ $ this ->fields = $ fields ;
793+ } else {
794+ $ this ->fields = func_get_args ();
795+ }
796+ return $ this ;
797+ }
798+
735799 /**
736800 * Sets the source filtering, specifying how the `_source` field of the
737801 * document should be returned.
0 commit comments