绊脚石乃是进身之阶。

多层nested的多条件查询

Elasticsearch | 作者 meowCoder123 | 发布于2022年05月17日 | 阅读数:1758

现在数据结构中嵌套了两层nested 结构如下
{

    "mappings":{
        "properties":{
           
            "lisCheckJson":{
                "type":"nested",
                "properties":{
                    "lisCheckItemList":{
                        "type":"nested",
                        "properties":{
                            "itemName":{
                                "type":"keyword"
                            },
                            "resultChar":{
                                "type":"keyword"
                            }
                        }
                    }
                }
            }
        }
    }
}
数据内容如下

{
                "lisCheckJson": [
                    {
                        "lisCheckItemList": [
                            {
                                "resultChar": "8.3",
                                "itemName": "1"
                            },
                            {
                                "resultChar": "3.93",
                                "itemName": "2"
                            },
                            {
                                "resultChar": "144",
                                "itemName": "3"
                            }
                        ],
                        "lisCheckItemList": [
                            {
                                "resultChar": "8.3",
                                "itemName": "1"
                            },
                            {
                                "resultChar": "3.93",
                                "itemName": "2"
                            },
                            {
                                "resultChar": "144",
                                "itemName": "3"
                            }
                        ]
                      
                    }
                ]
            }
 
预期结果:
我想查询出在同一个lisCheckItemList下的 resultChar=8.3 同时itemName=2 同时resultChar=144 同时 itemName=3 的数据 但我现在的查询是只要在同一个lisCheckJson中,不在同一个lisCheckItemList下的数据满足条件的也会查出来,请问该如何解决
查询语句:
{
    "from":0,
    "size":1,
    "timeout":"60s",
    "query":{
        "bool":{
            "must":[
                {
                    "bool":{
                        "should":[
                            {
                                "nested":{
                                    "query":{
                                        "bool":{
                                            "must":[
                                                {
                                                    "nested":{
                                                        "query":{
                                                            "bool":{
                                                                "must":[
                                                                    {
                                                                        "match":{
                                                                            "lisCheckJson.lisCheckItemList.itemName":{
                                                                                "query":"2",
                                                                                "operator":"OR",
                                                                                "prefix_length":0,
                                                                                "max_expansions":50,
                                                                                "fuzzy_transpositions":true,
                                                                                "lenient":false,
                                                                                "zero_terms_query":"NONE",
                                                                                "auto_generate_synonyms_phrase_query":true,
                                                                                "boost":1
                                                                            }
                                                                        }
                                                                    },
                                                                    {
                                                                        "match":{
                                                                            "lisCheckJson.lisCheckItemList.resultChar":{
                                                                                "query":"8.3",
                                                                                "operator":"OR",
                                                                                "prefix_length":0,
                                                                                "max_expansions":50,
                                                                                "fuzzy_transpositions":true,
                                                                                "lenient":false,
                                                                                "zero_terms_query":"NONE",
                                                                                "auto_generate_synonyms_phrase_query":true,
                                                                                "boost":1
                                                                            }
                                                                        }
                                                                    }
                                                                ],
                                                                "adjust_pure_negative":true,
                                                                "boost":1
                                                            }
                                                        },
                                                        "path":"lisCheckJson.lisCheckItemList",
                                                        "ignore_unmapped":false,
                                                        "score_mode":"none",
                                                        "boost":1
                                                    }
                                                },
                                                {
                                                    "nested":{
                                                        "query":{
                                                            "bool":{
                                                                "must":[
                                                                    {
                                                                        "match":{
                                                                            "lisCheckJson.lisCheckItemList.itemName":{
                                                                                "query":"3",
                                                                                "operator":"OR",
                                                                                "prefix_length":0,
                                                                                "max_expansions":50,
                                                                                "fuzzy_transpositions":true,
                                                                                "lenient":false,
                                                                                "zero_terms_query":"NONE",
                                                                                "auto_generate_synonyms_phrase_query":true,
                                                                                "boost":1
                                                                            }
                                                                        }
                                                                    },
                                                                    {
                                                                        "match":{
                                                                            "lisCheckJson.lisCheckItemList.resultChar":{
                                                                                "query":"144",
                                                                                "operator":"OR",
                                                                                "prefix_length":0,
                                                                                "max_expansions":50,
                                                                                "fuzzy_transpositions":true,
                                                                                "lenient":false,
                                                                                "zero_terms_query":"NONE",
                                                                                "auto_generate_synonyms_phrase_query":true,
                                                                                "boost":1
                                                                            }
                                                                        }
                                                                    }
                                                                ],
                                                                "adjust_pure_negative":true,
                                                                "boost":1
                                                            }
                                                        },
                                                        "path":"lisCheckJson.lisCheckItemList",
                                                        "ignore_unmapped":false,
                                                        "score_mode":"none",
                                                        "boost":1
                                                    }
                                                }
                                            ],
                                            "adjust_pure_negative":true,
                                            "boost":1
                                        }
                                    },
                                    "path":"lisCheckJson",
                                    "ignore_unmapped":false,
                                    "score_mode":"none",
                                    "boost":1
                                }
                            }
                        ],
                        "adjust_pure_negative":true,
                        "boost":1
                    }
                },
                {
                    "terms":{
                        "deptId":[
                            7,
                            11,
                            12,
                            18,
                            35,
                            38,
                            40,
                            41,
                            42,
                            43,
                            44,
                            45,
                            46,
                            47,
                            51,
                            52,
                            53,
                            54,
                            55,
                            60,
                            61,
                            62,
                            63,
                            64,
                            65,
                            74,
                            75
                        ],
                        "boost":1
                    }
                }
            ],
            "adjust_pure_negative":true,
            "boost":1
        }
    },
    "sort":[
        {
            "warehouseTime":{
                "order":"asc"
            }
        },
        {
            "donorId":{
                "order":"desc"
            }
        },
        {
            "sampleType":{
                "order":"desc"
            }
        }
    ],
    "track_total_hits":2147483647
}
已邀请:

duanxiaobiao - 90 后IT男

赞同来自: meowCoder123

{
  
    "query":{
        "nested": {
          "inner_hits":{},
          "path": "lisCheckJson",
          "query": {
            "bool":{
            "must":[
                {
                    "bool":{
                        "should":[
                            {
                                "nested":{
                                    "query":{
                                        "bool":{
                                            "should":[
                                                {
                                                  "bool":{
                                                     "filter":[
                                                        {
                                                          "term":{
                                                              "lisCheckJson.lisCheckItemList.itemName": "2"
                                                          }
                                                        },
                                                        {
                                                          "term":{
                                                              "lisCheckJson.lisCheckItemList.resultChar": "8.3"
                                                          }
                                                        }
                                                     ]
                                                    }
                                                  }
                                              ],
                                              
                                            "adjust_pure_negative": true,
                                            "boost": 1
                                        }
                                    },
                                    "path": "lisCheckJson.lisCheckItemList",
                                    "ignore_unmapped": false,
                                    "score_mode": "none",
                                    "boost": 1
                                }
                            },
                            {
                                "nested":{
                                    "query":{
                                        "bool":{
                                            "should":[
                                                {
                                                  "bool":{
                                                     "filter":[
                                                        {
                                                          "term":{
                                                              "lisCheckJson.lisCheckItemList.itemName": "3"
                                                          }
                                                        },
                                                        {
                                                          "term":{
                                                              "lisCheckJson.lisCheckItemList.resultChar": "144"
                                                          }
                                                        }
                                                     ]
                                                    }
                                                  }
                                              ],
                                            "adjust_pure_negative": true,
                                            "boost": 1
                                        }
                                    },
                                    "path": "lisCheckJson.lisCheckItemList",
                                    "ignore_unmapped": false,
                                    "score_mode": "none",
                                    "boost": 1
                                }
                            }
                        ],
                        "minimum_should_match": 2,
                        "adjust_pure_negative": true,
                        "boost": 1
                    }
                }
            ],
            "adjust_pure_negative": true,
            "boost": 1
        }
          }
        }
      
    }
}
 
=============结果=============

nested-result.png


nested-inner-hits.png

 
 
 

要回复问题请先登录注册