distantnative / search-for-kirby

Kirby 3 plugin for adding a search index (sqlite or Algolia).
43 stars 3 forks source link

Use 'field' option to rank results #10

Closed philipmarnef closed 4 years ago

philipmarnef commented 4 years ago

I have the 'fields' option set but they are all treated equally in the ranking of the results. It would be very nice if the order of the fields influenced the ranking, so results with a match in the title field take precedence over matches in the body field.

Or is there another way to achieve this?

distantnative commented 4 years ago

What provider are you using?

philipmarnef commented 4 years ago

SQLite

philipmarnef commented 4 years ago

I read Algolia uses the order of the searchableAttributes to calculate rank, was wondering if something similar is possible with the sqlite provider.

distantnative commented 4 years ago

I have to check again why I switched back from fts5 to fts4... but if that reason isn’t valid anymore and we could use fts5, there is a functionality for weights: https://android.jlelse.eu/offline-full-text-search-in-android-ios-b4dd5bed3acd

philipmarnef commented 4 years ago

In the alpha.2 I'm testing you ARE using FTS5...

I just added ->andWhere('rank MATCH \'bm25(5.0, 4.0, 3.0, 2.0, 1.0, 0, 0)\'') after ->where(...) to the search() method in the Sqlite Provider to test and it works.

I guess you could iterate over the search.fields option and build the bm25 query accordingly. It would be even cooler if you could define custom weights in your settings like:

'search' => [
    'fields' => [
      'pages' => [
          'title' => 10, 
          'intro' => 5,
          'body' => 1
      ]
    ],
distantnative commented 4 years ago

Oh my haha, I still had FTS4 in my head.

I can't implement it as suggested, since that way is already used to allow to run fields through field methods before adding to the index.

I will likely just add another option weights where you can define that.

distantnative commented 4 years ago

Actually not as easy as thought...

bm25 needs the wights in the exact order as columns were created. However, columns are currently created dynamically based on what the indexed content actually is.

In addition, if a page and a file have the same named field, the database table simply uses the same column. So weights could only be defined on basis of field names (No matter if page, file or user).

distantnative commented 4 years ago

Added the most basic/simple implementation for now:

Screen Shot 2020-02-01 at 17 56 26
distantnative commented 4 years ago

✅ in next alpha release (1.0.0-alpha.3)