I wanted to have elasticsearch implemented for all my laravel search queries. I have the latest Laravel and latest elasticsearch installed using brew.
curl http://localhost:9200/
gives,
{
"name" : "_SFvSGk",
"cluster_name" : "elasticsearch_an398690",
"cluster_uuid" : "xBi3aTDaTkmA6dtzhpOrwg",
"version" : {
"number" : "6.5.4",
"build_flavor" : "oss",
"build_type" : "tar",
"build_hash" : "d2ef93d",
"build_date" : "2018-12-17T21:17:40.758843Z",
"build_snapshot" : false,
"lucene_version" : "7.5.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
Here I am using the driver babenkoivan/scout-elasticsearch-driver
.
Model,
namespace App;
use ScoutElastic\Searchable;
use Illuminate\Database\Eloquent\Model;
class Customer extends Model
{
use Searchable;
/**
* @var string
*/
protected $indexConfigurator = CustomerIndexConfigurator::class;
/**
* @var array
*/
protected $searchRules = [
CustomerSearchRule::class
];
/**
* @var array
*/
protected $mapping = [
'properties' => [
'text' => [
'type' => 'text',
'fields' => [
'ref_num' => [
'type' => 'keyword',
]
]
],
]
];
}
SearchRule,
namespace App;
use ScoutElastic\SearchRule;
class CustomerSearchRule extends SearchRule
{
/**
* @inheritdoc
*/
public function buildHighlightPayload()
{
return [
'fields' => [
'ref_num' => [
'type' => 'plain'
]
]
];
}
/**
* @inheritdoc
*/
public function buildQueryPayload()
{
$query = $this->builder->query;
return [
[
'match' => [
'ref_num' => [
'query' => $query,
'boost' => 2
]
]
]
];
}
}
Configurator,
namespace App;
use ScoutElastic\IndexConfigurator;
use ScoutElastic\Migratable;
class CustomerIndexConfigurator extends IndexConfigurator
{
use Migratable;
/**
* @var array
*/
protected $settings = [
//
];
}
I have a record with ref_num
as I50263
. So I should get this record when I search I50
same like like query
. I tried all the below search but I am getting the result only with the complete word I50263
.
return Customer::search('I50')->get();
// no record
return Customer::search('I50263')->get();
// got record
return Customer::searchRaw([
'query' => [
'bool' => [
'must' => [
'match' => [
'ref_num' => 'I502'
]
]
]
]
]);
// no record
return Customer::searchRaw([
'query' => [
'bool' => [
'must' => [
"match_phrase" => [
"ref_num" => [
"query" => "I50",
"boost" => 1
]
]
]
]
]
]);
// no record
Tried field type as text
also.