Closed lukemorcom closed 1 year ago
Hi @lukemorcom,
Scout maps Elasticsearch documents to Eloquent models using ids: the document id should be the same as the model's Scout key, which is the id
field by default.
Please check if you have a model in the database with the same id as the document returned in a hit. If it is not the case, then probably something is misconfigured or maybe documents were indexed not using Scout.
If you have the corresponding model in the database, but it is not returned, then try find out what queries Scout does to retrieve the model. You can use some profiler or debug Elastic\ScoutDriverPlus\Builders\DatabaseQueryBuilder
. This should give you some insights.
Hope this helps 🙂
Hey @babenkoivan thank you for the response, really appreciate it.
The mappings all line up, including IDs. I have done some more troubleshooting, and when I dd()
the results of the searchQuery...
$results = User::searchQuery($this->query)
->size(100)
->execute();
dd($results);
The correct IDs are found for the correct index, and listed in the mappedIds
key of the results - however the mappedModels
key is totally empty. On double checking, the documents are definitely there in my elastic cluster, and also in the database, but they are not being mapped correctly. I did look into debugging DatabaseQueryBuilder
, but I couldn't find that class (I am using version 3.5.1 of the driver if that helps). Can you suggest any further steps I can take to troubleshoot?
Appreciate your time :)
After some more troubleshooting, everything runs smoothly until mapModels()
gets called in LazyModelFactory
. Specifically, $query->whereIn($model->getScoutKeyName(), $ids);
is the call which results in an empty collection being returned. Not overly familiar with how Scout works under the hood, but this is returning the key name as users.id
- when I change this to be id
(since the from is already set on the builder) the results I get are accurate.
As someone with undoubtedly more experience with the Scout source than me, do you think it's likely this is a problem with my code somewhere? The table structure for users
is as standard as it comes so I'm unsure where to go from here. But if it is looking like a problem with my code itself, I'd rather not take up anymore of your time responding to this!
Thanks :)
@lukemorcom it is hard to tell where the problem is without looking into your code and infrastructure. I’d recommend updating all Elastic packages to the latest version if possible, or you can simply override getScoutKeyName
method in your model if that works for you:
class User
{
public function getScoutKeyName()
{
return “id”;
}
}
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 7 days
Hi,
I have a simply query to match a username search with a wildcard - it's in array syntax and the JSON equivalent produces the expected results in Kibana.
Through troubleshooting I can see that the results also come back when running
execute()
on the query. But it seems that when callingmodels()
on the results object, I don't get any models, and through some more troubleshooting it appears that the hits each have a null value for the 'model' key.I have done a little bit of a dive into the source code but haven't really got anywhere - I'm sure this could just be a simple thing that I am missing here, but I'm struggling to think of any more troubleshooting steps. Just looking for any advice on why the model for a hit may come back as null?
Many thanks in advance 🙏