1.插入数据
POST t_bbs_posts_test/t_bbs_posts/5
{
"array":["a","b","c"]
}
2.查看字段的mapping
"array": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
3.查询字段
支持match、match_phrase、
GET t_bbs_posts_test/_search
{
"query":{
"match":{
"array":"a"
}
}
}
结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.25811607,
"hits": [
{
"_index": "t_bbs_posts_test",
"_type": "t_bbs_posts",
"_id": "5",
"_score": 0.25811607,
"_source": {
"array": [
"a",
"b",
"c"
]
}
}
]
}
}
4.增加字段中的某个值
不支持去重
POST t_bbs_posts_test/t_bbs_posts/5/_update
{
"script" : {
"inline": "ctx._source.array.add(params.tag)",
"params" : {
"tag" : "d"
}
}
}
5.删除字段中的某个值
POST t_bbs_posts_test/t_bbs_posts/5/_update
{
"script" : {
"inline": "ctx._source.array.remove(ctx._source.array.indexOf(params.tag))",
"params" : {
"tag" : "b"
}
}
}
查询并删除
POST t_bbs_posts_test/t_bbs_posts/_update_by_query
{
"query": {
"term": {
"array": "c"
}
},
"script": {
"inline": "ctx._source.array.remove(ctx._source.array.indexOf(params.tag))",
"params": {
"tag": "c"
}
}
}
6.创建模板
PUT _template/template_1
{
"order": 0,
"template": "t_test*",
"settings": {
"index": {
"max_result_window": "10000",
"refresh_interval": "60s",
"analysis": {
"filter": {
"english_stop": {
"type": "stop",
"stopwords": "_english_"
}
},
"analyzer": {
"key_analyzer": {
"filter": [
"lowercase"
],
"type": "custom",
"tokenizer": "keyword"
},
"comma": {
"pattern": ",",
"type": "pattern"
},
"text_analyzer": {
"filter": [
"lowercase"
],
"type": "custom",
"tokenizer": "standard"
},
"text_stop_analyzer": {
"filter": [
"lowercase",
"english_stop"
],
"type": "custom",
"tokenizer": "standard"
}
}
},
"number_of_shards": "3",
"number_of_replicas": "0"
}
},
"mappings": {
"t_test": {
"_all": {
"enabled": false
},
"properties": {
"array": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
},
"fielddata": true
}
}
}
},
"aliases": {
}
}
7.测试聚合
PUT t_test20210903/t_test/5
{
"array":["a","b","c"]
}
PUT t_test20210903/t_test/3
{
"array":["a","b","c","a"]
}
查询统计
GET t_test20210903/_search
{
"size": 0,
"aggs": {
"array": {
"terms": {
"field": "array"
}
}
}
}
聚合结果
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0,
"hits": [
]
},
"aggregations": {
"array": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "a",
"doc_count": 2
},
{
"key": "b",
"doc_count": 2
},
{
"key": "c",
"doc_count": 2
}
]
}
}
}