我有简单的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/