Elasticsearch-6.x.x 操作集合

#健康检查
curl 'localhost:9200/_cat/health?v'
status:绿色代表一切正常(集群功能齐全),黄色意味着所有的数据都是可用的,但是某些复制没有被分配(集群功能齐全),红色则代表因为某些原因,某些数据不可用
#节点查询
curl 'localhost:9200/_cat/nodes?v'
#索引查询
curl 'localhost:9200/_cat/indices?v'
1.新建和删除Index
curl -X PUT 'localhost:9200/xxx'
curl -X DELETE 'localhost:9200/weather'
2.新增记录
curl -X PUT 'localhost:9200/hupu/xxx/1' -d '
{
    "title:"怎么选?",
    "readnum:"432",
    "likeNum:"32"
}'
id可以指定,可以不指定;id 不一定是数字,也可以是字符串
3.查看记录
curl 'localhost:9200/hupu/xxx/1?pretty=true'
4.删除记录
curl -X DELETE 'localhost:9200/hupu/xxx/1'
5.更新记录
curl -X PUT 'localhost:9200/hupu/xxx/1' -d '
{
    "title":"曾某",
    "readnum":"1",
    "likenum":"23"
}
6.返回所有记录
curl 'localhost:9200/hupu/xxx/_search'
took字段表示该操作的耗时(单位为毫秒),time_out字段表示是否超时,hits字段表示命中记录组成的数组 total:返回记录数,本例是2条。max_score:最高的匹配程度,本例是1.0 
7.全文搜索
elastic的查询非常特别,使用自己的查询语法,要求GET请求带数据体。
curl 'localhost:9200/accounts/person/_search' -d '
{
    "query":{"match":{"desc":"软件"}},
    "from":1,
    "size":1
}'
elastic 默认一次返回10条结果,可以通过size字段改变这个设置.还可以通过from 字段,指定位移。
8.逻辑运算
如果有多个搜索关键字,elastic认为它们是or关系。
curl localhost:9200/accounts/person/_search' -d '
{
    "query":{"match":{"desc":"软件 系统"}}
},
如果要执行多个关键字的and搜索,必须使用布尔查询
curl 'localhost:9200/accounts/person/_search' -d [
{
    "query":{
        "bool":{
            "must":[
                { "match": { "desc": "软件" } },
                { "match": { "desc": "系统" } }
            ]
        }
    }
}





1.创建索引

PUT exchange
{
  "acknowledged": true,
  "shards_acknowledged": true,
  "index": "test"
}

注意:索引名不能包含大些字母

2.指定参数创建索引

PUT blog
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 1
  } 
}
{
  "acknowledged": true,
  "shards_acknowledged": true,
  "index": "blog"
}

3.查看索引

3.1 查看指定索引的配置信息

GET blog/_settings
{
  "blog": {
    "settings": {
      "index": {
        "creation_date": "1515458969949",
        "number_of_shards": "3",
        "number_of_replicas": "1",
        "uuid": "A7pKNO7bTgucu1uNgmXlQg",
        "version": {
          "created": "5060399"
        },
        "provided_name": "blog"
      }
    }
  }
}

4.删除索引

DELETE test
{
  "acknowledged": true
}

5.添加索引并指定类型字段信息

PUT exchangehistory
{
    "settings" : {
        "number_of_shards" : 5
    },
    "mappings" : {
        "record" : {
            "properties" : {
        "amount": {
          "type": "float"
        },
        "contract": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "exchange_time": {
          "type": "date"
        },
        "id": {
          "type": "long"
        },
        "last": {
          "type": "float"
        },
        "time": {
          "type": "date"
        },
        "volume": {
          "type": "float"
        }
      }
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章