zoukankan      html  css  js  c++  java
  • elasticsearch简单操作(一)

    1、增加记录

    例如1:向指定的 /Index/Type 发送 PUT 请求,就可以在 Index 里面新增一条记录。比如,向/accounts/person发送请求,就可以新增一条人员记录。

    curl -X POST  'localhost:9200/accounts/person/2' -d '{"user":"张三","title": "工程师","desc":"系统管理"}'
    

      系统提示如下错误:

    {"error":"Content-Type header [application/x-www-form-urlencoded] is not supported","status":406}
    

      

    需要制定插入数据格式:如下

    curl -H "Content-Type: application/json" -X POST  'localhost:9200/accounts/person/2' -d '{"user":"张三","title": "工程师","desc":"系统管理"}'

    系统提示:

    {"_index":"accounts","_type":"person","_id":"2","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}
    

      说明插入成功

    例如2:

    curl -H "Content-Type: application/json" -X PUT  'localhost:9200/accounts/person/4' -d '{"user":"李四","title": "软件工程师","desc":"系理集成"}'
    

      系统提示:

    {"_index":"accounts","_type":"person","_id":"4","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":1,"_primary_term":1}

    说明插入成功。

    可以使用PUT、POST插入数据,需要制定插入数据的数据格式,用 -H "Content-Type: application/json"制定为json格式。

    2、数据查询

    curl 'localhost:9200/accounts/person/4?pretty'

    系统返回:

    {
      "_index" : "accounts",
      "_type" : "person",
      "_id" : "4",
      "_version" : 1,
      "found" : true,
      "_source" : {
        "user" : "李四",
        "title" : "软件工程师",
        "desc" : "系理集成"
      }
    }

    说明查询成功。如果查询一个不存在id的记录,如命令:

    curl 'localhost:9200/accounts/person/5?pretty'

    id等于5的person因为没有对应的记录,所以,系统返回:

    {
      "_index" : "accounts",
      "_type" : "person",
      "_id" : "5",
      "found" : false
    }

    3、删除记录

    删除记录就是发出 DELETE 请求。

  • 相关阅读:
    utf8和utf8mb4的区别
    【JSP 标签】选择判断c:choose
    【JSP 标签】格式化日期
    【Spring 核心】AOP 面向切面编程
    【Spring 核心】高级装配
    【Spring 核心】装配bean(三)XML配置
    【Spring 核心】装配bean(二) JavaConfig装配
    【Spring 核心】装配Bean(一) 自动化装配
    【Js应用实例】图片预览
    【Js应用实例】限制上传图片大小
  • 原文地址:https://www.cnblogs.com/dyh004/p/8917121.html
Copyright © 2011-2022 走看看