Cms: date field not search indexed

Created on 10 Feb 2020  路  4Comments  路  Source: craftcms/cms

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

Most helpful comment

Good question. There鈥檚 currently no built-in way to set a Date/Time field鈥檚 query param from the control panel, but we鈥檙e 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.

All 4 comments

You鈥檒l probably have better luck working directly with the Date/Time field鈥檚 query param rather than going through search, which isn鈥檛 really intended for numerical/date values.

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

Good question. There鈥檚 currently no built-in way to set a Date/Time field鈥檚 query param from the control panel, but we鈥檙e 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.

Works perfectly! Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

timkelty picture timkelty  路  3Comments

michaelhue picture michaelhue  路  3Comments

RitterKnightCreative picture RitterKnightCreative  路  3Comments

mattstein picture mattstein  路  3Comments

angrybrad picture angrybrad  路  3Comments