javascript - 如何根据包含 '-' 个字符的部分字符串匹配索引来选择记录?

标签 javascript indexeddb ydn-db ydn-db-fulltext

我使用 YDN-DB(IndexedDB 之上的抽象)作为本地数据库。我有一个名为“conversations”的对象存储,在该存储中,有一个名为“participants”的索引,其中有一个字符串,其中包含对话中不同用户的 id。例如:

示例对话#1:

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

示例对话#2:

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

为了尝试对索引执行部分匹配,我尝试使用 ydn-db-fulltext 作为解决方案。全文目录如下所示:

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

我看到目录已生成,但精确匹配似乎存在问题。例如,如果我仅使用参与者索引中的部分键进行查询,我会从目录中获取主键:

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!
});

但是,当使用“-”之后的任何值时,搜索请求将返回 0 个结果:

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!
});

reading the documentation 时,这是有道理的,因为 '-' 和 '*' 是保留字符,分别删除短语并匹配前缀:

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.

我尝试将双引号放在单引号内,仅使用双引号,并用反斜杠转义所有“-”字符,但这些似乎都不起作用。

所以问题是如何在字符串包含“-”字符的索引中执行匹配?

最佳答案

您尝试过db.search('participants', '"d7fa26b3"')吗?

顺便说一句,您正在使用不应该做的全文搜索。您必须对字符串进行标记并手动对其进行索引。

关于javascript - 如何根据包含 '-' 个字符的部分字符串匹配索引来选择记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24815315/

相关文章:

javascript - IndexedDB - ObjectStores vs 多个数据库 vs 索引?

javascript - YDN-DB - 如何通过 id 更新记录?

javascript - YDN DB 按 ID 删除记录 (IndexedDB)

javascript - 时间保存到数据库中为 00 :00:00

javascript - 如何在 IndexedDB 中创建具有多个条件的查询

javascript - 请帮助我防止 Bootstrap 单选按钮过早提交我的表单(使用 Javascript?)

json - 如何从数组中获取特定值?

javascript - 对于 JavaScript 自动完成搜索框,我们必须使用 "input"事件处理程序吗?

javascript - Chart js 多个标题