ElasticSearch基本用法

1.查询所有索引:

GET _cat/indices

2.根据id查询索引里指定数据(users为索引名,1为id的值):

GET users/_doc/1 

3.查询索引里所有的数据(product为索引名)

GET product/_search

4.局部更新指定的数据(将product索引里id为1进行更新)

POST product/_update/1

{

 "doc":{

     "price":899

 }

}

5.全量更新

PUT product/_doc/1003

{"title":"小米手机",

 "category":"小米",

 "price":"599",

 "image":"https://mi.jpg"

}

6.删除指定的文档:

DELETE product/_doc/1002

7.按指定条件进行查询匹配:

GET shopping/_search
{"query":{
    "match_phrase":{
        "image":"https://xiaomi.jpg"
    }
  }
}

8.分页和排序,分页使用from表示起始记录数,size每页返回条数,排序,使用关键字sort,并指定排序字段,排序规则:

GET product/_search
{"query":{
    "match_all":{
        
    }},
     "from":0,
    "size":2,
    "_source":["brand","price"],
    "sort":{
        "price":{
            "order":"desc"
        }
      }
    }

9.多条件and查询,使用关键字must:

GET shopping/_search
{
 "query":{
   "bool":{
    "must":[
	   {"match":{
	     "category": "小米"
	  }},
	  {"match":{
	      "price": "599"
	  }}
	  
	 ]
   }
 }
}

10.多条件or查询,使用关键字should:

GET shopping/_search
{
 "query":{
   "bool":{
    "should":[
	   {"match":{
	     "brandName": "小米"
	  }},
	  {"match":{
	       "brandName": "vivo"
	  }}
	  
	 ]
   }
 }
}

11.新增一条数据并指定id:

POST /user/_doc/1001
{"name":"xiaomi",
 "sex":"F",
 "tel":"131"
}

PUT shopping/_create/1006
{"title":"vivo手机",
 "category":"vivo",
 "price":"2958",
 "image":"https://vivo.jpg"
}

12.分组统计:

GET phone/_search
{
    "aggs":{
        "price_group":{
            "terms":{
                "field":"price"
            }
        }
    },
    "size":0  //不显示原始数据
}

13.求平均数:

GET phone/_search
{
    "aggs":{
        "price_avg":{
            "avg":{
                "field":"price"
            }
        }
    },
    "size":0  //不显示原始数据
}

14.聚合查询(聚合中嵌套聚合):

GET gulimall_product/_search
{
  "query": {
    "match_all": {}
    
  },
  "aggs": {
    "brand_agg": {
      "terms": {
        "field": "brandId",
        "size": 10
      },
      "aggs": {
        "brand_name_agg": {
          "terms": {
            "field": "brandName",
            "size": 10
          }
        }
      }
    },
    "catalog_agg":{
      "terms": {
        "field": "catalogId",
        "size": 10
      }
    }
  }
}
15.创建索引映射结构
PUT gulimall_product
{
    "mappings" : {
      "properties" : {
        "attrs" : {
          "type" : "nested",
          "properties" : {
            "attrId" : {
              "type" : "long"
            },
            "attrName" : {
              "type" : "keyword"
            },
            "attrValue" : {
              "type" : "keyword"
            }
          }
        },
        "brandId" : {
          "type" : "long"
        },
        "brandImg" : {
          "type" : "keyword"
        },
        "brandName" : {
          "type" : "keyword"
        },
        "catalogId" : {
          "type" : "long"
        },
        "catalogName" : {
          "type" : "keyword"
        },
        "hasStock" : {
          "type" : "boolean"
        },
        "hasstock" : {
          "type" : "boolean"
        },
        "hotScore" : {
          "type" : "long"
        },
        "saleCount" : {
          "type" : "long"
        },
        "skuId" : {
          "type" : "long"
        },
        "skuImage" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "skuImg" : {
          "type" : "keyword"
        },
        "skuPrice" : {
          "type" : "keyword"
        },
        "skuTitle" : {
          "type" : "text",
          "analyzer" : "ik_smart"
        },
        "spuId" : {
          "type" : "keyword"
        }
      }
    }
  }
}
16.删除索引,将索引里所有文档删除,含映射结构:
DELETE gulimall_product

在ElasticSearch中POST 可以创建ES自动生成id类型的document,也可自己指定id。
PUT id存在为创建,否则为全量替换
另外PUT还可以 使用op_type=create或_create实行强制创建document。

ElasticSearch文档中字段类型一旦创建,便不能修改,需要修改时可先创建一个新的索引,在索引写好好映射结构,然后将上一个索引的数据迁移过去。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值