初探elasticsearch

在这里插入图片描述
在这里插入图片描述
其实elasticsearch权威指南这本书有点坑,还是建议大家看官网,官网如下图片实例中
在这里插入图片描述

可以拷贝为curl,就可以看到他的添加索引数据的命令
在这里插入图片描述

curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{
    "first_name" : "John",
    "last_name" :  "Smith",
    "age" :        25,
    "about" :      "I love to go rock climbing",
    "interests": [ "sports", "music" ]
}
'

运行结果如下
在这里插入图片描述

curl -X DELETE "localhost:9200/customer?pretty"
curl -X GET "localhost:9200/_cat/indices?v"

在这里插入图片描述
在这里插入图片描述

curl -X POST "localhost:9200/customer/_update/1?pretty" -H 'Content-Type: application/json' -d'
{
    "doc":{"first_name" : "Jane"}
}
'

curl -X POST "localhost:9200/customer/_update/1?pretty" -H 'Content-Type: application/json' -d'
{
    "doc":{"first_name" : "Jane","age":20}
}
'
curl -X POST "localhost:9200/customer/_update/1?pretty" -H 'Content-Type: application/json' -d'
{
    "script":"ctx._source.age+=5"                   
}
'

ctx._source指的是讲要更新的当前原文档
在这里插入图片描述

删除索引为2的文档:

curl -X DELETE "localhost:9200/customer/_doc/2?pretty"

在这里插入图片描述

批量更新:

[hp@localhost ~]$ curl -X POST "localhost:9200/customer/_bulk?pretty" -H 'Content-Type:application/json' -d'
{"index":{"_id":"1"}}
{"name":"John"}
{"index":{"_id":"2"}}
{"name":"Jong"}
'
{
  "took" : 50,
  "errors" : false,
  "items" : [
    {
      "index" : {
        "_index" : "customer",
        "_type" : "_doc",
        "_id" : "1",
        "_version" : 5,
        "result" : "updated",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 6,
        "_primary_term" : 1,
        "status" : 200
      }
    },
    {
      "index" : {
        "_index" : "customer",
        "_type" : "_doc",
        "_id" : "2",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 7,
        "_primary_term" : 1,
        "status" : 201
      }
    }
  ]
}
[hp@localhost ~]$ 

在这里插入图片描述

[hp@localhost bin]$ curl -X POST "localhost:9200/customer/_bulk?pretty" -H 'Content-Type:application/json' -d'
{"update":{"_id":"1"}}
{"doc":{"name":"John become Jane"}}
{"delete":{"_id":"2"}}
'
{
  "took" : 23,
  "errors" : false,
  "items" : [
    {
      "update" : {
        "_index" : "customer",
        "_type" : "_doc",
        "_id" : "1",
        "_version" : 7,
        "result" : "updated",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 10,
        "_primary_term" : 2,
        "status" : 200
      }
    },
    {
      "delete" : {
        "_index" : "customer",
        "_type" : "_doc",
        "_id" : "2",
        "_version" : 3,
        "result" : "deleted",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 11,
        "_primary_term" : 2,
        "status" : 200
      }
    }
  ]
}

在这里插入图片描述

curl -XPOST "localhost:9200/_doc/brank/1?pretty" -H'Content-Type:application/json' '
{
"account_number": 0,
"balance": 16623,
"firstname": "Bradshaw",
"lastname": "Mckenzie",
"age": 29,
"gender": "F",
"address": "244 Columbns Place",
"employer": "Euron",
"email": "bradshawmckenzie@euron.com",
"city": "Hobucken",
"state": "CO"
}
'

两者的命令一样,但是由于复制粘贴过来的json,导致有未知字符,所以建议不要粘贴json串

curl -X PUT "localhost:9200/brank/_doc/1?pretty" -H 'Content-Type:application/json' -d '
> {
> "account_number": 0,
> "balance": 16623,
> "firstname": "Bradshaw",
> "lastname": "Mckenzie",
> "age": 29,
> "gender": "F",
> "address": "244",
> "employer": "Euron",
> "email": "brandshw@com",
> "city": "Hobocke",
> "state": "CO"
> }'

在这里插入图片描述
在批量添加数据的使用,使用json文件导入出现如下错误
在这里插入图片描述
这是因为自己写的json文件里面的格式不对,所以还是需要官方的文件,
accounts.json

点击下载就可
重新运行命令就成功了

curl -H "Content-Type: application/x-ndjson" -XPOST "localhost:9200/bank/_bulk?pretty&refresh" --data-binary "@accounts.json"

当然如下的命令也可

curl -H "Content-Type: application/json" -XPOST "localhost:9200/bank/_bulk?pretty&refresh" --data-binary "@accounts.json"

在这里插入图片描述

下面来进行简单的类似于get请求,在uri上加参数的搜索

curl -XGET "localhost:9200/bank/_search?q=*&sort=account_number:asc&pretty"

在这里插入图片描述
解释一下每个参数的含义
bank指的时索引,不用多说
q=*,这个指的是es匹配指定索引中的所有文档,
sort=account_number:asc这个意思是按照每个文档的account_number字段按照升序对结果进行排序
第二行,took表示es执行搜索所用的时间,单位是ms
第三行,time_out 用来指示搜索是否超时
第四行,_shards指示搜索了多少分片,以及搜索成功和失败的分片的计数
第十行 hits是用来实际搜索结果集
第十一行,hits.total 是包含与搜索条件匹配的文档总数信息的对象
第十二行 hits.total.value 表示总命中计数的值

缺一个总结:

curl -XGET "localhost:9200/bank/_search?pretty" '
{
"query": {"match_all":{}},
"sort": [
{"account_number":"asc"}
]
}'

在这里插入图片描述
这里的区别已经很明显了,就是不在uri中拼参数,而是向search API提供一个json风格的查询请求提

很多人开始想要使用postman进行对es请求的时候会出现以下一个问题
elastic Error: connect ECONNREFUSED 192.168.109.11:9200

这种情况只可能出现在一个主机向另一个主机上的es发送请求这种情况
我的是win系统向虚拟机上的linux进行请求,win ping的通 linux但是就是无法访问,如上我给出的链接,里面的解决方法我用的是最后一个修改配置的方法,不过没有全改。改动如下
network.host: 192.168.189.129
http.port: 9200
node.name: node-1
node.master: true
cluster.initial_master_nodes: [“node-1”]

本来host 中localhost就是在linux上就是这个地址,但是你从外界请求可能会有其他状况,port应该不用改,然后后面三个配置的意思是,第三个集群初始就是node-1,第一个node-1就是这个主机,然后第二个,并且这个节点还是主节点,这是在没有配置集群之前只能这么做,所以那些配置了集群的,就不用参考了。应该直接就可以连接上。

这几个配置参考以下文章
文章一
参考完这个无法启动
文章二

文章二的配置彻底解决了我的问题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值