diff options
Diffstat (limited to 'www/wiki/extensions/SemanticMediaWiki/src/Elastic/QueryEngine/SearchResult.php')
-rw-r--r-- | www/wiki/extensions/SemanticMediaWiki/src/Elastic/QueryEngine/SearchResult.php | 219 |
1 files changed, 219 insertions, 0 deletions
diff --git a/www/wiki/extensions/SemanticMediaWiki/src/Elastic/QueryEngine/SearchResult.php b/www/wiki/extensions/SemanticMediaWiki/src/Elastic/QueryEngine/SearchResult.php new file mode 100644 index 00000000..a93142fa --- /dev/null +++ b/www/wiki/extensions/SemanticMediaWiki/src/Elastic/QueryEngine/SearchResult.php @@ -0,0 +1,219 @@ +<?php + +namespace SMW\Elastic\QueryEngine; + +use InvalidArgumentException; + +/** + * @license GNU GPL v2+ + * @since 3.0 + * + * @author mwjames + */ +class SearchResult { + + /** + * @var [] + */ + private $raw = []; + + /** + * @var [] + */ + private $errors = []; + + /** + * @var []|null + */ + private $results; + + /** + * @var string + */ + private $filterField = '_id'; + + /** + * @var [] + */ + private $container = [ + 'info' => [], + 'scores' => [], + 'excerpts' => [], + 'count' => 0, + 'continue' => false + ]; + + /** + * @since 3.0 + * + * @param array $raw + */ + public function __construct( array $raw = [] ) { + $this->raw = $raw; + } + + /** + * @since 3.0 + * + * @param array $errors + */ + public function setErrors( array $errors ) { + $this->errors = $errors; + } + + /** + * @since 3.0 + * + * @param string $filterField + */ + public function setFilterField( $filterField ) { + $this->filterField = $filterField; + } + + /** + * @since 3.0 + * + * @return array + */ + public function getErrors() { + return $this->errors; + } + + /** + * @since 3.0 + * + * @param integer|null $cutoff + * + * @return array + */ + public function getResults( $cutoff = null ) { + + if ( $this->results === null ) { + $this->filter_results( $this->raw, $cutoff ); + } + + return $this->results; + } + + /** + * @since 3.0 + * + * @return array + */ + public function get( $key ) { + + if ( isset( $this->container[$key] ) ) { + return $this->container[$key]; + } + + throw new InvalidArgumentException( "`$key` is an unkown key, or is not registered." ); + } + + /** + * @since 3.0 + * + * @param integer $cutoff + * + * @return [] + */ + public function filter_results( array $results, $cutoff = null ) { + + $this->results = []; + + $this->container = [ + 'info' => [], + 'scores' => [], + 'excerpts' => [], + 'count' => 0, + 'continue' => false + ]; + + if ( $results === [] ) { + return []; + } + + $info = $results; + $res = $this->filter_field( $results, $cutoff ); + + unset( $info['hits'] ); + unset( $info['_shards'] ); + + $this->results = array_keys( $res ); + $info['max_score'] = $results['hits']['max_score']; + $info['total'] = count( $res ); + + $this->container['info'] = $info; + $this->container['count'] = $info['total']; + + return $this->results; + } + + /** + * @see https://www.elastic.co/guide/en/elasticsearch/client/php-api/6.0/_search_operations.html + */ + private function filter_field( $results, $cutoff ) { + + $res = []; + $continue = false; + + $scores = []; + $excerpts = []; + $i = 0; + + $field = $this->filterField; + $pid = null; + + if ( strpos( $field, '.' ) !== false ) { + list( $pid, $field ) = explode( '.', $field ); + } + + foreach ( $results as $key => $value ) { + + if ( !isset( $value['hits'] ) ) { + continue; + } + + foreach ( $value['hits'] as $k => $v ) { + + if ( $cutoff !== null && $i >= $cutoff ) { + $continue = true; + break; + } + + $ids = []; + + if ( $pid !== null && isset( $v['_source'][$pid][$field] ) ) { + $ids = $v['_source'][$pid][$field]; + } elseif ( isset( $v['_source'][$field] ) ) { + $ids = $v['_source'][$field]; + } elseif ( isset( $v[$field] ) ) { + $ids = $v[$field]; + } + + $ids = (array)$ids; + + foreach ( $ids as $id ) { + $res[$id] = true; + + if ( isset( $v['_score'] ) ) { + $scores[$id] = $v['_score']; + } + + if ( isset( $v['highlight'] ) ) { + $excerpts[$id] = $v['highlight']; + } + + $i++; + } + } + } + + $this->container['scores'] = $scores; + $this->container['count'] = 0; + $this->container['excerpts'] = $excerpts; + $this->container['continue'] = $continue; + + return $res; + } + +} |