• ElasticSearch基本用法


    1.查询所有索引:

    GET _cat/indices

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

    GET users/_doc/1 

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

    GET product/_search

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

    1. POST product/_update/1
    2. {
    3.  "doc":{
    4.      "price":899
    5.  }
    6. }

    5.全量更新

    1. PUT product/_doc/1003
    2. {"title":"小米手机",
    3.  "category":"小米",
    4.  "price":"599",
    5.  "image":"https://mi.jpg"
    6. }

    6.删除指定的文档:

    DELETE product/_doc/1002

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

    1. GET shopping/_search
    2. {"query":{
    3. "match_phrase":{
    4. "image":"https://xiaomi.jpg"
    5. }
    6. }
    7. }

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

    1. GET product/_search
    2. {"query":{
    3. "match_all":{
    4. }},
    5. "from":0,
    6. "size":2,
    7. "_source":["brand","price"],
    8. "sort":{
    9. "price":{
    10. "order":"desc"
    11. }
    12. }
    13. }

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

    1. GET shopping/_search
    2. {
    3. "query":{
    4. "bool":{
    5. "must":[
    6. {"match":{
    7. "category": "小米"
    8. }},
    9. {"match":{
    10. "price": "599"
    11. }}
    12. ]
    13. }
    14. }
    15. }

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

    1. GET shopping/_search
    2. {
    3. "query":{
    4. "bool":{
    5. "should":[
    6. {"match":{
    7. "brandName": "小米"
    8. }},
    9. {"match":{
    10. "brandName": "vivo"
    11. }}
    12. ]
    13. }
    14. }
    15. }

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

    1. POST /user/_doc/1001
    2. {"name":"xiaomi",
    3. "sex":"F",
    4. "tel":"131"
    5. }

    1. PUT shopping/_create/1006
    2. {"title":"vivo手机",
    3. "category":"vivo",
    4. "price":"2958",
    5. "image":"https://vivo.jpg"
    6. }

    12.分组统计:

    1. GET phone/_search
    2. {
    3. "aggs":{
    4. "price_group":{
    5. "terms":{
    6. "field":"price"
    7. }
    8. }
    9. },
    10. "size":0 //不显示原始数据
    11. }

    13.求平均数:

    1. GET phone/_search
    2. {
    3. "aggs":{
    4. "price_avg":{
    5. "avg":{
    6. "field":"price"
    7. }
    8. }
    9. },
    10. "size":0 //不显示原始数据
    11. }

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

    1. GET gulimall_product/_search
    2. {
    3. "query": {
    4. "match_all": {}
    5. },
    6. "aggs": {
    7. "brand_agg": {
    8. "terms": {
    9. "field": "brandId",
    10. "size": 10
    11. },
    12. "aggs": {
    13. "brand_name_agg": {
    14. "terms": {
    15. "field": "brandName",
    16. "size": 10
    17. }
    18. }
    19. }
    20. },
    21. "catalog_agg":{
    22. "terms": {
    23. "field": "catalogId",
    24. "size": 10
    25. }
    26. }
    27. }
    28. }
    15.创建索引映射结构
    1. PUT gulimall_product
    2. {
    3. "mappings":{
    4. "properties": {
    5. "skuId":{ "type": "long" },
    6. "spuId":{ "type": "keyword" },
    7. "skuTitle": {
    8. "type": "text",
    9. "analyzer": "ik_smart"
    10. },
    11. "skuPrice": { "type": "keyword" },
    12. "skuImg" : { "type": "keyword" },
    13. "saleCount":{ "type":"long" },
    14. "hasStock": { "type": "boolean" },
    15. "hotScore": { "type": "long" },
    16. "brandId": { "type": "long" },
    17. "catalogId": { "type": "long" },
    18. "brandName": {"type": "keyword"},
    19. "brandImg":{
    20. "type": "keyword"
    21. },
    22. "catalogName": {"type": "keyword" },
    23. "attrs": {
    24. "type": "nested",
    25. "properties": {
    26. "attrId": {"type": "long" },
    27. "attrName": {
    28. "type": "keyword"
    29. },
    30. "attrValue": {"type": "keyword" }
    31. }
    32. }
    33. }
    34. }
    35. }
    16.删除索引,将索引里所有文档删除,含映射结构:
    DELETE gulimall_product

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

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

  • 相关阅读:
    ClickHouse进阶(六):副本与分片-2-Distributed引擎
    科普向丨语音芯片烧录工艺的要求
    java之单元测试与注解与枚举
    Pandas GroupBy 深度总结
    【Java基础】方法注意事项、通用格式及方法重载
    自动化测试如何进阶?本文从4个方面带你系统进阶自动化涨薪5k...
    SpringMvc学习笔记
    【Java集合类面试六】、 HashMap有什么特点?
    练习7-在Verilog中使用任务task
    固定资产可视化智能管理系统
  • 原文地址:https://blog.csdn.net/shenxiaomo1688/article/details/139625560