es查询nested字段类型数组长度来判断是否包含多个值
es查询数组长度
·
使用es时经常会碰到查询数组格式的字段是否有多个值的情况,总结一下。
一、字段类型为keywork时
"id": [
"1",
"2"
]
当字段类型为keyword时(创建索引后,添加的第一条数据id字段格式为数组格式,便会导致后续的数据都是数组),查询id字段有多值时用以下查询语句:
{
"query": {
"script": {
"script": "doc['id'].size() > 1" //1代表的是数组长度
}
}
}
二、字段类型为nested类型时
"entity": [
{
"id": "1"
"name": "张三"
},
{
"id": "2"
"name": "李四"
}
]
当字段类型为nested类型时,需要把entity字段的include_in_parent属性设置成true,例如:
"entity": {
"type": "nested",
"include_in_parent": true, //默认为false
"properties": {
"id": {
"type": "keyword"
},
"name": {
"type": "text",
"fields": {
"completion": {
"type": "text",
"analyzer": "completion_analyzer"
},
"keyword": {
"type": "keyword"
},
"pinyin": {
"type": "text",
"analyzer": "full_pinyin_analyzer"
}
},
"analyzer": "ik_max_word"
}
}
}
以下查询语句才会生效:
{
"query": {
"script": {
"script": "doc['entity.id'].size() > 1" //1代表的是数组长度
}
}
}
include_in_parent:设置所有嵌套对象属性的include_in_parent值,可选值有true和false。值为true则在嵌套对象中的所有所有字段会被添加到父文档的作为标准字段,默认为false。
更多推荐



所有评论(0)