diff --git a/readme.md b/readme.md index 00c3d4c..1964429 100644 --- a/readme.md +++ b/readme.md @@ -146,6 +146,14 @@ return array( 'default_index' => 'my_custom_index_name', + /* + |-------------------------------------------------------------------------- + | Should We Replace The Source With Highlight + |-------------------------------------------------------------------------- + | + */ + 'highlight_in_source' => true, + ); ``` @@ -338,7 +346,7 @@ The first method is a simple term search that searches all fields. The second is a query based search for more complex searching needs: ```php - public static function searchByQuery($query = null, $aggregations = null, $sourceFields = null, $limit = null, $offset = null, $sort = null) + public static function searchByQuery($query = null, $aggregations = null, $sourceFields = null, $limit = null, $offset = null, $sort = null, $highlight = null) ``` **Example:** @@ -354,6 +362,7 @@ Here's the list of available parameters: - `limit` - Number of records to return - `offset` - Sets the record offset (use for paging results) - `sort` - Your sort query +- `highlight` - Your highlight config ### Raw queries diff --git a/src/ElasticquentConfigTrait.php b/src/ElasticquentConfigTrait.php index 80f8e3f..8f7456a 100644 --- a/src/ElasticquentConfigTrait.php +++ b/src/ElasticquentConfigTrait.php @@ -4,6 +4,21 @@ trait ElasticquentConfigTrait { + /** + * Should the builder highlight the result + * + * @return array|bool + */ + public function getConfigIsHighlightTheSource() { + $index_name = $this->getElasticConfig('highlight_in_source'); + + if (!empty($index_name)) { + return $index_name; + } + + return false; + } + /** * Get Index Name * diff --git a/src/ElasticquentTrait.php b/src/ElasticquentTrait.php index a23e7c4..4142f00 100644 --- a/src/ElasticquentTrait.php +++ b/src/ElasticquentTrait.php @@ -218,10 +218,11 @@ public static function reindex() * @param int $limit * @param int $offset * @param array $sort + * @param array $highlight * * @return ElasticquentResultCollection */ - public static function searchByQuery($query = null, $aggregations = null, $sourceFields = null, $limit = null, $offset = null, $sort = null) + public static function searchByQuery($query = null, $aggregations = null, $sourceFields = null, $limit = null, $offset = null, $sort = null, $highlight = null) { $instance = new static; @@ -243,6 +244,10 @@ public static function searchByQuery($query = null, $aggregations = null, $sourc $params['body']['sort'] = $sort; } + if (!empty($highlight)) { + $params['body']['highlight'] = $highlight; + } + $result = $instance->getElasticSearchClient()->search($params); return static::hydrateElasticsearchResult($result); @@ -432,7 +437,7 @@ public static function mappingExists() /** * Get Mapping * - * @return void + * @return array */ public static function getMapping() { @@ -604,6 +609,20 @@ public function newFromHitBuilder($hit = array()) $attributes[$key] = $value; } } + + // Replace highlight to attributes + if ($this->getConfigIsHighlightTheSource()) { + if (isset($hit['highlight'])) { + foreach ($hit['highlight'] as $key => $value) { + // assume you set "number_of_fragments"=>1 on highlight setting + // does not support multi fragments highlight for convinience + // https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-highlighting.html + if (is_scalar($attributes[$key])) { + $attributes[$key] = $value[0]; + } + } + } + } $instance = $this::newFromBuilderRecursive($this, $attributes); diff --git a/src/config/elasticquent.php b/src/config/elasticquent.php index 7562a61..9c00cd4 100644 --- a/src/config/elasticquent.php +++ b/src/config/elasticquent.php @@ -29,4 +29,13 @@ 'default_index' => 'my_custom_index_name', + /* + |-------------------------------------------------------------------------- + | Should We Replace The Source With Highlight + |-------------------------------------------------------------------------- + | + */ + 'highlight_in_source' => true, + + );