繁体   English   中英

如何按包含 ElasticSearch、Node Js 中特定字符串的字段过滤数据?

[英]How to filter data by field that contains specific string in ElasticSearch, Node Js?

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

我想通过Path字段获取过滤数据,其中包含“ 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 ”字样的Path字段过滤数据?

请帮帮我,谢谢

您可以在您拥有的过滤器查询中使用通配符查询 我假设您正在将Standard Analyzer用于info.Path字段。

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

如果info.Pathnested类型:

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

如果info.Pathobject类型:

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

重要说明:通配符搜索会降低查询性能,如果您可以控制 Elasticsearch 的索引,那么您绝对应该查看ngram搜索模型,该模型在索引时创建n-gram标记,如链接所述。

让我知道这是否有帮助!

如果您不想返回带有“get”关键字的数据,您的通配符应该输入到must_not 例如:

POST <your_index_name>/_search
 {
   "query": {
     "bool": {
       "must_not":{
          "filter": {                       
             "wildcard":{
               "info.Path": "*get*"
              }
           }
        }
     }
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM