代码之家  ›  专栏  ›  技术社区  ›  Nafis

如何通过密码查询在文本中查找字符串参数?

  •  0
  • Nafis  · 技术社区  · 7 年前

    我有一个数据库,我保存了一些 :Post :HashTag 节点。 职位 节点具有属性 id text . 哈萨克 节点具有属性 身份证件 tag (此处的标签是 HashTag )

    哈萨克 节点都是小写并以开头 # . 我想写一个查询,每当我创建新的 Post 包含当前 井号标签 在其中的数据库中,它会自动查找 井号标签 文本 性质 职位 节点并建立这样的关系:

    (:Post)<-[:TAG]-(:HashTag)
    

    例如,我正在创建 职位 : Today is #friday, ready for the #weekend

    我有 weekend 我的数据库中有hashtag,但没有 friday . 所以我有一个关系 TAG 在这之间 职位 周末 hashtag与之无关 星期五 井号标签。

    但是如果我有这个 职位 :

    Chrismats #holiday , and #traveling

    这两个我都有 #holiday #traveling 作为 哈萨克 在我的数据库里,所以我会有一个 标签 这两者之间的关系 职位 假日 一个 标签 这两者之间的关系 职位 #travelling

    知道如何编写这个查询吗?

    2 回复  |  直到 7 年前
        1
  •  1
  •   InverseFalcon    7 年前

    您需要在这里进行一些文本处理,首先使用 split() 围绕空白获取单词列表,只筛选以“”开头的单词,然后清除该单词(转换为小写并删除标点和非字母数字字符)。你需要 APOC Procedures 使用清洁功能

    您可以对列表进行展开(这样每个单词都将在自己的行中),使用给定的名称匹配:hashtag节点,并创建关系。

    ... // assume you just created post:Post with text, with post and text still in scope
    WITH post, [tag in split(text, ' ') WHERE tag STARTS WITH '#' | apoc.text.clean(tag)] as tagWords
    UNWIND tagWords as tagWord
    MATCH (tag:HashTag {tag:tagWord})
    CREATE (tag)-[:TAG]->(post)
    
        2
  •  0
  •   Nafis    7 年前

    答案就在这里 https://stackoverflow.com/a/54661113/9161843 当我按代码搜索解决方案时:

    function getHashTags(postText) {
      const regex = /#[^\s!$%^&*+.,£#]+/gm;
      const selectedHashTag = [];
      const subStr = postText.split(' ');
      const checkHashTag = _.filter(subStr, word => word.startsWith('#') || word.includes('#'));
    
      checkHashTag.map((hashTags) => {
        if (hashTags.match(regex)) {
          hashTags.match(regex).map(hashTag => selectedHashTag.push(hashTag.substr(1)));
        }
        return true;
      });
      return selectedHashTag;
    }