ES 索引库常用语句 + 脚本语句

1\创建索引库

设置分片与备份数

    "number_of_shards": 5,
    "number_of_replicas": 2
    示例:
PUT emr
{
  "aliases": {},
  "mappings": {
    "properties": {
      "_score": {
        "type": "long"
      },
      "name": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      }
   }
  },
  "settings": {
    "index": {
      "routing": {
        "allocation": {
          "include": {
            "_tier_preference": "data_content"
          }
        }
      }
    },
    "number_of_shards": 5,
    "number_of_replicas": 2
  }
} 

2/删除索引库

DELETE emr

3/添加字段

PUT emr/_mapping
{
  "properties": {
    "birthTime": {
      "type": "date"
    }
  }
} 

4/为nested添加字段

// 补充嵌套字段
PUT emr/_mapping
{
  "properties": {
    "skuList":{
      "type" : "nested",
      "properties": {
         "birthTime": {
        "type": "date"
        }
      }
    }
  }
}

5/查询结构

GET emr

6/查询所有信息

GET emr/_search

7/根据ID查询

GET emr/_doc/9065

8/like查询

POST /emr/_search
{
  "from" : 0,
  "post_filter" : {
    "bool" : {
      "filter" : [ ]
    }
  },
  "query" : {
    "bool" : {
      "should" : [ {
        "match_phrase" : {
          "phoneNum" : {
            "query" : "李"
          }
        }
      }, {
        "match_phrase" : {
          "name" : {
            "query" : "李"
          }
        }
      } ]
    }
  },
  "size" : 5,
  "sort" : [ ],
  "track_scores" : false,
  "version" : true
}

9/固定字段查询

POST /emr/_search
{
  "from" : 0,
  "post_filter" : {
    "bool" : {
      "filter" : [ {
        "match" : {
          "phoneNum" : {
            "query" : "13613131313"
          }
        }
      } ]
    }
  },
  "query" : {
    "bool" : {
      "should" : [ ]
    }
  },
  "size" : 5,
  "sort" : [ ],
  "track_scores" : false,
  "version" : true
}

查询 + 条数+排序

POST /order/_search
{
  "from" : 0,
  "post_filter" : {
    "bool" : {
      "filter" : [ {
        "term" : {
          "userId" : {
            "value" : "137406"
          }
        }
      }]
    }
  },
  "size" : 10,
  "sort" : [ {
    "createTime" : {
      "mode" : "min",
      "order" : "desc"
    }
  } ],
  "track_scores" : false,
  "track_total_hits" : 2147483647,
  "version" : true
}

10/脚本

nativeQueryBuilder.withAggregation(userAggName, Aggregation
                .of(agg -> agg.terms(b -> b.field("userId").size(Integer.MAX_VALUE))
                        .aggregations("settlementStatusAgg", settlementStatusAgg -> settlementStatusAgg.terms(b -> b.field("settlementStatus").size(Integer.MAX_VALUE))
                                .aggregations("balanceAgg", balanceAgg -> balanceAgg.sum(b -> b.field("balance"))))
                        .aggregations("confirmStatusAgg", confirmStatusAgg -> confirmStatusAgg.terms(b -> b.field("confirmStatus").size(Integer.MAX_VALUE))
                                .aggregations("balanceAgg", balanceAgg -> balanceAgg.sum(b -> b.field("balance"))))
                        .aggregations("topHitsAgg", shareTypeAgg -> shareTypeAgg.topHits(top -> top.docvalueFields(Arrays.asList("shareType", "shopId")).size(1)))
                ));
    

11/时间范围查询

if(null != query.getLivingStart() && null != query.getLivingEnd()){
            queryList.add(QueryBuilders.range(b ->b.field("livingStart")
                    .gt(JsonData.fromJson(String.valueOf(query.getLivingStart().toEpochSecond(ZoneOffset.of("+8")))))
                    .lt(JsonData.fromJson(String.valueOf(query.getLivingEnd().toEpochSecond(ZoneOffset.of("+8")))))));

12/集合查询

POST /product/_search
{
  "from" : 0,
  "post_filter" : {
    "bool" : {
      "filter" : [ {
        "terms" : {
          "spuId" : [ 9066, 9064, 9065 ]
        }
      } ]
    }
  },
  "query" : {
    "bool" : { }
  },
  "size" : 10,
  "sort" : [ {
    "createTime" : {
      "mode" : "min",
      "order" : "desc"
    }
  } ],
  "track_scores" : false,
  "version" : true
}

修改

POST product/_update/8907
{
  "doc": {
    "spuName":"【123】"
  }
}

##批量修改

POST /share_info/_update_by_query
{
   "script": {
    "source": "ctx._source.createTime='2024-01-27T12:00:03.469';ctx._source.finishTime='2024-01-27T12:00:03.469';",
    "lang": "painless"
  },
  "query": {
    "bool": {
      "filter": [
        {
          "terms": {
            "_id": [
102946863,
102946865,
102946858
            ]
          }
        }
      ]
    }
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

01传说

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值