【问题标题】:how to setup this simple Elasticsearch Mapping in Elasticsearch-PHP?如何在 Elasticsearch-PHP 中设置这个简单的 Elasticsearch 映射?
【发布时间】:2018-04-01 11:42:30
【问题描述】:

问题末尾给出了一个终端命令,显示了一个简单的 Elasticsearch 映射。我需要使用 Elasticsearch-PHP 为索引设置这种映射。我需要在索引数据时执行此操作。

我知道如何在 Elasticsearch-PHP 中建立索引。会是这样的

for($i = 0; $i < 100; $i++) {
    $params['body'][] = [
        'index' => [
            '_index' => 'my_index',
            '_type' => 'my_type',
        ]
    ];

    $params['body'][] = [
        'my_field' => 'my_value',
        'second_field' => 'some more values'
    ];
}

$responses = $client->bulk($params);

我的问题是,我将如何设置一个 Mapping,对应于下面给出的特定 Mapping 以 elasticsearch-PHP 格式(我相信它会成为一个关联数组,但我不确定更多细节)?


这是示例 ES 映射,我想将其转换为 PHP 中使用的格式:

PUT _template/packets
{
  "template": "packets-*",
  "mappings": {
    "pcap_file": {
      "dynamic": "false",
      "properties": {
        "timestamp": {
          "type": "date"
        },
        "layers": {
          "properties": {
            "ip": {
              "properties": {
                "ip_ip_src": {
                  "type": "ip"
                },
                "ip_ip_dst": {
                  "type": "ip"
                }
              }
            }
          }
        }
      }
    }
  }
}

【问题讨论】:

    标签: php elasticsearch elasticsearch-5


    【解决方案1】:

    如果您不更新映射 - 您不必在每次将数据重新索引到 elasticsearch 时都进行 put 映射。但如果你这样做了,你可以使用新名称创建索引:

    $put = [
        'mappings' => [
            // your mapping here
        ],
    ];
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://yourHost:9200/yourIndex');
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($put));
    curl_exec($ch);
    

    或者你可以使用elasticsearch包:

    $params = [
        'index' => 'yourIndex',
        'body' => [
            'mappings' => [
                // your mapping here
            ]
        ]
    ];
    $response = $client->indices()->create($params);
    

    【讨论】:

    • 嘿,谢谢;你能告诉我如何写一个映射(在 PHP 中)对应于这里显示的映射:pastebin.com/H7QEWhNN
    • 也就是说,我将如何填充您在上面的代码中分配给'mappings' 的数组?
    猜你喜欢
    • 2014-11-27
    • 2017-02-23
    • 1970-01-01
    • 2019-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-11
    相关资源
    最近更新 更多