【问题标题】:Using a Kibana view query from application使用来自应用程序的 Kibana 视图查询
【发布时间】:2020-07-24 05:55:50
【问题描述】:

我使用了以下过滤器,然后使用 Lucene 搜索查询字符串以获得我正在寻找的视图。

{
  "query": {
    "match": {
      "eventSource": {
        "query": "ec2.amazonaws.com",
        "type": "phrase"
      }
    }
  }
}

我不想返回以单词 describe 或 get 开头的事件名称。应该返回来自 ec2 事件源的其余事件名称。

!(eventName.keyword:Describe* OR eventName.keyword: 获取*)

问题是如何将这两个搜索请求合二为一? 我需要从我的应用程序中使用该查询。


更新:

Kibana Discover 选项卡的 Inspect 菜单会生成此查询。我只是想用布尔 OR 子句用通常的 match 或 match_phrase 重写 query_string 部分。

  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "query": "!(eventName.keyword: Describe* OR  eventName.keyword: Get* )",
            "analyze_wildcard": true
          }
        },
        {
          "match_phrase": {
            "eventSource": {
              "query": "ec2.amazonaws.com"
            }
          }
        },
        {
          "range": {
            "@timestamp": {
              "format": "strict_date_optional_time",
              "gte": "2020-07-09T08:39:15.947Z",
              "lte": "2020-07-24T08:39:15.947Z"
            }
          }
        }
      ],
      "filter": [],
      "should": [],
      "must_not": []
    }
  }

【问题讨论】:

    标签: elasticsearch kibana


    【解决方案1】:

    您可以轻松地使用boolean query 的must_not 子句来排除您不希望在搜索结果中出现的文档,并且您可以添加任意数量的must_not,这很容易做到并且可以在单个查询中完成。

    请参阅同一链接中的示例以获取更多信息。在我的本地创建示例以显示您的正确查询,请注意,我使用的是prefix query,而不是通配符,这更好并为您的用例提供服务。

    创建索引映射

    {
        "mappings": {
            "properties": {
                "eventName": {
                    "type": "keyword"
                }
            }
        }
    }
    

    索引示例文档

    {
      "eventName" : "Describe the events"
    }
    
    {
      "eventName" : "the Describe events"
    }
    
    {
      "eventName" : "Get the event"
    }
    
    {
      "eventName" : "event Get"
    }
    

    现在根据您的要求搜索查询以仅获取 2 和 3 文档

    {
      "query": {
        "bool": {
          "must_not": [
            {
              "prefix": {
                "eventName": "Desc"
              }
            },
             {
              "prefix": {
                "eventName": "Get"
              }
            }
          ]
        }
      }
    }
    

    搜索结果

      "hits": [
          {
            "_index": "ngramkey",
            "_type": "_doc",
            "_id": "2",
            "_score": 0.0,
            "_source": {
              "eventName": "the Describe events"
            }
          },
          {
            "_index": "ngramkey",
            "_type": "_doc",
            "_id": "4",
            "_score": 0.0,
            "_source": {
              "eventName": "event Get"
            }
          }
        ]
    

    【讨论】:

      【解决方案2】:

      根据用户“Opster Elasticsearch Ninja”的建议,我已经合并了不能像这样的布尔查询......

      {
        "query": {
          "bool": {
            "must": [
              {
                "bool": {
                  "must_not": [
                    {
                      "prefix": {
                        "eventName.keyword": "Desc"
                      }
                    },
                    {
                      "prefix": {
                        "eventName.keyword": "Get"
                      }
                    }
                  ]
                }
              },
              {
                "match_phrase": {
                  "eventSource": {
                    "query": "ec2.amazonaws.com"
                  }
                }
              },
              {
                "range": {
                  "@timestamp": {
                    "format": "strict_date_optional_time",
                    "gte": "2020-07-09T08:39:15.947Z",
                    "lte": "2020-07-24T08:39:15.947Z"
                  }
                }
              }
            ],
            "filter": [],
            "should": [],
            "must_not": []
          }
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2015-03-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多