【实现验证】ElasticSearch数据库中存储数组字段

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
        }
      ]
    }
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值