Last active
October 31, 2021 21:10
-
-
Save marksparrish/24cd0f29e1c96215684f4e15f055b55d to your computer and use it in GitHub Desktop.
Laravel ElasticSearch Builder Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Elastic\Builders; | |
use App\Elastic\ElasticEngine; | |
use App\Models\Tag; | |
class ProductBuilder extends ElasticEngine | |
{ | |
/** | |
* @param mixed $query_string | |
* @return $this | |
*/ | |
function queryProductName($query_string) | |
{ | |
$query_string = strtolower(preg_replace('/^\s+|\s+$|\s+(?=\s)/', '', $query_string)); | |
$this->must_query->put('simple_query_string', | |
[ | |
'query' => $query_string, | |
'analyzer' => 'standard', | |
'fields' => ['name_*'], | |
'default_operator' => 'and' | |
] | |
); | |
return $this; | |
} | |
/** | |
* @param mixed $query_string | |
* @return $this | |
*/ | |
function queryProductFull($query_string) | |
{ | |
$query_string = strtolower(preg_replace('/^\s+|\s+$|\s+(?=\s)/', '', $query_string)); | |
$this->must_query->put('simple_query_string', | |
[ | |
'query' => $query_string, | |
'analyzer' => 'standard', | |
'fields' => ['name_*', 'tags'], | |
'default_operator' => 'and' | |
] | |
); | |
return $this; | |
} | |
/** | |
* Create a match all tags filter | |
* | |
* | |
* @param array(int) $tags An array of tag id (integers) | |
* | |
* @return tag filter array | |
* @author | |
**/ | |
function filterAllTags (array $tags) | |
{ | |
$count = count($tags); | |
$this->filter->push([ | |
'terms_set' => [ | |
'tags' => [ | |
'terms' => $tags, | |
'minimum_should_match_script' => [ | |
'source' => "{$count}", | |
], | |
] | |
] | |
]); | |
return $this; | |
} | |
/** | |
* Create a match any tag filter | |
* | |
* | |
* @param array(int) $tags An array of marker id (integers) | |
* | |
* @return tag filter array | |
* @author | |
**/ | |
function filterAnyTag (array $tags) | |
{ | |
$this->filter->push([ | |
'terms' => [ | |
'campaign_tags' => $tags, | |
] | |
]); | |
return $this; | |
} | |
/** | |
* Create a region filter | |
* | |
* | |
* @param (string) $region A region (string) | |
* | |
* @return region filter array | |
* @author | |
**/ | |
function filterRegion ($region) | |
{ | |
$this->filter->push([ | |
'term' => [ | |
'region' => $region, | |
] | |
]); | |
return $this; | |
} | |
/** | |
* @param int $size | |
* @return $this | |
*/ | |
function aggTags($size = 10) | |
{ | |
$this->aggregations->put('tags', | |
[ | |
'terms' => [ | |
'field' => 'tags', | |
'size' => $size, | |
] | |
] | |
); | |
return $this; | |
} | |
function formatTags($agg) | |
{ | |
$buckets = $agg['buckets']; | |
$return = []; | |
foreach ($buckets as $bucket) { | |
// look up the tag by id | |
$tag = Tag::find($bucket['key']); | |
// if a tag is not present then we will skip adding to the array | |
if ($tag) { | |
$return[] = [ | |
'id' => $bucket['key'], | |
// 'tag_category' => $tag->category, | |
'tag_name' => $tag->name, | |
'count' => $bucket['doc_count'] | |
]; | |
} | |
} | |
return $return; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment