首先说一下我的需求:查询条件只有student.en_name一个,需求是查询所有班级里,有两个或者以上的人名字是 张一 的所有结果下边是模拟数据
1.索引mapping如下,student为nested类型;
下边是我的查询
1.索引mapping如下,student为nested类型;
GET /index_class/_mapping
{
"index_class" : {
"mappings" : {
"properties" : {
"id" : {
"type" : "long"
},
"name" : {
"type" : "text"
},
"student" : {
"type" : "nested",
"properties" : {
"en_name" : {
"type" : "keyword"
},
"id" : {
"type" : "keyword"
},
"name" : {
"type" : "text",
"analyzer" : "ik_max_word"
}
}
},
"type" : {
"type" : "keyword"
}
}
}
}
}
2.放入数据{
"id": 1,
"name": "一班",
"type": "A",
"student": [{
"name": "张一",
"id": "1",
"en_name": "zhangyi"
},
{
"name": "张一",
"id": "2",
"en_name": "zhangyi"
},
{
"name": "张三",
"id": "3",
"en_name": "zhangsan"
},
{
"name": "张三",
"id": "4",
"en_name": "zhangsan"
}]
}, {
"id": 2,
"name": "二班",
"type": "A",
"student": [{
"name": "张一",
"id": "1",
"en_name": "zhangyi"
},
{
"name": "张二",
"id": "2",
"en_name": "zhanger"
},
{
"name": "张三",
"id": "3",
"en_name": "zhangsan"
},
{
"name": "张四",
"id": "4",
"en_name": "zhangsi"
}]
}
需求是使用student.en_name,查询到只有两个 张一 的一班结果,不查到一个张一的二班的结果,需要怎么写查询?请大神指教一下下边是我的查询
POST /index_class/_search
{
"from":0,
"size":10,
"query":{
"bool":{
"must":[
{
"nested":{
"query":{
"bool":{
"must":[
{
"match":{
"student.en_name":"zhangyi"
}
}
]
}
},
"path":"student"
}
},
{
"nested":{
"query":{
"bool":{
"must":[
{
"match":{
"student.en_name":"zhangyi"
}
}
]
}
},
"path":"student"
}
}
]
}
}
}
3 个回复
BetterLevi
赞同来自: HarryXie
BetterLevi
赞同来自:
lemonzero
赞同来自: