Closed zahidhasanemon closed 2 years ago
Hi @zahidhasanemon,
The term
search finds documents based on the exact value match. Therefore, it's better to define authorList
as a keyword
type in your mapping. In addition, authorList
should be indexed as an array of values and not a single string, i.e.:
public function toSearchableArray()
{
return [
// other fields
'authorList' => explode(',', $this->authorList) // you probably also need to trim each value
];
}
I hope this will give you the right direction. I'm closing this issue, cause it's a more ES-related question and not a package bug or a feature request.
@babenkoivan thank you very much for your suggestion and I am able to solve the filter. I need one more help if you may please. I am trying to sort by created_at
field. I have added the field in toSearchableArray
method.
public function toSearchableArray()
{
return [
'full_text_search' => $this->full_text_search,
'min_price' => $this->min_price,
'created_at' => $this->created_at->timestamp,
'authorList' => $this->authors,
];
}
and trying to sort like
$listBooks = Product::searchQuery($query)->sort('created_at', 'asc')->paginate(40)->onlyModels();
but I am getting Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. and I checked in kibana created_at
field is indexed as string. I am able to sort by number fields like min_price
. How can I sort using created_at
filed??
Hey @zahidhasanemon, you need to make sure that your index has a proper mapping. created_at
must be of date
type with the corresponding format
. You can use this package to create indices, update mapping, etc.
I am using following versions | PHP | 7.4 | Elasticsearch | 7.16 | Laravel | 6 | Laravel Scout | 8.6 | elastic-scout-driver | 2.0 | elastic-scout-driver-plus | 3.1
I want to search and filter data at the same time. Search from a field and filter from another field. Search string must match in that search field and filter field should contain one of the values from an array. So I am making query like
This returns no result. But if I omit the filter, only search query is returning expected result.
Is my approach wrong? What should be the right way to query to get my expected result?