Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add highligh search result support #104

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,14 @@ return array(

'default_index' => 'my_custom_index_name',

/*
|--------------------------------------------------------------------------
| Should We Replace The Source With Highlight
|--------------------------------------------------------------------------
|
*/
'highlight_in_source' => true,

);

```
Expand Down Expand Up @@ -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:**
Expand All @@ -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

Expand Down
15 changes: 15 additions & 0 deletions src/ElasticquentConfigTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
23 changes: 21 additions & 2 deletions src/ElasticquentTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down Expand Up @@ -432,7 +437,7 @@ public static function mappingExists()
/**
* Get Mapping
*
* @return void
* @return array
*/
public static function getMapping()
{
Expand Down Expand Up @@ -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);

Expand Down
9 changes: 9 additions & 0 deletions src/config/elasticquent.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,13 @@

'default_index' => 'my_custom_index_name',

/*
|--------------------------------------------------------------------------
| Should We Replace The Source With Highlight
|--------------------------------------------------------------------------
|
*/
'highlight_in_source' => true,


);