distantnative / search-for-kirby

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

Operator on SQLite #51

Open psntr opened 3 years ago

psntr commented 3 years ago

To reflect previous issue #47 about the operator AND.

@distantnative I do have the SQLite provider turn on.

in config.php: 'search' => [ 'provider' => 'sqlite', 'fields' => [ 'pages' => [ 'title', 'text', 'description' ] ] ]

Controller:

return function ($site) {

    $query = trim(get('q'));
    if (empty($query) === false) {
        //$results = $site->search($query, 'title|text|description|blocks');
        $results = $site->search($query, ['operator' => 'AND', 'limit' => 50]);
    }
    // apply pagination
    $results = $results->filterBy('status', 'listed');
    $total = $results->count();
    $results   = $results->paginate(12);
    $pagination = $results->pagination();
    return [
        'query' =>  html(strip_tags($query), false),
        'total' =>  $total,
        'results' => $results ?? [],
        'pagination' => $pagination
    ];
};

The index is also build and does reflect the search query when there is a single word.

distantnative commented 3 years ago

@psntr thanks got opening this new issue. I will try to take a closer look at this in the following days

psntr commented 3 years ago

@distantnative thank you, I'm sorry it brought so much heat from the previous thread.

From what I can tell, the search works well for single words. But when it comes to composed word like "firstname" and "lastname" it doesn't work as expected. For some reason it seems to perform the search and return results with particular words combination which I don't see the logic.

For instance if I would put in the search query "John doe" and in the SQLite export there is obviously a post with the title "John doe […]" it will not find it. But with maybe other word combination, let's say "Marco Polo" it will return some results although the search query string isn't necessarily in the title. So far I was only able to make it work with one particular "firstname" "lastname" combination.