ElasticSearch / Kibana:如何汇总相似类型的页面

时间:2020-07-16 04:25:51

标签: elasticsearch kibana

全新的kibana用户,我正在尝试跟踪网站上的使用情况。

我有一些页面的网址类型如下:

https://example.com/services/{serviceId}/users

因此,每项服务的serviceId都会更改,但是我想看看有多少人会转到其服务的/users页。

是否可以汇总该计数?

1 个答案:

答案 0 :(得分:0)

假设url为multi-field类型,则具有以下映射:

PUT my_url_index
{
  "mappings" : {
    "properties" : {
      "url" : {
        "type" : "text",
        "fields" : {
          "keyword" : {
            "type" : "keyword",
            "ignore_above" : 256
          }
        }
      }
    }
  }
}

示例文档:

POST my_url_index/_doc/1
{
  "url": "https://example.com/services/101/users"
}

POST my_url_index/_doc/2
{
  "url": "https://example.com/services/asdasd/users"
}

POST my_url_index/_doc/3
{
  "url": "https://example.com/services/555/users"
}

POST my_url_index/_doc/4
{
  "url": "https://example.com/users/4444/create"
}

请注意,我假设您只有一个URL变体,其中同时包含servicesuser令牌。

以下查询可以帮助您获得总计数以及每个用户去过多少次的计数:

POST my_url_index/_search
{
  "size": 0,
  "query": {
    "query_string": {
      "query": "(services) AND (users)",
      "fields": ["url"]
    }
  },
  "aggs": {
    "my_users": {
      "terms": {                          <----- Note the logic mentioned here
        "script": {
          "source": "doc['url.keyword'].toString().substring(30, doc['url.keyword'].toString().indexOf('/users'));"
        }, 
        "size": 10
      }
    }
  }
}

响应:

{
  "took" : 336,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,                             <---- total count 
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {                           <----- count of each user
    "my_users" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "101",
          "doc_count" : 1
        },
        {
          "key" : "555",
          "doc_count" : 1
        },
        {
          "key" : "asdasd",
          "doc_count" : 1
        }
      ]
    }
  }
}

让我知道这是否有帮助,以及您是否在寻找其他任何东西。