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
}

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】”
}
}

你可能感兴趣的:(elasticsearch,数据库,搜索引擎)