欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Elasticsearch 操作

程序员文章站 2022-05-25 15:43:43
集群健康检查 取得健康状态 GET /_cat/health?v 返回: 健康状态分类 green:索引的primary shard和replica shard都是active状态的 yellow:索引的primary shard都是active状态的,但是部分replica shard不是acti ......

集群健康检查

取得健康状态

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"
        }
    }
}