elasticsearch简单操作

#查看健康状态

GET _cat/health?v

#检查分片信息

GET _cat/shards?v

#查看索引信息

GET _cat/indices?v

#新增索引

PUT /test_index

GET /test_index

#删除索引

DELETE /test_index

#新增document

PUT /test_index/my_type/1
{
   "name":"test_doc_01",
   "remark":"first test elastic search",
   "order_no":1
}
PUT /test_index/my_type/2
{
   "name":"test_doc_02",
   "remark":"second test elastic search",
   "order_no":2
}
PUT /test_index/my_type/3
{
   "name":"test_doc_03",
   "remark":"third test elastic search",
   "order_no":3
}

#新增 自动分配id

POST /test_index/my_type
{
   "name":"test_doc_04",
   "remark":"forth test elastic search",
   "order_no":4
}

GET /test_index/my_type/1

#批量查询

GET /_mget
{
  "docs" : [
    {
      "_index" : "test_index",
      "_type" : "my_type",
      "_id" : "1"
    },
    {
      "_index" : "test_index",
      "_type" : "my_type",
      "_id" : "2"
    }
  ]
}

GET /test_index/_mget
{
  "docs" : [
    {
      "_type" : "my_type",
      "_id" : "1"
    },
    {
      "_type" : "my_type",
      "_id" : "2"
    }
  ]
}

GET /test_index/my_type/_mget
{
  "docs" : [
    {
      "_id" : "1"
    },
    {
      "_id" : "2"
    }
  ]
}

# 强制新增 已存在的document报错

PUT /test_index/my_type/1/_create

{

   "name":"new_test_doc_01",

   "remark":"first test elastic search",

   "order_no":1

}

# 只更新某一些字段 底层执行跟全量更新无差别

POST /test_index/my_type/1/_update
{
   "doc":{
      "name":" test_doc_01_for_update"
   }
}

# bulk操作

POST /_bulk
{ "create" : { "_index" : "test_index" , "_type" : "my_type", "_id" : "1" } }
{ "field_name" : "field value" }

POST /_bulk
{"index":{ "_index" : "test_index", "_type" : "my_type" , "_id" : "2"}}
{"field_name" : "field value 2"}

POST /_bulk
{ "delete" : { "_index" : "test_index", "_type" : "my_type", "_id" : "2" } }

 

# 全数据搜索
GET /test_index/my_type/_search

GET /test_index/my_type/_search?q=remark:test&sort=order_no:desc

#query DSL(特殊领域语言)

GET /test_index/my_type/_search
{
  "query": {
    "match": {
      "remark": "test"
    }
  },
  "sort": [
    {
      "order_no": {
        "order": "asc"
      }
    }
  ]
}

#query filter 作为dsl的补充,过滤时候不做匹配分数计算

GET /person/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match_all": {}
        }
      ],
      "filter": {
        "range": {
          "age": {
            "gte": 18,
            "lte": 27
          }
        }
      }
    }
  }
}

# 分页
GET /test_index/my_type/_search
{
   "query" : { "match_all" : {} },
   "from" : 1, 
   "size" : 1,
   "sort" : [
      { "order_no" : "asc" }
   ]
}

# 聚合操作

PUT /products_index/_mapping
{
  "properties": {
    "tags": {
      "type": "text",
      "fielddata": true
    }
  }
}

GET /products_index/phone_type/_search
{
  "size": 0,
  "aggs": {
    "group_by_tags": {
      "terms": {
        "field": "tags"
      }
    }
  }
}

GET /products_index/phone_type/_search
{
  "size": 0,
  "query": {
    "match": {
      "name": "PLUS"
    }
  },
  "aggs": {
    "group_by_tags": {
      "terms": {
        "field": "tags"
      }
    }
  }
}

GET /products_index/phone_type/_search
{
  "query": {
    "match": {
      "name": "plus"
    }
  },
  "aggs": {
    "avg_by_price": {
      "avg": {
        "field": "price"
      }
    }
  }
}

GET /products_index/phone_type/_search
{
  "size": 0,
  "aggs": {
    "group_by_tags": {
      "terms": {
        "field": "tags",
        "order": {
          "avg_price": "desc"
        }
      },
      "aggs": {
        "avg_price": {
          "avg": {
            "field": "price"
          }
        }
      }
    }
  }
}

GET /products_index/phone_type/_search
{
  "query": {
    "match_all": {}
  },
  "_source": "price",
  "aggs": {
    "range_by_price": {
      "range": {
        "field": "price",
        "ranges": [
          {
            "from": 500000,
            "to": 600000
          },
          {
            "from": 600001,
            "to": 800000
          },
          {
            "from": 800001,
            "to": 1000000
          }
        ]
      },
      "aggs": {
        "avg_by_price": {
          "avg": {
            "field": "price"
          }
        }
      }
    }
  }
}
 

常用元数据

_index _type _id _source _version

# external_version

PUT /products_index/phone_type/1?version=5&version_type=external
{
  "name" : "iphone x",
  "remark" : "256G",
  "price" : 899900,
  "producer" : "apple",
  "tags" : [ "256G" ]
}

# 分页搜索
GET /_search?from=0&size=10

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值