1.创建索引 见官方文档:索引文档 | Elasticsearch: 权威指南 | Elastic
1.1单个创建
PUT /{index}/{type}/{id}
{
"field": "value",
...
}
案例 website表示 索引的名字 bllog 表示 索引下面的分类 , 123 表示索引的id ,举例 比如 一个点上系统里面的产品 ,分类有 电子产品 ,我们就可以 索引的名字叫 product,
type 叫electronic
PUT /website/blog/123
{
"title": "My first blog entry",
"text": "Just trying this out...",
"date": "2014/01/01"
}
1.2在指定的索引和type 下面创建
比如在index user ,type api 下面创建 id 为 345 的索引
POST /user/api/345 HTTP/1.1
Host: localhost:9200
Content-Type: application/json
Content-Length: 73
{"age":15,"username":"huang","gender":"man","desc":"i am a strong man"}
插入成功后,我们用id取回
1.3批量创建
使用postman
POST /_bulk HTTP/1.1
Host: localhost:9200
Content-Type: application/json
Content-Length: 538
{ "index" : { "_index" : "product","_type":"number", "_id" : "40372399b5596341bb5b63f3bf09c502" } }
{ "seri_number" : "1100614626" }
{ "index" : { "_index" : "product","_type":"number", "_id" : "a05277598f75d66a900ab458701adbc7" } }
{ "seri_number" : "549197690" }
{ "index" : { "_index" : "product","_type":"number", "_id" : "6d88f094bf4e4b31984b7ad8bd32bdf7" } }
{ "seri_number" : "514706614" }
{ "index" : { "_index" : "product","_type":"number", "_id" : "71b74e4624b622d6f5053514344cbc87" } }
{ "seri_number" : "1778533480" }
注意数据最后一定要加多一个回车换行,不然会提示失败
{
"error": {
"root_cause": [
{
"type": "illegal_argument_exception",
"reason": "The bulk request must be terminated by a newline [\\n]"
}
],
"type": "illegal_argument_exception",
"reason": "The bulk request must be terminated by a newline [\\n]"
},
"status": 400
}
php 调用curl 批量插入索引代码
<?php
//elastic批量插入
ini_set('memory_limit','1024M');
function getRandHanzi($num){
$result = '';
$hanziSource = '丰王井开夫天元无云专扎艺木五支厅不太犬区历友尤匹车巨牙屯比互切瓦止少日中贝内水冈见手午牛毛气升长仁什片仆化仇币仍仅斤爪反介父从今凶分乏公仓月氏勿风欠丹匀乌勾凤六文方火为斗忆计订户认心尺引丑巴孔队办以允予劝双书幻玉刊末未示击打巧正扑扒功扔去甘世古节本术可丙左厉石右布龙平灭轧东卡北占业旧帅归目旦且叮叶甲申号电田由只央史兄叼叫叨另叹四生失禾丘付仗代仙们仪白仔他斥瓜乎丛令用甩印乐句匆册犯外处冬鸟务包饥主市立闪兰半汁汇头汉宁穴它讨写让礼训必议讯记永司尼民出辽奶奴加召皮边孕发圣对台矛纠母幼丝式刑动扛寺吉扣考托老圾巩执扩扫地扬场耳共芒亚芝朽朴机权过臣再协西压厌在百有存而页匠夸夺灰达列死成夹轨邪划迈毕至此贞师尘尖劣光当早吐吓虫曲团同吊吃因吸吗屿帆岁回岂则刚网肉年朱先丢舌竹迁乔伟传乒乓休伍伏优伐延件任伤价份华仰仿伙伪自血向似后行舟全会杀合兆企众爷伞创肌朵杂危旬旨负各名多争色壮冲冰庄庆亦刘齐交次衣产决充妄闭问闯羊并关米灯州汗污江池汤忙兴宇守宅字安讲军许论农讽设访寻那迅尽导异孙阵阳收阶阴防奸如妇好她妈戏羽观欢买红纤约级纪驰巡寿弄麦形进戒吞远违运扶抚坛技坏扰拒找批扯址走抄坝贡攻赤折抓扮抢孝均抛投坟坑抗坊抖护壳志块扭声把报却劫芽花芹芬苍芳严芦劳克苏杆杜杠材村杏极李杨求更束豆两丽医辰励否还歼来连步坚旱盯呈时吴助县里呆园旷围呀吨足邮男困吵串员听吩吹呜吼吧别岗帐财钉针告我乱利秃秀私每兵估体何但伸作伯伶佣低你住位伴身皂佛近彻役返余希坐谷妥含邻岔肝肚肠龟免狂犹角删条卵岛迎饭饮系言冻状亩况床库疗应冷这序辛弃冶忘闲间闷判灶灿弟汪沙汽沃泛沟没沈沉怀忧快完宋宏牢究穷灾良证启评补初社识诉诊词译君灵即层尿尾迟局改张忌际陆阿陈阻附妙妖妨努忍劲鸡驱纯纱纲纳纵驳纷纸纹纺驴纽';
$hanziSourceLen = strlen($hanziSource);
for($i=0;$i<$num;$i++){
$starLoc = 3* rand(0,$hanziSourceLen/3);
$tempHanzi = $hanziSource[$starLoc].$hanziSource[$starLoc+1].$hanziSource[$starLoc+2];
$result.=$tempHanzi;
}
return $result;
}
function randLetter($num){
$str = 'abcdefghijklmnopqrstuvwxyz';
$result = '';
for($i=0;$i<($num);$i++){
$result .= $str[rand(0,26)];
}
return $result;
}
for($j=0;$j<1000;$j++){
$curl = curl_init();
$template = '{ "index" : { "_index" : "product","_type":"number", "_id" : "#id" } }
{ "seri_number" : "#number" ,"username":"#username"}'."\n";
$stime = time();
$putStr ='';
for($i=0;$i<100000;$i++){
$id = md5(time().microtime().rand(1,1000000000));
$number = getRandHanzi(10);
$indexInsert = str_replace('#id',$id,$template);
$indexInsert = str_replace('#number',$number,$indexInsert);
$indexInsert = str_replace('#username',randLetter(10),$indexInsert);
if($i==0 || $i ==1){
echo $indexInsert;
}
$putStr.= $indexInsert;
}
curl_setopt_array($curl, array(
CURLOPT_URL => 'http://localhost:9200/_bulk',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $putStr,
CURLOPT_HTTPHEADER => array(
'Cont