From 2db23a52dde286a791b3f314baba45128a7e1609 Mon Sep 17 00:00:00 2001 From: itbdw Date: Tue, 2 Aug 2016 17:00:45 +0800 Subject: [PATCH 1/7] add highligh search result support --- readme.md | 2 +- src/ElasticquentTrait.php | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index 00c3d4c..3d043b4 100644 --- a/readme.md +++ b/readme.md @@ -338,7 +338,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:** diff --git a/src/ElasticquentTrait.php b/src/ElasticquentTrait.php index a23e7c4..b965c87 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; @@ -242,6 +243,10 @@ public static function searchByQuery($query = null, $aggregations = null, $sourc if (!empty($sort)) { $params['body']['sort'] = $sort; } + + if (!empty($highlight)) { + $params['body']['highlight'] = $highlight; + } $result = $instance->getElasticSearchClient()->search($params); From 1d5e60f9bb3aae6f0931b9a2f3587098171d7e02 Mon Sep 17 00:00:00 2001 From: itbdw Date: Tue, 2 Aug 2016 20:00:05 +0800 Subject: [PATCH 2/7] fix getMapping() method return type --- src/ElasticquentTrait.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ElasticquentTrait.php b/src/ElasticquentTrait.php index b965c87..e298eee 100644 --- a/src/ElasticquentTrait.php +++ b/src/ElasticquentTrait.php @@ -243,7 +243,7 @@ public static function searchByQuery($query = null, $aggregations = null, $sourc if (!empty($sort)) { $params['body']['sort'] = $sort; } - + if (!empty($highlight)) { $params['body']['highlight'] = $highlight; } @@ -437,7 +437,7 @@ public static function mappingExists() /** * Get Mapping * - * @return void + * @return array */ public static function getMapping() { From f33c23c2d4ebe37ce12b6c59fd069096f4682400 Mon Sep 17 00:00:00 2001 From: itbdw Date: Sat, 6 Aug 2016 11:19:13 +0800 Subject: [PATCH 3/7] add a config to replace the result with highlight --- src/ElasticquentConfigTrait.php | 15 +++++++++++++++ src/ElasticquentTrait.php | 9 +++++++++ src/config/elasticquent.php | 9 +++++++++ 3 files changed, 33 insertions(+) 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 e298eee..1190288 100644 --- a/src/ElasticquentTrait.php +++ b/src/ElasticquentTrait.php @@ -609,6 +609,15 @@ 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) { + $attributes[$key] = $value; + } + } + } $instance = $this::newFromBuilderRecursive($this, $attributes); diff --git a/src/config/elasticquent.php b/src/config/elasticquent.php index 7562a61..043acc8 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 Highlights + |-------------------------------------------------------------------------- + | + */ + 'highlight_in_source' => true, + + ); From c878621ec4bc12619d72b3877a659f337b9896ed Mon Sep 17 00:00:00 2001 From: itbdw Date: Sat, 6 Aug 2016 11:22:17 +0800 Subject: [PATCH 4/7] update readme for highlight --- readme.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/readme.md b/readme.md index 3d043b4..22e9253 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 Highlights + |-------------------------------------------------------------------------- + | + */ + 'highlight_in_source' => true, + ); ``` @@ -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 From c13c167a219493bc756b638caf41cd357662f91d Mon Sep 17 00:00:00 2001 From: itbdw Date: Sat, 6 Aug 2016 11:24:45 +0800 Subject: [PATCH 5/7] tidy comments --- readme.md | 2 +- src/config/elasticquent.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index 22e9253..1964429 100644 --- a/readme.md +++ b/readme.md @@ -148,7 +148,7 @@ return array( /* |-------------------------------------------------------------------------- - | Should We Replace The Source with Highlights + | Should We Replace The Source With Highlight |-------------------------------------------------------------------------- | */ diff --git a/src/config/elasticquent.php b/src/config/elasticquent.php index 043acc8..9c00cd4 100644 --- a/src/config/elasticquent.php +++ b/src/config/elasticquent.php @@ -31,7 +31,7 @@ /* |-------------------------------------------------------------------------- - | Should We Replace The Source with Highlights + | Should We Replace The Source With Highlight |-------------------------------------------------------------------------- | */ From 524b0237108a508b263cc0a348fff9c4efd64451 Mon Sep 17 00:00:00 2001 From: itbdw Date: Mon, 8 Aug 2016 15:15:02 +0800 Subject: [PATCH 6/7] replace the highlight only the data type is scalar for now --- src/ElasticquentTrait.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/ElasticquentTrait.php b/src/ElasticquentTrait.php index 1190288..535205e 100644 --- a/src/ElasticquentTrait.php +++ b/src/ElasticquentTrait.php @@ -614,7 +614,10 @@ public function newFromHitBuilder($hit = array()) if ($this->getConfigIsHighlightTheSource()) { if (isset($hit['highlight'])) { foreach ($hit['highlight'] as $key => $value) { - $attributes[$key] = $value; + //todo have no idea why the type of $value is array + if (is_scalar($attributes[$key])) { + $attributes[$key] = $value[0]; + } } } } From 895aca9e7b03e15fa4f49bf781b137afe3f8f8bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?IT=E4=B8=8D=E5=80=92=E7=BF=81=20=28Zhao=20Binyan=29?= Date: Sat, 5 Aug 2017 01:50:59 +0800 Subject: [PATCH 7/7] Update ElasticquentTrait.php assume you set "number_of_fragments"=>1 on highlight setting ie. ``` $highlight=[ "fragment_size"=>150, "no_match_size"=>150, "number_of_fragments"=>1, "fields"=>[ "title"=>(Object)[], "content"=>(Object)[], ], ]; ``` --- src/ElasticquentTrait.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ElasticquentTrait.php b/src/ElasticquentTrait.php index 535205e..4142f00 100644 --- a/src/ElasticquentTrait.php +++ b/src/ElasticquentTrait.php @@ -614,7 +614,9 @@ public function newFromHitBuilder($hit = array()) if ($this->getConfigIsHighlightTheSource()) { if (isset($hit['highlight'])) { foreach ($hit['highlight'] as $key => $value) { - //todo have no idea why the type of $value is array + // 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]; }