[英]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.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
搜索模型,該模型在索引時創建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.