Skip to content

Instantly share code, notes, and snippets.

@bayareawebpro
Created October 16, 2019 22:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bayareawebpro/bb8c3a03718f6dc70a8ed569e4465f7e to your computer and use it in GitHub Desktop.
Save bayareawebpro/bb8c3a03718f6dc70a8ed569e4465f7e to your computer and use it in GitHub Desktop.
<?php
namespace App;
use App\Traits\FullTextSearchable;
use App\Traits\Radial;
use Illuminate\Database\Eloquent\Model;
use ScoutElastic\Searchable;
class Location extends Model
{
use Searchable, Radial;
public $timestamps = false;
/**
* Fillable Attributes
* @var array
*/
public $fillable = [
"osm_type",
"osm_id",
"class",
"type",
"name",
"display_name",
"alternate_names",
"street",
"city",
"county",
"state",
"country",
"country_code",
"lat",
"lon",
"north",
"south",
"east",
"west",
"place_rank",
"importance",
"wikidata",
"wikipedia",
];
/**
* Attribute Casts
* @var array
*/
public $casts = [
"lat" => "double",
"lon" => "double",
"north" => "double",
"south" => "double",
"east" => "double",
"west" => "double",
"importance" => "double",
];
/**
* Appendable Attributes
* @var array
*/
public $appends = [
"geo_point",
];
/**
* Wikidata URL
* @return string|null
*/
public function getWikidataUrl(){
if($url = $this->getAttributeValue("wikidata")){
return "https://wikipedia.org/wiki/{$url}";
}
return null;
}
/**
* Wikipedia URL
* @return string|null
*/
public function getWikipediaUrl(){
if($url = $this->getAttributeValue("wikipedia")){
return "https://wikipedia.org/wiki/{$url}";
}
return null;
}
/**
* LocationsIndexConfigurator
* @var string $indexConfigurator
*/
protected $indexConfigurator = LocationIndex::class;
/**
* Search Rules
* @var array $searchRules
*/
protected $searchRules = [];
/**
* Get the indexable data array for the model.
* @return array
*/
public function toSearchableArray()
{
return $this->toArray();
}
/**
* Model Search Mapping
* @var array
*/
protected $mapping = [
"properties" => [
"name" => [
"type" => "text",
"analyzer" => "keyword",
"fields" => [
"raw" => [
"type" => "keyword",
]
],
],
"display_name" => [
"type" => "text",
"analyzer" => "autocomplete",
"search_analyzer" => "autocomplete_search",
"fields" => [
"raw" => [
"type" => "completion",
]
]
],
"alternate_names" => [
"type" => "text",
"analyzer" => "autocomplete",
"search_analyzer" => "autocomplete_search",
"fields" => [
"raw" => [
"type" => "completion",
]
]
],
"street" => [
"type" => "text",
"analyzer" => "keyword",
"fields" => [
"raw" => [
"type" => "keyword",
]
],
],
"city" => [
"type" => "text",
"analyzer" => "keyword",
"fields" => [
"raw" => [
"type" => "keyword",
]
],
],
"county" => [
"type" => "text",
"analyzer" => "keyword",
"fields" => [
"raw" => [
"type" => "keyword",
]
],
],
"state" => [
"type" => "text",
"analyzer" => "keyword",
"fields" => [
"raw" => [
"type" => "keyword",
]
],
],
"country" => [
"type" => "text",
"analyzer" => "keyword",
"fields" => [
"raw" => [
"type" => "keyword",
]
],
],
"country_code" => [
"type" => "text",
"analyzer" => "keyword",
"fields" => [
"raw" => [
"type" => "keyword",
]
],
],
"lat" => [
"type" => "double",
],
"lon" => [
"type" => "double",
],
"north" => [
"type" => "double",
],
"south" => [
"type" => "double",
],
"east" => [
"type" => "double",
],
"west" => [
"type" => "double",
],
"importance" => [
"type" => "float",
],
"place_rank" => [
"type" => "float",
],
"geo_point" => [
"type" => "geo_point"
],
]
];
/**
* Get Location Point Attribute
* @return array
*/
public function getGeoPointAttribute(){
return $this->only(["lat", "lon"]);
}
/**
* Unique List of Field
* @param $query
* @param string $attribute
* @return mixed
*/
public function scopeUnique($query, $attribute = "type"){
return $query
->distinct()
->pluck($attribute)
->transform(function($entry){
return explode(",", $entry);
})
->flatten(1)
->unique();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment