【问题标题】:Apply mappings in ElasticSearch 7在 ElasticSearch 7 中应用映射
【发布时间】:2021-01-17 22:05:01
【问题描述】:

我已经使用 ElasticSearch 6 很长时间了,并设法为我最常用的字段应用了映射。最近,我创建了另一个集群 (v7.7),但由于 default 已被弃用,因此无法应用相同的映射。我的映射中有一些嵌套对象。当我用 _doc 替换 default 时,我可以应用映射,但是所有传入 ES 的请求都将被拒绝并进入 Logstash 死信队列。 (我正在使用 logstash 并将我的应用程序日志发送到 ElasticSearch。这是我的映射 json:

  "index_patterns": [
    "logstash-*"
  ],
  "settings": {
    "index": {
      "mapping": {
        "total_fields": {
          "limit": "50000"
        }
      },
      "refresh_interval": "5s"
    }
  },
  "mappings": {
    "_default_": {
      "_all": {
        "norms": false
      },
      "properties": {
        "@timestamp": {
          "type": "date"
        },
        "name": {
          "ignore_above": 64,
          "type": "keyword"
        },
        "logger": {
          "ignore_above": 64,
          "type": "keyword"
        },
        "hostname": {
          "ignore_above": 256,
          "type": "keyword"
        },
        "pid": {
          "type": "integer"
        },
        "level": {
          "type": "short"
        },
        "v": {
          "type": "short"
        },
        "env": {
          "ignore_above": 64,
          "type": "keyword"
        },
        "service": {
          "ignore_above": 256,
          "type": "keyword"
        },
        "message": {
          "type": "text"
        },
        "module": {
          "type": "keyword"
        },
        "subModule": {
          "type": "keyword"
        },
        "docker": {
          "properties": {
            "container_id": {
              "ignore_above": 512,
              "type": "keyword"
            },
            "container_name": {
              "ignore_above": 512,
              "type": "keyword"
            },
            "image_id": {
              "ignore_above": 512,
              "type": "keyword"
            },
            "image_name": {
              "ignore_above": 512,
              "type": "keyword"
            },
            "command": {
              "ignore_above": 512,
              "type": "keyword"
            },
            "tag": {
              "ignore_above": 512,
              "type": "keyword"
            },
            "created": {
              "type": "date"
            }
          }
        },
        "data": {
          "type": "text"
        },
        "context": {
          "properties": {
            "trade": {
              "properties": {
                "id": {
                  "ignore_above": 64,
                  "type": "keyword"
                }
              }
            },
            "trader": {
              "properties": {
                "id": {
                  "ignore_above": 64,
                  "type": "keyword"
                },
                "email": {
                  "ignore_above": 1024,
                  "type": "keyword"
                }
              }
            },
            "payment": {
              "properties": {
                "id": {
                  "ignore_above": 64,
                  "type": "keyword"
                }
              }
            },
            "err": {
              "properties": {
                "message": {
                  "type": "text"
                },
                "name": {
                  "ignore_above": 256,
                  "type": "keyword"
                },
                "stack": {
                  "type": "text"
                },
                "code": {
                  "ignore_above": 256,
                  "type": "keyword"
                },
                "file": {
                  "ignore_above": 256,
                  "type": "keyword"
                },
                "line": {
                  "type": "integer"
                },
                "cause": {
                  "properties": {
                    "message": {
                      "type": "text"
                    },
                    "name": {
                      "ignore_above": 256,
                      "type": "keyword"
                    },
                    "stack": {
                      "type": "text"
                    },
                    "code": {
                      "ignore_above": 256,
                      "type": "keyword"
                    },
                    "file": {
                      "ignore_above": 256,
                      "type": "keyword"
                    },
                    "line": {
                      "type": "integer"
                    },
                    "cause": {
                      "properties": {
                        "message": {
                          "type": "text"
                        },
                        "name": {
                          "ignore_above": 256,
                          "type": "keyword"
                        },
                        "stack": {
                          "type": "text"
                        },
                        "code": {
                          "ignore_above": 256,
                          "type": "keyword"
                        },
                        "file": {
                          "ignore_above": 256,
                          "type": "keyword"
                        },
                        "line": {
                          "type": "integer"
                        }
                      }
                    }
                  }
                }
              }
            },
            "req": {
              "properties": {
                "id": {
                  "ignore_above": 64,
                  "type": "keyword"
                },
                "remoteAddress": {
                  "type": "ip"
                },
                "remotePort": {
                  "type": "integer"
                },
                "method": {
                  "ignore_above": 32,
                  "type": "keyword"
                },
                "path": {
                  "type": "text"
                },
                "body": {
                  "type": "text"
                },
                "bodyLength": {
                  "type": "integer"
                },
                "headers": {
                  "type": "text"
                }
              }
            },
            "res": {
              "properties": {
                "statusCode": {
                  "type": "short"
                },
                "body": {
                  "type": "text"
                },
                "bodyLength": {
                  "type": "integer"
                },
                "headers": {
                  "type": "text"
                },
                "latency": {
                  "type": "integer"
                }
              }
            },
            "event": {
              "properties": {
                "eventName": {
                  "ignore_above": 256,
                  "type": "keyword"
                },
                "context": {
                  "type": "text"
                },
                "uuid": {
                  "ignore_above": 64,
                  "type": "keyword"
                },
                "time": {
                  "type": "long"
                },
                "attempts": {
                  "type": "short"
                }
              }
            },
            "task": {
              "properties": {
                "taskName": {
                  "ignore_above": 256,
                  "type": "keyword"
                },
                "context": {
                  "type": "text"
                },
                "uuid": {
                  "ignore_above": 64,
                  "type": "keyword"
                },
                "time": {
                  "type": "long"
                },
                "attempts": {
                  "type": "short"
                },
                "origin": {
                  "ignore_above": 128,
                  "type": "keyword"
                }
              }
            }
          }
        },
        "dlq": {
          "properties": {
            "eventFields": {
              "type": "text"
            },
            "reason": {
              "type": "text"
            }
          }
        },
        "tags": {
          "ignore_above": 1024,
          "type": "keyword"
        },
        "type": {
          "ignore_above": 1024,
          "type": "keyword"
        }
      }
    }
  }
}

我想知道如何将它应用到 elasticSearch7?

这里是 ElasticSearch 错误:

[o.e.a.a.i.m.p.TransportPutMappingAction] [0c85334cb2d41154383d0174502e13f2] failed to put mappings on indices [[[__PATH__]]], type [logs]
java.lang.IllegalArgumentException: Rejecting mapping update to [logstash-2020.10.02] as the final mapping would have more than 1 type: [_doc, logs]

【问题讨论】:

    标签: elasticsearch mapping logstash logstash-configuration


    【解决方案1】:

    @YLR 给出的答案几乎是正确的,但是他提供的映射中有一些解析错误。

    _all field 在 6.0 中已弃用

    _all 可能不再为在 6.0+ 中创建的索引启用,请使用自定义字段和映射 copy_to 参数

    请参阅此Elasticsearch documentation,了解更多信息。

    在 Elasticsearch 7.0.0 或更高版本中创建的索引不再接受 默认映射。在 6.x 中创建的索引将继续像以前在 Elasticsearch 6.x 中一样运行。类型在 7.0 的 API 中被弃用,使用 对索引创建、放置映射、获取映射、放置的重大更改 模板、获取模板和获取字段映射 API。

    修改后的映射为:

    {
      "index_patterns": [
        "logstash-*"
      ],
      "settings": {
        "index": {
          "mapping": {
            "total_fields": {
              "limit": "50000"
            }
          },
          "refresh_interval": "5s"
        }
      },
      "mappings": {
        "properties": {
          "@timestamp": {
            "type": "date"
          },
          "name": {
            "ignore_above": 64,
            "type": "keyword"
          },
          "logger": {
            "ignore_above": 64,
            "type": "keyword"
          },
          "hostname": {
            "ignore_above": 256,
            "type": "keyword"
          },
          "pid": {
            "type": "integer"
          },
          "level": {
            "type": "short"
          },
          "v": {
            "type": "short"
          },
          "env": {
            "ignore_above": 64,
            "type": "keyword"
          },
          "service": {
            "ignore_above": 256,
            "type": "keyword"
          },
          "message": {
            "type": "text"
          },
          "module": {
            "type": "keyword"
          },
          "subModule": {
            "type": "keyword"
          },
          "docker": {
            "properties": {
              "container_id": {
                "ignore_above": 512,
                "type": "keyword"
              },
              "container_name": {
                "ignore_above": 512,
                "type": "keyword"
              },
              "image_id": {
                "ignore_above": 512,
                "type": "keyword"
              },
              "image_name": {
                "ignore_above": 512,
                "type": "keyword"
              },
              "command": {
                "ignore_above": 512,
                "type": "keyword"
              },
              "tag": {
                "ignore_above": 512,
                "type": "keyword"
              },
              "created": {
                "type": "date"
              }
            }
          },
          "data": {
            "type": "text"
          },
          "context": {
            "properties": {
              "trade": {
                "properties": {
                  "id": {
                    "ignore_above": 64,
                    "type": "keyword"
                  }
                }
              },
              "trader": {
                "properties": {
                  "id": {
                    "ignore_above": 64,
                    "type": "keyword"
                  },
                  "email": {
                    "ignore_above": 1024,
                    "type": "keyword"
                  }
                }
              },
              "payment": {
                "properties": {
                  "id": {
                    "ignore_above": 64,
                    "type": "keyword"
                  }
                }
              },
              "err": {
                "properties": {
                  "message": {
                    "type": "text"
                  },
                  "name": {
                    "ignore_above": 256,
                    "type": "keyword"
                  },
                  "stack": {
                    "type": "text"
                  },
                  "code": {
                    "ignore_above": 256,
                    "type": "keyword"
                  },
                  "file": {
                    "ignore_above": 256,
                    "type": "keyword"
                  },
                  "line": {
                    "type": "integer"
                  },
                  "cause": {
                    "properties": {
                      "message": {
                        "type": "text"
                      },
                      "name": {
                        "ignore_above": 256,
                        "type": "keyword"
                      },
                      "stack": {
                        "type": "text"
                      },
                      "code": {
                        "ignore_above": 256,
                        "type": "keyword"
                      },
                      "file": {
                        "ignore_above": 256,
                        "type": "keyword"
                      },
                      "line": {
                        "type": "integer"
                      },
                      "cause": {
                        "properties": {
                          "message": {
                            "type": "text"
                          },
                          "name": {
                            "ignore_above": 256,
                            "type": "keyword"
                          },
                          "stack": {
                            "type": "text"
                          },
                          "code": {
                            "ignore_above": 256,
                            "type": "keyword"
                          },
                          "file": {
                            "ignore_above": 256,
                            "type": "keyword"
                          },
                          "line": {
                            "type": "integer"
                          }
                        }
                      }
                    }
                  }
                }
              },
              "req": {
                "properties": {
                  "id": {
                    "ignore_above": 64,
                    "type": "keyword"
                  },
                  "remoteAddress": {
                    "type": "ip"
                  },
                  "remotePort": {
                    "type": "integer"
                  },
                  "method": {
                    "ignore_above": 32,
                    "type": "keyword"
                  },
                  "path": {
                    "type": "text"
                  },
                  "body": {
                    "type": "text"
                  },
                  "bodyLength": {
                    "type": "integer"
                  },
                  "headers": {
                    "type": "text"
                  }
                }
              },
              "res": {
                "properties": {
                  "statusCode": {
                    "type": "short"
                  },
                  "body": {
                    "type": "text"
                  },
                  "bodyLength": {
                    "type": "integer"
                  },
                  "headers": {
                    "type": "text"
                  },
                  "latency": {
                    "type": "integer"
                  }
                }
              },
              "event": {
                "properties": {
                  "eventName": {
                    "ignore_above": 256,
                    "type": "keyword"
                  },
                  "context": {
                    "type": "text"
                  },
                  "uuid": {
                    "ignore_above": 64,
                    "type": "keyword"
                  },
                  "time": {
                    "type": "long"
                  },
                  "attempts": {
                    "type": "short"
                  }
                }
              },
              "task": {
                "properties": {
                  "taskName": {
                    "ignore_above": 256,
                    "type": "keyword"
                  },
                  "context": {
                    "type": "text"
                  },
                  "uuid": {
                    "ignore_above": 64,
                    "type": "keyword"
                  },
                  "time": {
                    "type": "long"
                  },
                  "attempts": {
                    "type": "short"
                  },
                  "origin": {
                    "ignore_above": 128,
                    "type": "keyword"
                  }
                }
              }
            }
          },
          "dlq": {
            "properties": {
              "eventFields": {
                "type": "text"
              },
              "reason": {
                "type": "text"
              }
            }
          },
          "tags": {
            "ignore_above": 1024,
            "type": "keyword"
          },
          "type": {
            "ignore_above": 1024,
            "type": "keyword"
          }
        }
      }
    }
    

    【讨论】:

    • @ahmokhtari 哪些是您在问题中提到的映射中的嵌套字段?
    • 感谢@Bhavya,但正如我所提到的,我已经尝试删除 default 但没有成功。正如您在模板中看到的,还有其他类型,例如“docker”、“event”、“task”,我不确定我应该如何处理它们?
    • @ahmokhtari 是 dockereventtask 字段是嵌套类型吗?我的意思是您是否希望这些字段是您在问题中提到的嵌套类型?
    • 嗯,它们不是嵌套的,但我可以说是我最常用的字段。我的问题是,如何修改它们以使此模板正常工作?
    • 我已经应用了您提供的模板@Bhavya,但这并不意味着 ElasticSearch 拒绝所有传入的请求。
    【解决方案2】:

    我找到了答案!

    Logstash 以 logs 类型将日志发送到 ElasticSearch。用日志替换 default 解决了这个问题。但是,要在 version7 中应用映射,您需要使用 PUT /_template/template_1?include_type_name=true

    【讨论】:

      【解决方案3】:

      您必须更新 v7 中不支持的某些字段。

      此请求是删除/更新不支持字段的示例:

      PUT _index_template/template_1 
      {
       "index_patterns": [
          "logstash-*"
        ],
          "template": {
      
        "settings": {
          "index": {
            "mapping": {
              "total_fields": {
                "limit": "50000"
              }
            },
            "refresh_interval": "5s"
          }
        },
        "mappings": {
      
            "properties": {
              "@timestamp": {
                "type": "date"
              },
              "name": {
                "ignore_above": 64,
                "type": "keyword"
              },
              "logger": {
                "ignore_above": 64,
                "type": "keyword"
              },
              "hostname": {
                "ignore_above": 256,
                "type": "keyword"
              },
              "pid": {
                "type": "integer"
              },
              "level": {
                "type": "short"
              },
              "v": {
                "type": "short"
              },
              "env": {
                "ignore_above": 64,
                "type": "keyword"
              },
              "service": {
                "ignore_above": 256,
                "type": "keyword"
              },
              "message": {
                "type": "text"
              },
              "module": {
                "type": "keyword"
              },
              "subModule": {
                "type": "keyword"
              },
              "docker": {
                "properties": {
                  "container_id": {
                    "ignore_above": 512,
                    "type": "keyword"
                  },
                  "container_name": {
                    "ignore_above": 512,
                    "type": "keyword"
                  },
                  "image_id": {
                    "ignore_above": 512,
                    "type": "keyword"
                  },
                  "image_name": {
                    "ignore_above": 512,
                    "type": "keyword"
                  },
                  "command": {
                    "ignore_above": 512,
                    "type": "keyword"
                  },
                  "tag": {
                    "ignore_above": 512,
                    "type": "keyword"
                  },
                  "created": {
                    "type": "date"
                  }
                }
              },
              "data": {
                "type": "text"
              },
              "context": {
                "properties": {
                  "trade": {
                    "properties": {
                      "id": {
                        "ignore_above": 64,
                        "type": "keyword"
                      }
                    }
                  },
                  "trader": {
                    "properties": {
                      "id": {
                        "ignore_above": 64,
                        "type": "keyword"
                      },
                      "email": {
                        "ignore_above": 1024,
                        "type": "keyword"
                      }
                    }
                  },
                  "payment": {
                    "properties": {
                      "id": {
                        "ignore_above": 64,
                        "type": "keyword"
                      }
                    }
                  },
                  "err": {
                    "properties": {
                      "message": {
                        "type": "text"
                      },
                      "name": {
                        "ignore_above": 256,
                        "type": "keyword"
                      },
                      "stack": {
                        "type": "text"
                      },
                      "code": {
                        "ignore_above": 256,
                        "type": "keyword"
                      },
                      "file": {
                        "ignore_above": 256,
                        "type": "keyword"
                      },
                      "line": {
                        "type": "integer"
                      },
                      "cause": {
                        "properties": {
                          "message": {
                            "type": "text"
                          },
                          "name": {
                            "ignore_above": 256,
                            "type": "keyword"
                          },
                          "stack": {
                            "type": "text"
                          },
                          "code": {
                            "ignore_above": 256,
                            "type": "keyword"
                          },
                          "file": {
                            "ignore_above": 256,
                            "type": "keyword"
                          },
                          "line": {
                            "type": "integer"
                          },
                          "cause": {
                            "properties": {
                              "message": {
                                "type": "text"
                              },
                              "name": {
                                "ignore_above": 256,
                                "type": "keyword"
                              },
                              "stack": {
                                "type": "text"
                              },
                              "code": {
                                "ignore_above": 256,
                                "type": "keyword"
                              },
                              "file": {
                                "ignore_above": 256,
                                "type": "keyword"
                              },
                              "line": {
                                "type": "integer"
                              }
                            }
                          }
                        }
                      }
                    }
                  },
                  "req": {
                    "properties": {
                      "id": {
                        "ignore_above": 64,
                        "type": "keyword"
                      },
                      "remoteAddress": {
                        "type": "ip"
                      },
                      "remotePort": {
                        "type": "integer"
                      },
                      "method": {
                        "ignore_above": 32,
                        "type": "keyword"
                      },
                      "path": {
                        "type": "text"
                      },
                      "body": {
                        "type": "text"
                      },
                      "bodyLength": {
                        "type": "integer"
                      },
                      "headers": {
                        "type": "text"
                      }
                    }
                  },
                  "res": {
                    "properties": {
                      "statusCode": {
                        "type": "short"
                      },
                      "body": {
                        "type": "text"
                      },
                      "bodyLength": {
                        "type": "integer"
                      },
                      "headers": {
                        "type": "text"
                      },
                      "latency": {
                        "type": "integer"
                      }
                    }
                  },
                  "event": {
                    "properties": {
                      "eventName": {
                        "ignore_above": 256,
                        "type": "keyword"
                      },
                      "context": {
                        "type": "text"
                      },
                      "uuid": {
                        "ignore_above": 64,
                        "type": "keyword"
                      },
                      "time": {
                        "type": "long"
                      },
                      "attempts": {
                        "type": "short"
                      }
                    }
                  },
                  "task": {
                    "properties": {
                      "taskName": {
                        "ignore_above": 256,
                        "type": "keyword"
                      },
                      "context": {
                        "type": "text"
                      },
                      "uuid": {
                        "ignore_above": 64,
                        "type": "keyword"
                      },
                      "time": {
                        "type": "long"
                      },
                      "attempts": {
                        "type": "short"
                      },
                      "origin": {
                        "ignore_above": 128,
                        "type": "keyword"
                      }
                    }
                  }
                }
              },
              "dlq": {
                "properties": {
                  "eventFields": {
                    "type": "text"
                  },
                  "reason": {
                    "type": "text"
                  }
                }
              },
              "tags": {
                "ignore_above": 1024,
                "type": "keyword"
              },
              "type": {
                "ignore_above": 1024,
                "type": "keyword"
              }
            }
        }
          }
      }
      }
      

      【讨论】:

      • 感谢 YLR,但正如我提到的,我已经尝试删除 default 但没有成功。正如您在模板中看到的,还有其他类型,例如“docker”、“event”、“task”,我不确定我应该如何处理它们?
      • 我猜,一种解决方案可能是通过“对象”(如 docker / event / task)放置 1 个索引,它看起来功能正确,但这取决于您的用例
      【解决方案4】:

      Elasticsearch 7.0 使用 php 7.4 版本。 TO在服务器中映射Elasticsearch 7.0中的数据库表

      <?php
          require 'includes/config.php';
          require 'includes/connection.php';
          require 'vendor/autoload.php';
      
          class SearchElastic {
              
              private $elasticclient = null;
              
              public function __construct(){
                  $db = new Connection();
                  $this->con = $db->connect();
                  //echo "<pre>";print_r($this->con); die;
                  
                  $elasticUrls = 'http://'.ELASTICSEARCH_HOST.':'.ELASTICSEARCH_PORT; 
                  $hosts = [$elasticUrls];                                 // SSL to localhost
                  
                  $this->elasticclient = Elasticsearch\ClientBuilder::create()->setHosts($hosts)->build();
                  //echo "<pre>"; print_r($this->elasticclient); die;
              }
              
              public function Mapping(){
                  $params = ['index' => 'employee'];
                  $response = $this->elasticclient->indices()->delete($params);
                  $params = [
                      'index' => 'employee',
                      'body' => [     
                          'products' => [             
                              'mappings' => [             
                                  'properties' => [
                                      'code' => [
                                          'type' => 'integer'
                                      ],
                                      'name' => [
                                          'type' => 'text'
                                      ],
                                      'created_at' => [
                                          'type' => 'text'
                                      ],
                                      'last_updated' => [
                                          'type' => 'text'
                                      ],
                                      'rank' => [
                                          'type' => 'integer'
                                      ],                      
                                  ]   
                              ]
                          ]
                      ]
                  ];
                  //echo "<pre>"; print_r($params); die;
                  $this->elasticclient->indices()->create($params);
                  //echo "<pre>"; print_r($var); die;
              }
              
              public function Search($query){
                  $client = $this->elasticclient;
                  $result = array();
      
                  $i = 0;
      
                  $params = [
                      'index' => 'employee',
                      'type'  => 'doc',
                      'body'  => [
                          'query' => [
                              'match' => ['name' => $query],
                          ],
                          'size' => 9,
                          'sort' => [
                              ['rank' =>  'desc'],
                          ],
                      ],
                  ];
                  
                  $query = $client->search($params);
                  //echo "<pre>"; print_r($query);die;
                  $hits  = sizeof($query['hits']['hits']);
                  $hit   = $query['hits']['hits'];
                  
                  $result['searchfound'] = $hits;
                  while ($i < $hits) {
      
                      $result['result'][$i] = $query['hits']['hits'][$i]['_source'];
      
                      $i++;
                  }
      
                  return  $result;
              }
              
              public function InsertData(){
                  $this->Mapping();   
                  $client = $this->elasticclient;
                  $stmt = "SELECT * FROM `table_name` where id=1";
                  $result = $this->con->query($stmt);
                  
                  $params = null;
                  while ($row = $result->fetch_assoc()){
                      $params['body'][] = array(
                        'index' => array(
                          '_index' => 'employee',
                          '_type'  => 'doc',
                          '_id'    => $row['id'],
                        ) ,
                      );
                      $params['body'][] = [
                          'id'           => $row['id'],
                          'name'         => $row['name'],
                          'created_at'   => $row['created_at'],
                          'last_updated' => $row['last_updated'],
                          'rank'         => $row['rank'],
                      ];          
                  }
                      
                  $responses = $client->bulk($params);
                  //echo "<pre>"; print_r($responses); die;
                  return true;
                  
              }
              
              public function UpdateData(){      
                  $client = $this->elasticclient;
                  $stmt = "SELECT * FROM `table_name` limit 1, 100";
                  $result = $this->con->query($stmt);
                  
                  $params = null;
                  while ($row = $result->fetch_assoc()){  
                      $params['body'][] = array(
                        'index' => array(
                          '_index' => 'employee',
                          '_type' => 'doc',
                          '_id' => $row['id'],
                        ) ,
                      );
                      $params['body'][] = [
                          'id'           => $row['id'],
                          'name'         => $row['name'],
                          'created_at'   => $row['created_at'],
                          'last_updated' => $row['last_updated'],
                          'rank'         => $row['rank'],
                      ];
                      
                  }
                  
                  $responses = $client->bulk($params);
                  //echo "<pre>"; print_r($responses); die;
                  return true;
              }
          }
      ?>
      

      【讨论】:

        猜你喜欢
        • 2019-09-27
        • 2021-10-19
        • 2012-07-11
        • 1970-01-01
        • 2022-01-03
        • 1970-01-01
        • 2019-05-18
        • 2016-02-21
        • 1970-01-01
        相关资源
        最近更新 更多