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

date field not search indexed #5626

Closed
hiasl opened this issue Feb 10, 2020 · 4 comments
Closed

date field not search indexed #5626

hiasl opened this issue Feb 10, 2020 · 4 comments

Comments

@hiasl
Copy link
Contributor

hiasl commented Feb 10, 2020

Description

We have a date field with search indexing enabled in certain entry types, but the keywords column in Craft's search index stays empty and the date is therefore not searchable.

Steps to reproduce

  1. Create a new field of type date (both date and time in our case)
  2. Make sure the checkbox "Use this field's values as search keyword" is enabled
  3. Add the field to an entry type
  4. Create an entry based on the entry type, set a date/time on the field mentioned above and save
  5. Wait for the search index to update
  6. Check the searchindex table in Craft's DB for the entry saved and find the keywords column empty.

Please note we are using a multisite setup and the keyword column is empty for each multi site row in the searchindex table.

Additional info

  • Craft version: 3.4.5
  • PHP version: 7.3.8
  • Database driver & version: MySQL 5.7.25
@brandonkelly
Copy link
Member

You’ll probably have better luck working directly with the Date/Time field’s query param rather than going through search, which isn’t really intended for numerical/date values.

@hiasl
Copy link
Contributor Author

hiasl commented Feb 11, 2020

Does this work in the CP as well? We are trying to find entries matching a certain date in a certain field.

@brandonkelly
Copy link
Member

Good question. There’s currently no built-in way to set a Date/Time field’s query param from the control panel, but we’re working to make that possible in Craft 4.

In the meantime, if you are running Craft 3.4.4.1 or later, you could add a date range picker to element indexes yourself, using the CP JS plugin and some custom JS code:

// Is this the Entries index page?
if (Craft.elementIndex && Craft.elementIndex.elementType === 'craft\\elements\\Entry') {
    // Add a date range picker to the toolbar
    let startDate = endDate = null;
    Craft.ui.createDateRangePicker({
        onChange: function(s, e) {
            startDate = s;
            endDate = e;
            Craft.elementIndex.updateElements();
        },
    }).appendTo(Craft.elementIndex.$toolbar);

    // When the element index is updated, set our Date/Time field param
    Craft.elementIndex.on('registerViewParams', function(event) {
        event.params.criteria.myDateTimeField = ['and'];
        if (startDate) {
            event.params.criteria.myDateTimeField.push('>=' + (startDate.getTime() / 1000));
        }
        if (endDate) {
            event.params.criteria.myDateTimeField.push('<' + (endDate.getTime() / 1000 + 86400));
        }
    });
}

(Replace myDateTimeField with your actual Date/Time field handle.

@hiasl
Copy link
Contributor Author

hiasl commented Feb 17, 2020

Works perfectly! Thanks!

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

No branches or pull requests

2 participants