summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Elastic/Lookup/ProximityPropertyValueLookup.php
blob: ac5005ab672bdc823a8357b8c7c6d40cb8406d4f (plain)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<?php

namespace SMW\Elastic\Lookup;

use SMW\Elastic\Connection\Client as ElasticClient;
use SMW\Elastic\QueryEngine\FieldMapper;
use SMW\DataTypeRegistry;
use SMW\DataValueFactory;
use SMWDITime as DITime;
use SMWDataItem as DataItem;
use SMW\DIProperty;
use SMW\Store;
use SMW\RequestOptions;
use RuntimeException;

/**
 * Experimental implementation to showcase how a Elasticsearch specific implementation
 * for a property value lookup can be used and override the default SQL service.
 *
 * The class is targeted to be used for API (e.g. autocomplete etc.) intensive
 * services.
 *
 * @license GNU GPL v2+
 * @since 3.0
 *
 * @author mwjames
 */
class ProximityPropertyValueLookup {

	/**
	 * @var Store
	 */
	private $store;

	/**
	 * @since 3.0
	 *
	 * @param Store $store
	 */
	public function __construct( Store $store ) {
		$this->store = $store;
		$this->fieldMapper = new FieldMapper();
	}

	/**
	 * @since 3.0
	 *
	 * @param DIProperty $property
	 * @param string $value
	 * @param RequestOptions $opts
	 *
	 * @return array
	 */
	public function lookup( DIProperty $property, $value = '', RequestOptions $opts ) {

		$connection = $this->store->getConnection( 'elastic' );
		$continueOffset = 0;

		$pid = $this->fieldMapper->getPID(
			$this->store->getObjectIds()->getSMWPropertyID( $property )
		);

		$diType = DataTypeRegistry::getInstance()->getDataItemByType(
			$property->findPropertyTypeID()
		);

		$field = $this->fieldMapper->getField( $property );

		if ( $value === '' ) {
			// Just create a list of available values where the property exists
			$params = $this->fieldMapper->exists( "$pid.$field" );

			// Increase the range of the initial match since a property field
			// stores are all sorts of values, this is to make sure that the
			// aggregation has enough objects available to build a selection
			// list that satisfies the RequestOptions::getLimit
			$limit = 500;
		} elseif( $diType === DataItem::TYPE_TIME ) {
			$limit = 500;

			$dataValue = DataValueFactory::getInstance()->newDataValueByProperty(
				$property,
				$value
			);

			$params = $this->fieldMapper->bool(
				'must',
				$this->fieldMapper->range( "$pid.$field", $dataValue->getDataItem()->getJD(), SMW_CMP_GEQ )
			);
		} elseif( $diType === DataItem::TYPE_NUMBER ) {
			$limit = 500;

			if ( strpos( $value, '*' ) === false ) {
				$value = "*$value*";
			}

			$params = $this->fieldMapper->bool(
				'must',
				$this->fieldMapper->wildcard( "$pid.$field.keyword", $value )
			);
		} else {
			$limit = 500;

			if ( strpos( $value, '*' ) === false ) {
				$value = "$value*";
			}

			$params = $this->fieldMapper->bool(
				'must',
				$this->fieldMapper->match_phrase( "$pid.$field", $value )
			);
		}

		$body = [
			'_source' => [ "$pid.$field" ],
			'from'    => $opts->getOffset(),
			'size'    => $limit,
			'query'   => $params
		];

		$limit = $opts->getLimit() + 1;

		// Aggregation is used to filter a specific value aspect from a property
		// field contents
		if ( $value !== '' ) {
			// Setting size to 0 which avoids executing the fetch query of the search
			// hereby making the request more efficient.
			$body['size'] = 0;

			$body += $this->aggs_filter( $diType, $pid, $field, $limit, $property, trim( $value, '*' ) );
		}

		if ( $opts->sort ) {
			$body += [ 'sort' => [ "$pid.$field" => [ 'order' => $opts->sort ] ] ];
		}

		$params = [
			'index' => $connection->getIndexName( ElasticClient::TYPE_DATA ),
			'type'  => ElasticClient::TYPE_DATA,
			'body'  => $body
		];

		list( $res, $errors ) = $connection->search( $params );

		if ( isset( $res['aggregations'] ) ) {
			list( $list, $i ) = $this->match_aggregations( $res['aggregations'], $diType, $limit );
		} elseif ( isset( $res['hits'] ) ) {
			list( $list, $i ) = $this->match_hits( $res['hits'], $pid, $field, $limit );
		} else {
			$list = [];
			$i = 0;
		}

		if ( $list !== [] ) {
			$list = array_values( $list );

			if (  $diType === DataItem::TYPE_TIME ) {
				foreach ( $list as $key => $value ) {

					if ( strpos( $value, '/' ) !== false ) {
						$dataItem = DITime::doUnserialize( $value );
					} else {
						$dataItem = DITime::newFromJD( $value );
					}

					$list[$key] = DataValueFactory::getInstance()->newDataValueByItem( $dataItem, $property )->getWikiValue();
				}
			}
		}

		return $list;
	}

	private function aggs_filter( $diType, $pid, $field, $limit, $property, $value ) {

		// A field on ES to a property can can all different kind of values and
		// the request is only interested in those values that match a certain
		// prefix or affix hence use `include` to only return aggregated values
		// that contain the search term or value

		if ( $diType === DataItem::TYPE_TIME ) {

			$dataValue = DataValueFactory::getInstance()->newDataValueByProperty(
				$property,
				$value
			);

			return [
				'aggs' => [
					'value_terms' => [
						'terms' => [
							'field' => "$pid.dat_raw",
							'size'  => $limit,
							"order" => [ "_key" => "asc" ],
							'include' => $dataValue->getDataItem()->getSerialization() . ".*"
						]
					]
				]
			];
		}

		if ( $diType === DataItem::TYPE_NUMBER ) {
			return [
				'aggs' => [
					'value_terms' => [
						'terms' => [
							'field' => "$pid.$field.keyword",
							'size'  => $limit,
							"order" => [ "_key" => "asc" ],
							'include' => ".*" . $value . ".*"
						]
					]
				]
			];
		}

		return [
			'aggs' => [
				'value_terms' => [
					'terms' => [
						'field' => "$pid.$field.keyword",
						'size' => $limit,
						'include' =>
							".*" . $value . ".*|" .
							".*" . ucfirst( $value ) . ".*|" .
							".*" . mb_strtoupper( $value ) . ".*"
					]
				]
			]
		];
	}

	private function match_aggregations( $res, $diType, $limit ) {

		$isNumeric = $diType === DataItem::TYPE_NUMBER;
		$list = [];
		$i = 0;

		foreach ( $res as $aggs ) {
			foreach ( $aggs as $val ) {

				if ( !is_array( $val ) ) {
					continue;
				}

				foreach ( $val as $v ) {

					if ( $i >= $limit ) {
						break;
					}

					if ( isset( $v['key'] ) ) {
						$val = (string)$v['key'];

						// Aggregation happens on keyword field, numerics are of type
						// double hence is coerced as 5 -> 5.0
						if ( $isNumeric && substr( $val, -2 ) === '.0' ) {
							$val = substr( $val, 0, -2 );
						}

						$list[] = $val;
						$i++;
					}
				}
			}
		}

		return [ $list, $i ];
	}

	private function match_hits( $res, $pid, $field, $limit ) {

		$list = [];
		$i = 0;

		foreach ( $res as $key => $value ) {

			if ( $key !== 'hits' ) {
				continue;
			}

			foreach ( $value as $v ) {

				if ( !isset( $v['_source'][$pid][$field] ) ) {
					continue;
				}

				foreach ( $v['_source'][$pid][$field] as $match ) {

					if ( $i >= $limit ) {
						break;
					}

					// Filter duplicates
					$hash = md5( $match );

					if ( isset( $list[$hash] ) ) {
						continue;
					}

					$list[$hash] = (string)$match;
					$i++;
				}
			}
		}

		return [ $list, $i ];
	}

}