Arrays | Elasticsearch Guide [7.6] | Elastic
elastic 本身是支持数组的,但是没有特定的数组类型来支持, 下面我举例数组的使用.官方文档见链接Arrays | Elasticsearch Guide [7.6] | Elastic
1. 创建一个测试index: test_array
{
"mappings": {
"properties": {
"location": {
"type": "keyword"
}
}
}
}
2. 填入3条数据
{"location": ["山东","北京","天津","河南","云南"]}
{"location": ["山东", "四川","草原"]}
{"location": ["草原"]}
3. 验证
a. 查询包含北京或者包含草原的
{
"query": {
"bool": {
"should": [
{
"term": {
"location": "草原"
}
},
{
"term": {
"location": "北京"
}
}
]
}
}
}
返回3条数据:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 1.5956267,
"hits": [
{
"_index": "test_array",
"_type": "_doc",
"_id": "OnuMI34BK32v-9MebjV2",
"_score": 1.5956267,
"_source": {
"location": [
"山东",
"北京",
"天津",
"河南",
"云南"
]
}
},
{
"_index": "test_array",
"_type": "_doc",
"_id": "O3uNI34BK32v-9MeIDVl",
"_score": 0.9186288,
"_source": {
"location": [
"山东",
"四川",
"草原"
]
}
},
{
"_index": "test_array",
"_type": "_doc",
"_id": "PHuwI34BK32v-9MeJjVA",
"_score": 0.9186288,
"_source": {
"location": [
"草原"
]
}
}
]
}
}
b. 验证既包含山东又包含北京的数据
{
"query": {
"bool": {
"must": [
{
"term": {
"location": "山东"
}
},
{
"term": {
"location": "北京"
}
}
]
}
}
}
返回结果1条数据
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 2.5142555,
"hits": [
{
"_index": "test_array",
"_type": "_doc",
"_id": "OnuMI34BK32v-9MebjV2",
"_score": 2.5142555,
"_source": {
"location": [
"山东",
"北京",
"天津",
"河南",
"云南"
]
}
}
]
}
}



