How do I select a record by matching an index based on a partial string that contains '-' characters?

612 views Asked by At

I'm using YDN-DB (an abstraction on top of IndexedDB) as a local database. I have an object store called 'conversations', and in that store, there's an index called 'participants' where there is a string containing id's for different users in the conversation. For example:

Example Conversation #1:

id: 1234343434353456,
participants: '171e66ca-207f-4ba9-8197-d1dac32499db,82be80e2-2831-4f7d-a8d7-9223a2d4d511'

Example Conversation #2:

id: 4321343434356543,
participants: 'd7fa26b3-4ecc-4f84-9271-e15843fcc83f,171e66ca-207f-4ba9-8197-d1dac32499db'

To try to perform a partial match on an index, I tried using ydn-db-fulltext as a solution. The full text catalog looks like this:

  {
    name: 'participants',
    lang: 'en',
    sources: [
      {
        storeName: 'conversations',
        keyPath: 'participants',
        weight: 1
      }
    ]
  }

I see that the catalog is generated, but there seems to be a problem doing exact matches. For example, if I query using only part of the key in the participants index, I get back a primary key from the catalog:

db.search('participants', 'd7fa26b3').done(function(results) {
  if(results.length == 0) console.debug('No results found...');
  console.debug(results);                  // there is 1 object here!
  var primaryKey = results[0].primaryKey;  // primaryKey exists!
});

However, when using any value past the '-', the search request returns 0 results:

db.search('participants', 'd7fa26b3-4ecc-4f84-9271-e15843fcc83f').done(function(results) {
  if(results.length == 0) console.debug('No results found...');
  console.debug(results);                  // there are 0 objects in the array
  var primaryKey = results[0].primaryKey;  // primaryKey throws undefined since there are 0 results!
});

This makes sense, when reading the documentation, in that '-' and '*' are reserved characters that remove a phrase and match a prefix respectively:

Query format is free text, in which implicit and/or/near logic operator apply for each token. Use double quote for exact match, - to subtract from the result and * for prefix search.

I tried putting double quotes inside the single quotes, using only double quotes, and also escaping all of the '-' characters with a backslash, but none of these seem to work.

So the question is how does one perform a match in an index where the string contains '-' characters?

2

There are 2 answers

9
Kyaw Tun On

Have you try db.search('participants', '"d7fa26b3"').

BTW, you are using full text search that is not suppose to do. You have to tokenize your string and index them manually.

0
Josh On

If you store the participants field of your object as an array, then you can use the multi-entry flag to the createIndex method called on the participants field, and probably do what you want.

The number of items in the participants property of the object is mutable. When you update an object in the store and it has a different number of items in the partic property, then the index is automatically updated as a result (just like any other index). If you add an item to the prop, then restore (put/override/cursor.update) the object in the store, the index updates.

It helps to review the basics of how a multi-entry index works. You can do this with vanilla js, without a framework, and certainly without full-text searching.