0

While using scout elastic search i need to use the same index for a model and their related fields as well. I created the index, but multiple model is not mapped with the same index. Also for same model related fields are not migrated.

Eg.

My index is lead_index

Model A has a Model B

Model A is a searchable model

All fields of Model A and Model B are mentioned in model A

Running model update with index is fine with Model A but throws error when trying to map with model B as multiple model cannot be mapped with same index.

Nitish Jha
  • 23
  • 4

1 Answers1

0

Laravel Scout has raw method so you can map results on your own

$modelB = new SearchResults(ModelA::search('zonga')->raw())->first()->modelB;

If you need to search among multiple models consider using an engine with multi-model searching feature. https://github.com/matchish/laravel-scout-elasticsearch#search-amongst-multiple-models

Mixed::search('title:Barcelona or to:Barcelona')
    within(implode(',', [
        (new Ticket())->searchableAs(),
        (new Book())->searchableAs(),
    ]))
->get();
Serhii Shliakhov
  • 2,631
  • 3
  • 19
  • 37
  • I am using laravel 5.8 and this packag https://github.com/ErickTamayo/laravel-scout-elastic . How i can search from multiple models? Package you highlighted doesn't work with Laravel 5.8 –  Sep 16 '20 at 14:34
  • Did you try `3.x` version? https://github.com/matchish/laravel-scout-elasticsearch/tree/3.x – Serhii Shliakhov Sep 17 '20 at 08:03