node.js - 如何通过ElasticSearch Node Js中包含特定字符串的字段过滤数据?

标签 node.js elasticsearch

我有简单的Node Js应用程序。

我想通过路径字段获取过滤的数据,该字段包含“ get ”字。

例如我的数据如下:

"_source": {
    "time": "2020-03-12T01:25:41.61836-07:00",
    "level": "Info",
    "info": {
      "IpAddress": "0.0.0.0",
      "Path": "/api/test/getTest/1",
      "QueryString": "",
      "UserAgent": "",
      "LogDate": "2020-03-12T08:25:41.6220806Z",
      "Username": "cavidan.aliyev",
      "NodeId": "123456"
    }

换句话说,我的实体对象的结构如下:
{
   time,
        level,
        info: {
          IpAddress,
          Path,
          QueryString,
          UserAgent,
          LogDate,
          Username,
          NodeId
        }
}

我的查询如下:
 client.search({
                index: collectionName,
                body: { 
                    from: (params.currentPage - 1) * params.pageSize,
                    size: params.pageSize,
                    "query": {
                        "bool": {
                            "must": mustArr,
                            "filter": [ 
                                {
                                   "match_all": {}
                                }
                            ]
                        }
                    }
                }
            }, function (err, res) {
                if (err) { 
                    reject(err);
                }
                else { 
                    let result = res.hits.hits. map(x => x._source);
                    resolve(result);
                }
            });

如何通过路径字段过滤数据,该字段包含“ get ”字?

请帮助我,谢谢

最佳答案

您可以在过滤查询中使用Wildcard Query。我假设您在Standard Analyzer字段中使用 info.Path

请注意,为简单起见,我刚刚提到了filter查询中应该包含的内容。

如果info.Path nested ,请输入:

POST <your_index_name>/_search
{
  "query": {
    "bool": {
      "filter": {                        <--- Note this
        "nested": {
          "path": "info",
          "query": {
            "wildcard": {
              "info.Path": {
                "value": "*get*"
              }
            }
          }
        }
      }
    }
  }
}

如果info.Path object ,请输入:
POST <your_index_name>/_search
{
  "query": {
    "bool": {
      "filter": {                        <--- Note this
        "wildcard":{
          "info.Path": "*get*"
        }
      }
    }
  }
}

重要说明:通配符搜索会降低查询性能,并且如果您对Elasticsearch的索引有控制权,那么您绝对应该查看ngram搜索模型,该模型会在this链接中提到的索引时间创建n-gram token 。

让我知道这是否有帮助!

关于node.js - 如何通过ElasticSearch Node Js中包含特定字符串的字段过滤数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60650889/

相关文章:

node.js - 如何在 Sandstorm 上安装 Etherpad Lite 插件?

javascript - Mongoose - 将用户 _id 保存到不同的架构会更改 ID

javascript - 如何处理异步循环函数中的await调用-nodejs

ruby-on-rails - 提交Elasticsearch时如何在搜索框中保留搜索框输入

elasticsearch - Elasticsearch中的主/从设置

Node.js - 快速 - session

node.js - 如何在 Docker 中使用 process.argv 运行容器?

elasticsearch - 在提取管道中使用搜索模板

node.js - Elasticsearch 映射

python - 加快读取python中的url响应