teamtnt / tntsearch

A fully featured full text search engine written in PHP
https://tnt.studio/solving-the-search-problem-with-laravel-and-tntsearch
MIT License
3.1k stars 291 forks source link

One index for multiple tables or combining multiple indexes #155

Open dirkjf opened 6 years ago

dirkjf commented 6 years ago

First of I want to say thank you for creating this and making it available. I am using this in a symfony application and it is working quite well.

There is one thing I did not achieve in a way I wanted, and I think it might not be possible currently. It's about indexing multiple tables in one index and showing the mixed results. I am not the first one with this issue. This has been noted before in #100, #117 and #135.

When working with one index for multiple tables, the corresponding IDs are no longer corresponding because you will have for example and ID '14' in multiple tables. Since TNTsearch only returns an ID there is no way of knowing which table this ID belongs to.

When working with multiple indexes you cannot combine the results. You could put them under each other or use a faceted search, But what if you would like to combine the results?

I think there are two possible solutions:

  1. Return not only the ID of the document/record, but also some table identifier. This way you could have one index with all tables combined.
  2. Return the individual match score of the records as a relevance percentage. This would make it possible to combine the results from multiple indexes on one page while being able to have the correct order.

Am I overlooking something? Please let me know.

rauwebieten commented 6 years ago

I did some tests, and came up with the following:

$indexer = $tnt->createIndex('name.index');
$indexer->query('SELECT concat(\'album_\',id) as id, artist, title, year FROM albums;');
$indexer->run();
$indexer->query('SELECT concat(\'artist_\',id) as id, name FROM artists;');
$indexer->run();

Now the result has info about the originating tables:

array:3 [                       
  "ids" => array:4 [            
    0 => "album_1"              
    1 => "album_2"              
    2 => "artist_1"             
    3 => "artist_2"             
  ]                             
  "hits" => 4                   
  "execution_time" => "1.523 ms"
]                               

Anyone of the developers, is this the way to go? I do not see any other solution at first glance.

emjayess commented 5 years ago

I am also looking for this type of interface... searching against multiple/many indexed models at once and getting mixed results, without re-assembling from disparate search indeces by hand. Note that I am not looking for faceted search/browsing, that's a different use case.

Point of reference: in the context of Laravel Scout with the Algolia driver, this is achieved via 'Aggregators'.