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

Find old results after updating document with same ID but new data #607

Closed tonprince closed 1 year ago

tonprince commented 1 year ago

I am adding a document with ID XYZ, then updating the same document with new data. I expect not to find anything with old value, but assert failed.

import si from "search-index";
import sw from 'stopword';
import assert from 'assert';

export async function test() {
  let db = await si({ name: "test", stopwords: sw.eng });

  await db.PUT([{ _id: "XYZ", name: "bees" }]);

  let results = await db.QUERY("bees");
  assert.strictEqual(1, results.RESULT_LENGTH);

  await db.PUT([{ _id: "XYZ", name: "beex" }]);
  results = await db.QUERY("bees");
  assert.strictEqual(0, results.RESULT_LENGTH);
}

test();

AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: 0 !== 1

fergiemcdowall commented 1 year ago

Hmm interesting- yes there does seem to be a bug there. Will investigate.

fergiemcdowall commented 1 year ago

Actually on second thoughts- having reviewed the code and documentation, this is actually the intended functionality. If a document is added with an _id that is already in use in the index, then all the attributes of the new doc are simply appended to the old one. This allows for some snazzy updating possibilities.

That said, I do agree that it is maybe more intuitive that the "new" document simply replaces the old one, deleting all existing tokens. This could be an addition for v4.x.x

eklem commented 1 year ago

I guess it needs to just be documented clearly and for backwards compability keep it as is and add an alternative way of updating?