在ElascicSearch中查找空嵌套的已归档文档



我想查找preices.info文件等于null的文档。价格是一个嵌套的字段查询的例子看起来像这个

    {
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "doc_type": "supnom"
                    }
                },
                {
                    "term": {
                        "is_linking_disabled": false
                    }
                },
                {
                    "has_parent": {
                        "parent_type": "sup",
                        "query": {
                            "bool": {
                                "must": [
                                    {
                                        "term": {
                                            "id": 89634082
                                        }
                                    }
                                ]
                            }
                        }
                    }
                },
                {
                    "bool": {
                        "should": [
                            {
                                "bool": {
                                    "must": [
                                        {
                                            "term": {
                                                "folder_type": 0
                                            }
                                        },
                                        {
                                            "term": {
                                                "folder": "NULL"
                                            }
                                        },
                                        {
                                            "term": {
                                                "is_folder": true
                                            }
                                        },
                                        {
                                            "term": {
                                                "is_folder_hybrid": true
                                            }
                                        }
                                    ]
                                }
                            },
                            {
                                "bool": {
                                    "must": [
                                        {
                                            "term": {
                                                "folder_type": 1
                                            }
                                        },
                                        {
                                            "term": {
                                                "folder": "89634082-NULL"
                                            }
                                        },
                                        {
                                            "term": {
                                                "is_folder": true
                                            }
                                        },
                                        {
                                            "term": {
                                                "is_folder_hybrid": true
                                            }
                                        }
                                    ]
                                }
                            },
                            {
                                "bool": {
                                    "must": [
                                        {
                                            "term": {
                                                "is_folder": false
                                            }
                                        },
                                        {
                                            "term": {
                                                "folder_hybrid": "NULL"
                                            }
                                        },
                                        {
                                            "nested": {
                                                "path": "prices",
                                                "query": {
                                                    "terms": {
                                                        "prices.id_prcknd": [
                                                            22215
                                                        ]
                                                    }
                                                }
                                            }
                                        }
                                    ]
                                }
                            },
                            {
                                "bool": {
                                    "must_not": [
                                        {
                                            "nested": {
                                                "path": "prices",
                                                "query": {
                                                    "bool": {
                                                        "filter": {
                                                            "exists": {
                                                                "field": "prices.info"
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    ]
                                }
                            }
                        ]
                    }
                }
            ],
            "must_not": [],
            "should": []
        }
    },
    "size": 70,
    "from": 0,
    "_source": true,
    "sort": [
        {
            "is_folder": {
                "order": "desc"
            }
        },
        {
            "title_low.order": {
                "order": "asc"
            }
        }
    ]
}

无论如何,我在结果文档中得到了NOT null prices.info字段

{
"took": 65,
"timed_out": false,
"_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
},
"hits": {
    "total": 7,
    "max_score": null,
    "hits": [
        {
            "_index": "supnoms",
            "_type": "doc",
            "_id": "supnom-108340976",
            "_score": null,
            "_routing": "1",
            "_source": {
                "codes": null,
                "folder": null,
                "folder_hybrid": null,
                "folder_hybrid_type": 0,
                "folder_type": null,
                "id": "108340976",
                "id_supplier": 89634082,
                "is_folder": false,
                "is_folder_hybrid": null,
                "is_linking_disabled": false,
                "manufacturer": {
                    "id": null,
                    "title": null
                },
                "market_nom": {
                    "folder": null,
                    "id": null
                },
                "path_market": {
                    "parents": null,
                    "pids": null,
                    "strpath": null
                },
                "path_supnom": {
                    "parents": null,
                    "pids": null,
                    "strpath": null
                },
                "prices": [
                    {
                        "currency": "RUR",
                        "id_offer": 605966502,
                        "id_prcdoc": 42040,
                        "id_prcknd": 22215,
                        "info": {
                            "Price": "По запросу"
                        },
                        "min_order": null,
                        "sell_by": null,
                        "value": "По запросу"
                    }
                ],
                "sup_code": "c39f81c9-1a69-3a7d-9e04-5f4e124242e7",
                "title": "баклажан UPC",
                "title_low": "баклажан upc",
                "_join_field_name": "doc_type",
                "doc_type": {
                    "name": "supnom",
                    "parent": "sup-89634082"
                }
            },
            "sort": [
                0,
                "баклажан upc"
            ]
        },
        {
            "_index": "supnoms",
            "_type": "doc",
            "_id": "supnom-108340975",
            "_score": null,
            "_routing": "1",
            "_source": {
                "codes": null,
                "folder": null,
                "folder_hybrid": null,
                "folder_hybrid_type": 0,
                "folder_type": null,
                "id": "108340975",
                "id_supplier": 89634082,
                "is_folder": false,
                "is_folder_hybrid": null,
                "is_linking_disabled": false,
                "manufacturer": {
                    "id": null,
                    "title": null
                },
                "market_nom": {
                    "folder": null,
                    "id": null
                },
                "path_market": {
                    "parents": null,
                    "pids": null,
                    "strpath": null
                },
                "path_supnom": {
                    "parents": null,
                    "pids": null,
                    "strpath": null
                },
                "prices": [
                    {
                        "currency": "RUR",
                        "id_offer": 605966498,
                        "id_prcdoc": 42040,
                        "id_prcknd": 22215,
                        "info": {},
                        "min_order": null,
                        "sell_by": null,
                        "value": "10.00"
                    }
                ],
                "sup_code": "8be655dc-0873-3dfc-994b-602b2e4e0799",
                "title": "Избранные для дубля без цены",
                "title_low": "избранные для дубля без цены",
                "_join_field_name": "doc_type",
                "doc_type": {
                    "name": "supnom",
                    "parent": "sup-89634082"
                }
            },
            "sort": [
                0,
                "избранные для дубля без цены"
            ]
        },
        {
            "_index": "supnoms",
            "_type": "doc",
            "_id": "supnom-108340974",
            "_score": null,
            "_routing": "1",
            "_source": {
                "codes": null,
                "folder": null,
                "folder_hybrid": null,
                "folder_hybrid_type": 0,
                "folder_type": null,
                "id": "108340974",
                "id_supplier": 89634082,
                "is_folder": false,
                "is_folder_hybrid": null,
                "is_linking_disabled": false,
                "manufacturer": {
                    "id": null,
                    "title": null
                },
                "market_nom": {
                    "folder": null,
                    "id": null
                },
                "path_market": {
                    "parents": null,
                    "pids": null,
                    "strpath": null
                },
                "path_supnom": {
                    "parents": null,
                    "pids": null,
                    "strpath": null
                },
                "prices": [
                    {
                        "currency": "RUR",
                        "id_offer": 605966500,
                        "id_prcdoc": 42040,
                        "id_prcknd": 22215,
                        "info": {},
                        "min_order": null,
                        "sell_by": null,
                        "value": "10.00"
                    }
                ],
                "sup_code": "867f138d-d8cc-37e7-b1b2-f07a4627ba49",
                "title": "Избранные для дубля с ценой",
                "title_low": "избранные для дубля с ценой",
                "_join_field_name": "doc_type",
                "doc_type": {
                    "name": "supnom",
                    "parent": "sup-89634082"
                }
            }

指数中的价格字段如下"价格":{"类型":"嵌套","属性":{"货币":{"type":"关键字"},"id_offer":{"type":"integer"},"id_prcdoc":{"type":"integer"},"id_prcknd":{"type":"integer"},"最小订单(_O(":{"type":"关键字"},"sell_by":{"type":"关键字"},"值":{"类型":"关键字","index":false}}}

从您的查询中,您希望查看所有没有prices.info值的文档

只需将您必须的must_not部分移动到您所拥有的bool查询的第一级。请参阅以下内容以获得更清晰的信息:

原始查询:

{
  "query": {
    "bool": {
      "must": [
        { term }
        { term }
        { hasparent }
        { bool 
          - should 
             - must
             - must
             - must
             - must_not         <---- Remove this 
                - { exists }    <---- Copy this
        }
      ],
      "must_not": [],           <---- And add that here
      "should": [
        {}
      ]
    }
  }
}

Psuedo代码解决方案:

{
  "query": {
    "bool": {
      "must": [
        { term }
        { term }
        { hasparent }
        { bool 
          - should 
             - must
             - must
             - must
        }
      ],
      "must_not": [
        { exists }     <--- Note this    
      ],
      "should": [
        {}
      ]
    }
  }
}

查询DSL

以下是您正在查找的查询:

{
   "query":{
      "bool":{
         "must":[
            {
               "term":{
                  "doc_type":"supnom"
               }
            },
            {
               "term":{
                  "is_linking_disabled":false
               }
            },
            {
               "has_parent":{
                  "parent_type":"sup",
                  "query":{
                     "bool":{
                        "must":[
                           {
                              "term":{
                                 "id":89634082
                              }
                           }
                        ]
                     }
                  }
               }
            },
            {
               "bool":{
                  "should":[
                     {
                        "bool":{
                           "must":[
                              {
                                 "term":{
                                    "folder_type":0
                                 }
                              },
                              {
                                 "term":{
                                    "folder":"NULL"
                                 }
                              },
                              {
                                 "term":{
                                    "is_folder":true
                                 }
                              },
                              {
                                 "term":{
                                    "is_folder_hybrid":true
                                 }
                              }
                           ]
                        }
                     },
                     {
                        "bool":{
                           "must":[
                              {
                                 "term":{
                                    "folder_type":1
                                 }
                              },
                              {
                                 "term":{
                                    "folder":"89634082-NULL"
                                 }
                              },
                              {
                                 "term":{
                                    "is_folder":true
                                 }
                              },
                              {
                                 "term":{
                                    "is_folder_hybrid":true
                                 }
                              }
                           ]
                        }
                     },
                     {
                        "bool":{
                           "must":[
                              {
                                 "term":{
                                    "is_folder":false
                                 }
                              },
                              {
                                 "term":{
                                    "folder_hybrid":"NULL"
                                 }
                              },
                              {
                                 "nested":{
                                    "path":"prices",
                                    "query":{
                                       "terms":{
                                          "prices.id_prcknd":[
                                             22215
                                          ]
                                       }
                                    }
                                 }
                              }
                           ]
                        }
                     }
                  ]
               }
            }
         ],
         "must_not":[
            {
               "nested":{
                  "path":"prices",
                  "query":{
                     "exists":{
                        "field":"prices.info"
                     }
                  }
               }
            }
         ],
         "should":[
         ]
      }
   },
   "size":70,
   "from":0,
   "_source":true,
   "sort":[
      {
         "is_folder":{
            "order":"desc"
         }
      },
      {
         "title_low.order":{
            "order":"asc"
         }
      }
   ]
}

请注意,您不应该看到任何具有prices.info字段的文档。还要注意,我已经简化了查询以使其保持简单。

让我知道这是否有效!

最新更新