fergiemcdowall / search-index

A persistent, network resilient, full text search library for the browser and Node.js
MIT License
1.38k stars 149 forks source link

Numeric SORT not working as expected #619

Closed mtford-carewell closed 5 months ago

mtford-carewell commented 5 months ago

@fergiemcdowall I strongly suspect this is just user error, but I'm having some trouble with numeric sorts...

Despite opting for DESCENDING I'm actually getting the results in ascending order.

Here's a minimal reproduction. I tried various options for FIELD e.g. _match.timestamp too. Am I missing something?

const si = await init();
await si.PUT([{ timestamp: 1 }, { timestamp: 2 }, { timestamp: 3 }, { timestamp: 4 }, { timestamp: 5 }]);

const results = await si.QUERY(
  {
    FIELD: "timestamp",
  },
  {
    SORT: {
      DIRECTION: "DESCENDING",
      TYPE: "NUMERIC",
      FIELD: "timestamp",
    },
  },
);

console.log("results", JSON.stringify(results, null, 2));

Result:

console.log
  results {
    "RESULT": [
      {
        "_id": "1711793911768-0",
        "_match": [
          {
            "FIELD": "timestamp",
            "VALUE": 1,
            "SCORE": 1
          }
        ]
      },
      {
        "_id": "1711793911768-1",
        "_match": [
          {
            "FIELD": "timestamp",
            "VALUE": 2,
            "SCORE": 2
          }
        ]
      },
      {
        "_id": "1711793911768-2",
        "_match": [
          {
            "FIELD": "timestamp",
            "VALUE": 3,
            "SCORE": 3
          }
        ]
      },
      {
        "_id": "1711793911768-3",
        "_match": [
          {
            "FIELD": "timestamp",
            "VALUE": 4,
            "SCORE": 4
          }
        ]
      },
      {
        "_id": "1711793911768-4",
        "_match": [
          {
            "FIELD": "timestamp",
            "VALUE": 5,
            "SCORE": 5
          }
        ]
      }
    ],
    "RESULT_LENGTH": 5
  }
fergiemcdowall commented 5 months ago

The documentation should probably a bit better here: you can only SORT on a SCORE (this makes sense with larger resultsets from a performance standpoint)

Something like this should work:

import SearchIndex from "search-index";

const si = await SearchIndex();

await si.PUT([
  { timestamp: 1 },
  { timestamp: 2 },
  { timestamp: 3 },
  { timestamp: 4 },
  { timestamp: 5 },
]);

const results = await si.QUERY(
  {
    FIELD: "timestamp",
  },
  {
    SCORE: {
      FIELDS: ["timestamp"],
      TYPE: "VALUE",
    },
    SORT: {
      DIRECTION: "DESCENDING",
      TYPE: "NUMERIC",
      FIELD: "timestamp",
    },
  }
);

console.log("results", JSON.stringify(results, null, 2));
mtford-carewell commented 5 months ago

ah! that did the trick!

thanks!

fergiemcdowall commented 5 months ago

You're welcome and thanks for opening the issue 👍