使用cURL尝试ElasticSearch

测试环境:debian 9
官网提供了 deb,rpm,源码下载

官方下载地址:https://www.elastic.co/downloads/elasticsearch

通过源码安装会遇到一些小问题,为了方便,我直接下载deb安装(我不确定通过deb安装是否需要java环境,因为我提前安装了openjdk-8-jdk)。

可以通过 service elasticsearch start/stop 启动关闭服务,默认监听了 9200端口,可以更改配置文件

通过deb安装的配置文件在:/etc/elasticsearch/elasticsearch.yml

如果要在localhost外连接elasticsearch ,更改配置文件中的 network.host:0.0.0.0

如果一起顺利就可以开始测试了

查看es基本信息
curl localhost:9200

列出所有的Index
curl -X GET 'http://localhost:9200/_cat/indices?v'

列举每个Index下的Type
curl 'localhost:9200/_mapping?pretty=true'

添加Index
curl -X PUT 'localhost:9200/weather'

删除Index
curl -X DELETE 'localhost:9200/weather'

安装中文分词插件ik (安装完需要重启es)
elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.5.4/elasticsearch-analysis-ik-6.5.4.zip

创建一个Index,并设置其结构和分词
curl -X PUT -H 'Content-Type: application/json' 'localhost:9200/accounts' -d '
{
  "mappings": {
    "person": {
      "properties": {
        "user": {
          "type": "text",
          "analyzer": "ik_max_word",
          "search_analyzer": "ik_max_word"
        },
        "title": {
          "type": "text",
          "analyzer": "ik_max_word",
          "search_analyzer": "ik_max_word"
        }
      }
    }
  }
}'

向Index增加记录
PUT方式
curl -X PUT -H 'Content-Type: application/json' 'localhost:9200/accounts/person/1' -d '
{
  "user": "张三",
  "title": "工程师"
}' 

POST方式(POST方式不需要传id,id随机生成)
curl -X POST -H 'Content-Type: application/json' 'localhost:9200/accounts/person' -d '
{
  "user": "李四",
  "title": "工程师"
}'
注意:如果没有先创建 Index(这个例子是accounts),直接执行上面的命令,Elastic 也不会报错,而是直接生成指定的 Index。所以,打字的时候要小心,不要写错 Index 的名称。

查看指定条目的记录
curl 'localhost:9200/accounts/person/1?pretty=true'

删除一条记录
curl -X DELETE 'localhost:9200/accounts/person/1'

更新一条记录
curl -X PUT -H 'Content-Type: application/json' 'localhost:9200/accounts/person/1' -d '
{
    "user" : "张三",
    "title" : "软件开发"
}' 

查询所有记录
curl 'localhost:9200/accounts/person/_search?pretty=true'

简单查询
curl -H 'Content-Type: application/json' 'localhost:9200/accounts/person/_search?pretty=true'  -d '
{
  "query" : { "match" : { "title" : "工程" }},
  "from": 1, #0开始
  "size": 1, #返回几条数据
}'

OR查询
curl -H 'Content-Type: application/json' 'localhost:9200/accounts/person/_search?pretty=true'  -d '
{
  "query" : { "match" : { "title" : "工程 哈哈" }}
}'

AND查询
curl -H 'Content-Type: application/json' 'localhost:9200/accounts/person/_search?pretty=true'  -d '
{
  "query": {
    "bool": {
      "must": [
        { "match": { "title": "工程" } },
        { "match": { "title": "哈哈" } }
      ]
    }
  }
}'

 

转载于:https://www.cnblogs.com/codeAB/p/10185296.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值