Elasticsearch 操作

2018-08-26 17:20:18来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

集群健康检查

取得健康状态

GET /_cat/health?v

返回:

epoch      timestamp cluster       status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1535248805 10:00:05  elasticsearch yellow          1         1      1   1    0    0        1             0                  -                 50.0%

健康状态分类

green:索引的primary shard和replica shard都是active状态的

yellow:索引的primary shard都是active状态的,但是部分replica shard不是active状态,处于不可用的状态、

red:不是所有索引的primary shard都是active状态的,部分索引有数据丢失了

当前处于yellow状态是因为目前只启动了一个es进程,只有一个节点node,这是不安全的。这个节点的primary shard 有分配,但是replica shard 没有节点分配,因此是yellow状态。

快速查看集群有哪些节点

 GET /_cat/indices?v
health status index   uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   .kibana xSbUffi_SYGYIhPtiMxH5w   1   1          1            0      3.1kb          3.1kb

简单的索引操作

创建索引:PUT /test_index?pretty
返回:
{
  "acknowledged": true,
  "shards_acknowledged": true
}
删除索引:DELETE /test_index?pretty
返回:
{
  "acknowledged": true
}

CURD 操作Demo

(1)新增商品:新增文档,建立索引

PUT /ecommerce/product/1
{
    "name" : "xi jie jing",
    "desc" :  "gaoxiao qing jie",
    "price" :  30,
    "producer" :      "xijiejing producer",
    "tags": [ "qingxi", "wuzhi" ]
}
 返回:
{
  "_index": "ecommerce",
  "_type": "product",
  "_id": "1",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "created": true
}

新增

PUT /ecommerce/product/2
{
    "name" : "niunai",
    "desc" :  "mengniu niunai",
    "price" :  25,
    "producer" :      "mengniuniunai producer",
    "tags": [ "niunai" ]
}

返回:

{
  "_index": "ecommerce",
  "_type": "product",
  "_id": "2",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "created": true
}
 
(2)查询商品:检索文档
GET /ecommerce/product/1

返回:

{
  "_index": "ecommerce",
  "_type": "product",
  "_id": "1",
  "_version": 1,
  "found": true,
  "_source": {
    "name": "xi jie jing",
    "desc": "gaoxiao qing jie",
    "price": 30,
    "producer": "xijiejing producer",
    "tags": [
      "qingxi",
      "wuzhi"
    ]
  }
}
(3)修改商品:替换文档
PUT /ecommerce/product/1
{
    "name" : "jiaqiangban gaolujie yagao",
    "desc" :  "gaoxiao meibai",
    "price" :  30,
    "producer" :      "gaolujie producer",
    "tags": [ "meibai", "fangzhu" ]
}

返回:

{
  "_index": "ecommerce",
  "_type": "product",
  "_id": "1",
  "_version": 2,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "created": false
}
(5)删除商品:删除文档
DELETE /ecommerce/product/1
{
  "found": true,
  "_index": "ecommerce",
  "_type": "product",
  "_id": "1",
  "_version": 3,
  "result": "deleted",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  }
}
多种搜索方式
1 query string search
GET /ecommerce/product/_search 
{
  "took": 12,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "ecommerce",
        "_type": "product",
        "_id": "2",
        "_score": 1,
        "_source": {
          "name": "niunai",
          "desc": "mengniu niunai",
          "price": 25,
          "producer": "mengniuniunai producer",
          "tags": [
            "niunai"
          ]
        }
      }
    ]
  }
}

字段说明:

took:耗时
timed_out:是否超时
_shards:数据拆成了5个分片
hits.total:查询结果的数量,3个document
hits.max_score:匹配分数,越相关,就越匹配,分数也高
hits.hits:包含了匹配搜索的document的详细数据
 
此方法只是临时在命令行使用,例如crul,不适用于复杂查询。
 
2 query DSL
可以用json语法查询语法
查询所有的商品:
GET /ecommerce/product/_search
{
  "query": { "match_all": {} }
}

按照价格排序:

GET /ecommerce/product/_search
{
    "query" : {
        "match" : {
            "name" : "yagao"
        }
    },
    "sort": [
        { "price": "desc" }
    ]
}

 

 
 
 
 
 
 
 
 

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:mybatis架构

下一篇:2019 Java面试题