elasticseach 增删改查 ,包括各种组合查询(全)

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Elasticsearch是一个开源的分布式搜索和分析引擎,可以用于实时将物流快递单等业务数据增量同步至Elasticsearch索引中。它被广泛应用于各种场景,包括日志分析、文搜索、数据可视化等。根据引用中的信息,李猛是一位对Elasticsearch有深入体验的工程师,他对Elastic-Stack的开发、架构、运维等方面有丰富的经验,并且提供咨询、培训和调优实施等服务。 如果您想使用Elasticsearch,您需要启动Elasticsearch服务器和Kibana。启动Elasticsearch服务器可以使用版本2.1或更高版本。同时,您还可以使用Kibana来进行数据可视化和仪表板的创建。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [一文快速了解Elastic Search 开源搜索引擎(技术选型+启动命令)](https://blog.csdn.net/xianyu120/article/details/117518586)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [ES既是搜索引擎又是数据库?真的有那么能吗?](https://blog.csdn.net/w397090770/article/details/108934868)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值