【问题标题】:How filter search in elasticlunr.js如何在 elasticlunr.js 中过滤搜索
【发布时间】:2019-05-15 04:29:06
【问题描述】:

我正在尝试搜索由@gatsby-contrib/elasticlunr 生成的索引,生成的索引如下所示:

    {
     "version": "0.9.5",
     "fields": ["title", "path", "section"],
     "ref": "id",
     "documentStore": {
        "docs": {
            "fe565569-77a5-566d-bb47-96a6094c22c5": {
                "id": "fe565569-77a5-566d-bb47-96a6094c22c5",
                "title": "schema",
                "path": "/graphql/schema",
                "section": "graphql"
            },
            "cd1cdd40-4bb7-5ff6-9908-6c9ad692e75c": {
                "id": "cd1cdd40-4bb7-5ff6-9908-6c9ad692e75c",
                "title": "Component",
                "path": "/react/component",
                "section": "react"
            },
            "c1aecadb-3d1e-5d49-87f3-2b6f2c73433c": {
                "id": "c1aecadb-3d1e-5d49-87f3-2b6f2c73433c",
                "title": "Component",
                "path": "/react/component2",
                "section": "react"
            },
            "07159f12-dafb-53f6-b1ad-5032d56d25bb": {
                "id": "07159f12-dafb-53f6-b1ad-5032d56d25bb",
                "title": "Lazy",
                "path": "/react/suspense",
                "section": "react"
            },
            "380309db-ffa1-5f24-a192-36ac36b90a06": {
                "id": "380309db-ffa1-5f24-a192-36ac36b90a06",
                "title": "suspense",
                "path": "/react/lazy",
                "section": "react"
            },
           "380309db-ffa1-5f24-a192-36ac36b90uuuu": {
                "id": "380309db-ffa1-5f24-a192-36ac36b9uuuu",
                "title": "super",
                "path": "/graphql/super",
                "section": "graphql"
            }
        }
    }
   .....
}

有没有一种方法可以让我只获得 section == react 的结果,而不是所有在字段方面与搜索查询匹配的文档。

例如 当我在配置中使用expand: true 搜索术语su 并设置过滤器section = 'graphql' 它应该返回:

      "380309db-ffa1-5f24-a192-36ac36b90uuuu": {
            "id": "380309db-ffa1-5f24-a192-36ac36b9uuuu",
            "title": "super",
            "path": "/graphql/super",
            "section": "graphql"
       }

但我目前得到的是:

{
    "380309db-ffa1-5f24-a192-36ac36b90a06": {
        "id": "380309db-ffa1-5f24-a192-36ac36b90a06",
        "title": "suspense",
        "path": "/react/lazy",
        "section": "react"
    },
    "380309db-ffa1-5f24-a192-36ac36b90uuuu": {
        "id": "380309db-ffa1-5f24-a192-36ac36b9uuuu",
        "title": "super",
        "path": "/graphql/super",
        "section": "graphql"
    }
}

谢谢!

【问题讨论】:

    标签: javascript gatsby lunrjs


    【解决方案1】:

    您不能使用 elasticlunr.js 进行这样的过滤。这是一个轻量级的全文搜索引擎。

    还有elasticlunr不支持正则表达式

    或者,您可以再次使用循环过滤结果。

    var finalResult ={};
        for (var key in label) {
            var result = label[key];
            // graphql
            if (result.doc.section === 'graphql' && !!result.doc.title.match(/su/g)) {
                finalResult[key] = result;
            }
            console.log(finalResult);
        }
        // output 
        console.log(finalResult);
    

    谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-07
      • 2019-02-12
      相关资源
      最近更新 更多