簡體   English   中英

為什么 elasticsearch 關鍵字搜索不起作用?

[英]why elasticsearch keyword search not working?

我使用 NLog 將日志消息寫入 Elasticsearch,索引結構在這里:

"mappings": {
    "logevent": {
        "properties": {
            "@timestamp": {
                "type": "date"
            },
            "MachineName": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "ignore_above": 256,
                        "type": "keyword"
                    }
                }
            },
            "level": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "ignore_above": 256,
                        "type": "keyword"
                    }
                }
            },
            "message": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "ignore_above": 256,
                        "type": "keyword"
                    }
                }
            }
        }
    }
}

我能夠使用文本搜索獲得結果:

GET /webapi-2022.07.28/_search
{
  "query": {
    "match": {
      "message": "ERROR" 
    }
  }
}


結果“命中”:[{“_index”:“webapi-2022.07.28”,“_type”:“logevent”,“_id”:“IFhYQoIBRhF4cR9wr-ja”,“_score”:4.931916,“_source”:{“@時間戳”:“2022-07-28T01:07:58.8822339Z”,“級別”:“錯誤”,“消息”:“”“2022-07-28 09:07:58.8822|錯誤|AppSrv.Filter.AccountAuthorizeAttribute| [KO17111808]-[172.10.2.200]-[ERROR]-"message"""", "MachineName": "WIN-EPISTFOBD41" } } //..... ]

但是當我使用關鍵字時,我什么也得不到:

GET /webapi-2022.07.28/_search
{
  "query": {
    "term": {
      "message.keyword": "ERROR"
    }
  }
}

我嘗試了術語和匹配,結果是一樣的。

這是因為消息字段不僅包含ERROR ,而且在.keyword字段中還有其他字符串,您需要僅在您的情況下使用text搜索,您只能在精確搜索的情況下使用.keyword字段。

如果您的message字段僅包含ERROR字符串,而不是僅在.keyword上搜索會產生結果,您可以通過索引示例文檔來自己測試它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM