简体   繁体   English

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

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

I have simple Node Js application.我有简单的 Node Js 应用程序。

I want get filtered data by Path field, that contains ' get ' word.我想通过Path字段获取过滤数据,其中包含“ get ”字样。

For example my data is like below:例如我的数据如下:

"_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"
    }

In other words my entity object's structure like as below:换句话说,我的实体对象的结构如下所示:

{
   time,
        level,
        info: {
          IpAddress,
          Path,
          QueryString,
          UserAgent,
          LogDate,
          Username,
          NodeId
        }
}

My query is like below:我的查询如下:

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

How I can filter data by Path field, that contains ' get ' word?如何按包含“ get ”字样的Path字段过滤数据?

Please help me, thanks请帮帮我,谢谢

You can make use of Wildcard Query inside the filter query you have.您可以在您拥有的过滤器查询中使用通配符查询 I'm assuming that you are making use of Standard Analyzer for info.Path field.我假设您正在将Standard Analyzer用于info.Path字段。

Note that for the sake of simplicity I've just mentioned what should be going inside the filter query you have.请注意,为了简单起见,我刚刚提到了您拥有的filter查询中应该包含的内容。

If info.Path is nested type:如果info.Pathnested类型:

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

If info.Path is object type:如果info.Pathobject类型:

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

Important Note: Wildcard search slows the query performance, and if you have a control on the Elasticsearch's index, then you should definitely look at ngram search model, which creates n-gram tokens at index-time as mentioned in this link.重要说明:通配符搜索会降低查询性能,如果您可以控制 Elasticsearch 的索引,那么您绝对应该查看ngram搜索模型,该模型在索引时创建n-gram标记,如链接所述。

Let me know if this helps!让我知道这是否有帮助!

If you don't want returned data with "get" keywords, your wildcard should type into the must_not .如果您不想返回带有“get”关键字的数据,您的通配符应该输入到must_not For example:例如:

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