ElasticSearch + Kibana 展示业务数据

ElasticSearch + Kibana to display business data

所以我收集了过去几年的访客数据——超过 1400 万条记录。最重要的是,我有过去几年的表格数据。两者有共同的ID。

现在我正在尝试使用访问者数据学习 ElasticSearch + Kibana。数据相当简单,但格式不正确 - PHP 的 $_REQUEST 和 $_SERVER 数据。这是 Google 机器人访问的示例:

{u'Entrance Time': 1407551587.7385,
 u'domain': u'############',
 u'pages': {u'6818555600ccd9880bf7acef228c5d47': {u'REQUEST': [],
   u'SERVER': {u'DOCUMENT_ROOT': u'/var/www/####/',
    u'Entrance Time': 1407551587.7385,
    u'GATEWAY_INTERFACE': u'CGI/1.1',
    u'HTTP_ACCEPT': u'*/*',
    u'HTTP_ACCEPT_ENCODING': u'gzip,deflate',
    u'HTTP_CONNECTION': u'Keep-alive',
    u'HTTP_FROM': u'googlebot(at)googlebot.com',
    u'HTTP_HOST': u'############',
    u'HTTP_IF_MODIFIED_SINCE': u'Fri, 13 Jun 2014 20:26:33 GMT',
    u'HTTP_USER_AGENT': u'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
    u'PATH': u'/usr/local/bin:/usr/bin:/bin',
    u'PHP_SELF': u'/index.php',
    u'QUERY_STRING': u'',
    u'REDIRECT_SCRIPT_URI': u'http://############/',
    u'REDIRECT_SCRIPT_URL': u'############',
    u'REDIRECT_STATUS': u'200',
    u'REDIRECT_URL': u'############',
    u'REMOTE_ADDR': u'############',
    u'REMOTE_PORT': u'46271',
    u'REQUEST_METHOD': u'GET',
    u'REQUEST_TIME': u'1407551587',
    u'REQUEST_URI': u'############',
    u'SCRIPT_FILENAME': u'/var/www/PIAN/index.php',
    u'SCRIPT_NAME': u'/index.php',
    u'SCRIPT_URI': u'http://############/',
    u'SCRIPT_URL': u'/############/',
    u'SERVER_ADDR': u'############',
    u'SERVER_ADMIN': u'admin@############',
    u'SERVER_NAME': u'############',
    u'SERVER_PORT': u'80',
    u'SERVER_PROTOCOL': u'HTTP/1.1',
    u'SERVER_SIGNATURE': u'<address>Apache/2.2.22 (Ubuntu) Server at ############ Port 80</address>\n',
    u'SERVER_SOFTWARE': u'Apache/2.2.22 (Ubuntu)',
    u'uniqID': u'bbc398716f4703cfabd761cc8d4101a1'},
   u'SESSION': {u'Entrance Time': 1407551587.7385,
    u'uniqID': u'bbc398716f4703cfabd761cc8d4101a1'}}},
 u'uniqID': u'bbc398716f4703cfabd761cc8d4101a1'}

我使用 Python 包 elasticsearch.py 作为我的界面。我这样创建索引:

es.indices.create(
    index=Visit_to_ElasticSearch.INDEX,
    body={
        'settings': {
            'number_of_shards': 5,
            'number_of_replicas': 1,
        }
    },
    # ignore already existing index
    ignore=400
)

这是我的映射:

# Create mappings of a visit
time_date_mapping = { 'type': 'date_time' }
str_not_analyzed = { 'type': 'string'} # This used to include 'index': 'not_analyzed'

visit_mapping = {
    'properties': {
        'uniqID': str_not_analyzed,
        'pages': str_not_analyzed,
        'domain': str_not_analyzed,
        'Srvr IP': str_not_analyzed,
        'Visitor IP': str_not_analyzed,
        'Agent': { 'type': 'string', 'index': 'not_analyzed' },
        'Referrer': { 'type': 'string' },
        'Entrance Time': time_date_mapping,
        'Request Time': time_date_mapping,
        'Raw': { 'type': 'string', 'index': 'not_analyzed' },
        'Pages': { 'type': 'string', 'index': 'not_analyzed' },
    },
}

ES 报告的实际映射:

'visits': {
  'mappings': {
    'visit': {
      'properties': {
        'Agent': {'type': 'string'},
        'Entrance Time': {'format': 'dateOptionalTime', 'type': 'date'},
        'Pages': {'type': 'string'},
        'Raw': {
          'properties': {
            'Entrance Time': {'type': 'double'},
            'domain': {'type': 'string'},
            'uniqID': {'type': 'string'}
          }
        },
        'Referrer': {'type': 'string'},
        'Request Time': {'format': 'dateOptionalTime', 'type': 'date'},
        'Srvr IP': {'type': 'string'},
        'Visitor IP': {'type': 'string'},
        'domain': {'type': 'string'},
        'uniqID': {'type': 'string'}
      }
    }
  }
}

当我将试用数据转储到 ES 并在 Kibana4 中查看时出现问题。在“发现”选项卡中,它向我显示了前 5 个代理的 "Quick Count",其中包含完整字符串的截断版本。但是,当我使用聚合中的术语和字段中的 Agetn 创建可视化(可视化->饼图->从新搜索->拆分切片)时,我得到前 5 个单词列表 - 该列表是 mozilla,5.0 , 兼容, http, 2.0.

Kibana 警告我正在分析 Agent 字段,尽管我告诉它不要分析映射中的该字段。

我对此很陌生,我假设如果不分析 Agent 它将对完整的 Agent 字符串进行计数,我是不是不正确?用下划线替换空格并不能解决这个问题。那么我该如何解决呢?有没有办法将 Agent sting 放入 ES 中,使其仅作为一个整体考虑?

谢谢

可以在此 .

找到完整的映射代码

-------- cURL 后的映射--------

我用 curl --request PUT 'http://127.0.0.1:9200/visits/_mapping/visit?ignore_conflicts=true' --data '{"visit" : { "properties" : { "Agent" : { "type" : "string", "index" : "not_analyzed" } } } }' 改变了映射,这是新映射:

{
  "visits" : {
    "mappings" : {
      "visit" : {
        "properties" : {
          "Agent" : {
            "type" : "string",
            "norms" : {
              "enabled" : false
            }
          },
          "Entrance Time" : {
            "type" : "date",
            "format" : "dateOptionalTime"
          },
          "Pages" : {
            "type" : "string"
          },
          "Raw" : {
            "properties" : {
              "Entrance Time" : {
                "type" : "double"
              },
              "domain" : {
                "type" : "string"
              },
              "uniqID" : {
                "type" : "string"
              }
            }
          },
          "Referrer" : {
            "type" : "string"
          },
          "Request Time" : {
            "type" : "date",
            "format" : "dateOptionalTime"
          },
          "Srvr IP" : {
            "type" : "string"
          },
          "Visitor IP" : {
            "type" : "string"
          },
          "domain" : {
            "type" : "string"
          },
          "uniqID" : {
            "type" : "string"
          }
        }
      }
    }
  }
}

这与 是同一个问题,它不起作用的原因与映射 visit_mapping 从未通过 put_mapping 安装有关。因此,ES 根据 visit 文档中发送的内容创建了自己的映射。

要解决此问题,只需在 为您的第一个 visit 文档编制索引之前使用您的映射 调用 put_mapping