• Elasticsearch 操作


    集群健康检查

    取得健康状态

    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" }
        ]
    }
    3 query filter
    搜索商品名称包含yagao,而且售价大于25元的商品
    
    GET /ecommerce/product/_search
    {
        "query" : {
            "bool" : {
                "must" : {
                    "match" : {
                        "name" : "yagao" 
                    }
                },
                "filter" : {
                    "range" : {
                        "price" : { "gt" : 25 } 
                    }
                }
            }
        }
    }
     
    4、full-text search(全文检索)
    GET /ecommerce/product/_search
    {
        "query" : {
            "match" : {
                "producer" : "yagao producer"
            }
        }
    }
     
     
     
     
     
  • 相关阅读:
    vue去除#号tomcat配置
    vscode配置
    git忽略想要提交的文件
    vue-cli配置移动端自适应
    远程调试工具 -- weinre
    将博客搬至CSDN
    错误:this dependency was not found:'element-ui/lib/theme-chalk/index.css'。。。。。。。
    win上java1.7和1.8版本修改环境变量无效.md
    mysql 事务
    mysql 函数
  • 原文地址:https://www.cnblogs.com/zhenghongxin/p/9536532.html
Copyright © 2020-2023  润新知