如何在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);
}
});

如何通过Path字段过滤数据,该字段包含">get"字?

请帮帮我,谢谢

您可以在现有的过滤器查询中使用通配符查询。我假设您将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*"
}
}
}
}
}
}

最新更新