Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search - can't sort by date #10892

Open
AtmoFX opened this issue Oct 3, 2024 · 2 comments
Open

Search - can't sort by date #10892

AtmoFX opened this issue Oct 3, 2024 · 2 comments
Labels

Comments

@AtmoFX
Copy link

AtmoFX commented Oct 3, 2024

Bug description

Continuation of this discussion

Apparently, search:results array can only return result sorted by relevance, when I expected, from 2383, that adding sort='date:desc' would do the trick.

I also tried to add 'property_weights' => ['date' => 1], to the index used for the search, to no avail.

How to reproduce

I posted an example Statamic - Search, sort by date repo from a blank site.

There are 3 articles posted on 3 consecutive days. All 3 articles contain the word "text" but the middle article contains it twice so that it is expected to be more relevant than the other 2 when searching for that word.

The code for the search page (template search.antlers.html) is:

<ol>
    {{ search:results index="testindex" sort='date:desc' }}
        <li>{{ date }} / {{ title }} </li>
    {{ /search:results }}
</ol>

testindex is configured like so:

        'testindex' => [
            'driver' => 'local',
            'searchables' => ['collection:test'],
            'fields' => ['title', 'content'],
            'min_characters' => 3,
            'property_weights' => [ 
                'date' => 1
            ],
        ],
  1. Goto URL /search?q=text. The desired order (newest to oldest) of the returned articles is 3, 2, 1.
    They will be returned in order 2, 1, 3, though.
  2. Removing sort='date:desc' from the template has no impact
  3. Changing the index definition to include 0, 1 or several of the following lines has no impact either (at least, no impact on the order but definitely an undesired impact on the search result, see comments:
    • 'property_weights' => [ 'date' => 1],
    • 'include_properties' => ['date'], (with this line, the search returns no post; the example site from the repo will just display the title of the search page itself)
    • 'include_properties' => ['title', 'tags', 'content', 'date'], (same unsorted results)
    • 'sort_by_score' => false,
    • 'sort_by_score' => true, (to make it explicit)
    • 'group_by_category' => true, (out of desperation)
    • Transforming the date to a number in the hope it can be multiplied by the property_weights:
    'transformers' => [ 
        'date' => function ($date) { 
            $duration = date_diff(date_create("1900-01-01"), $date); 
            return (new DateTime())->setTimeStamp(0)->add($duration)->getTimeStamp();
        }
    ]
    
    (Not too sure about this one but Statamic lets me update the index so I assume it was OK).

Logs

No response

Environment

Environment
Application Name: Statamic
Laravel Version: 11.26.0
PHP Version: 8.3.12
Composer Version: 2.7.7
Environment: local
Debug Mode: ENABLED
URL: Test.test
Maintenance Mode: OFF
Timezone: UTC
Locale: en

Cache
Config: NOT CACHED
Events: NOT CACHED
Routes: NOT CACHED
Views: CACHED

Drivers
Broadcasting: log
Cache: file
Database: sqlite
Logs: stack / single
Mail: log
Queue: sync
Session: file

Statamic
Addons: 0
Sites: 1
Stache Watcher: Enabled (auto)
Static Caching: Disabled
Version: 5.29.0 Solo

Installation

Fresh statamic/statamic site via CLI

Additional details

No response

@duncanmcclean duncanmcclean changed the title Search - sort by date Can't search by date Oct 3, 2024
@duncanmcclean
Copy link
Member

duncanmcclean commented Oct 3, 2024

I think you might need to add the date field to the fields array in your search config in order for it to work:

'fields' => ['title', 'content', 'date'],

@AtmoFX AtmoFX changed the title Can't search by date Search - can't sort by date Oct 3, 2024
@AtmoFX
Copy link
Author

AtmoFX commented Oct 3, 2024

If I do that, I can now search by date (e.g. /search?q=2024-10-02) but this has no impact on the sorting of results.
What I am trying to do is the sorting only, not the lookup of posts. I took the liberty to restore the title of this issue accordingly.

BTW, this is the reason why I tried using include_properties. The description (down this section) is not very explicit if you ask me but it looked to me that there was a chance the date was included in the search results and could therefore be used for sorting.

FYI, I noticed in the debugbar that {{ search:results index="testindex" sort='date:desc' }} does impact (at least) the query:

select * from search_testindex where status = 'published' order by date desc

Why I get the results in a different order is beyond me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants