How can I sort elastic search ui results by date?

Hello, what happens is that I have a page with react that uses the package:

@elastic/search-ui-site-search-connector

Through the configuration and the query I bring information and already display it visually.

The problem is that I want to sort the results by date but even if I add the sort property, it doesn't do it, I tried to do it even with a simple and more common field as title but it doesn't sort even if I do it.

I try to use sort property like the next way:

"sort": [{"createdPageDate.raw": {"order": "desc"}}]
sort: [{ createdPageDate: { order: 'desc' } }]
sort: [{ "createdPageDate": { "order": 'desc' } }]
"sort": [{ "createdPageDate": { "order": 'desc' } }]
sort: {createdPageDate : 'desc'},
sort: {
field: "createdPageDate",
order: "desc",
}

This is the query and config that I use:

const searchConnector = new SiteSearchAPIConnector({
    engineKey: apikey,
    documentType: "page"
  });

  const config = {
    apiConnector: searchConnector,
    autocompleteQuery: {
      suggestions: {
        types: {
          documents: {
            fields: ["title"]
          }
        },
        size: 4
      }
    },
    searchQuery: {
      search_fields:
              {
                title: {},
                description: {}
              },
      result_fields: {
        title: {
          raw: {}
        },
        createdPageDate: {
          raw: {}
        },
        link: {
          raw: {}
        },
        page_id: {
          raw: {}
        },
        content_type: {
          raw: {}
        },
      },
      facets: {
        content_type: {type: "value"},
        size: {type: "value"}
      },
    },
    alwaysSearchOnInitialLoad: true,
  };

  // RENDER THE RESULT ON VISUAL FORM

<SearchProvider config={config}>
            <Results
                resultView={({result}) => {
                  if (result.content_type !== undefined) {
              // REST OF THE CODE 
             />
</SearchProvider>

Hi there
You can specify sort params in the initialState

initialState: {
    sort: [{ field: "createdPageDate", order: "desc"}]
  },

In RequestState doc you can also read more about other options to change it.

Yan

1 Like

Still not working, is necessary to modify something else about the code or the elasticsearch / swiftype settings?

Is a space or specific section of the query where I need to add the code you provide me?

Don't need to change anything in elasticsearch/swiftype
Put this initialState in the root of config

In sandbox example for some reason only works with sortList
Try to use this code below for sandbox

initialState: {
    sortList: [
      {
        field: "title.keyword",
        direction: "desc",
      },
    ],
  },

Yan

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.