elasticsearch 查询格式错误,应为[END_OBJECT],但找到[FIELD_NAME]

gorkyyrv  于 2022-11-22  发布在  ElasticSearch
关注(0)|答案(2)|浏览(127)

原始查询如下

{
    "query": {
        "bool": {
            "must": [
                {
                    "has_parent": {
                        "parent_type": "doc",
                        "query": {
                            "bool": {
                                "must": [
                                    {
                                        "terms": {
                                            "id": [
                                                713
                                            ]
                                        }
                                    },
                                    {
                                        "range": {
                                            "created": {
                                                "lte": "now/d"
                                            }
                                        }
                                    },
                                    {
                                        "range": {
                                            "expires": {
                                                "gte": "now/d"
                                            }
                                        }
                                    }
                                ]
                            }
                        }
                    }
                },
                {
                    "term": {
                        "doc_type": "item"
                    }
                },
                {
                    "bool": {
                        "should": [
                            {
                                "term": {
                                    "have_prices": true
                                }
                            },
                            {
                                "term": {
                                    "is_folder": true
                                }
                            }
                        ]
                    }
                }
            ],
            "must_not": {
                "exists": {
                    "field": "folder"
                }
            }
        }
    },
    "sort": [
        {
            "is_folder": {
                "order": "desc"
            }
        },
        {
            "title_low.order": {
                "order": "asc"
            }
        }
    ],
   "size": 1000 
}

我得到了一些结果

"hits": {
        "total": 19,
        "max_score": null,
        "hits": [
            {
                "_index": "prices",
                "_type": "doc",
                "_id": "item-6800004",
                "_score": null,
                "_routing": "1",
                "_source": {
                    "id": 6800004,
                    "id_pricedoc": 713,
                    "title": "\"водка №1\" 1",
                    "title_low": "\"водка №1\" 1",
                    "supplier": {
                        "id": 7831,
                        "type": null
                    },
                    "supplier_nom": {
                        "id": 1375697,
                        "market_nom": {
                            "id": null
                        },
                        "codes": null,
                        "sup_code": "7a6713a5-73c1-3acb-9b62-9e38b2314dce",
                        "manufacturer": {
                            "id": null,
                            "title": null
                        }
                    },
                    "is_folder": false,
                    "folder": null,
                    "path": null,
                    "pricedoc_created": "2016-03-21",
                    "prices": [
                        {
                            "currency": "RUR",
                            "id_offer": 15735967,
                            "id_prcknd": 167,
                            "value": "391.50"
                        }
                    ],
                    "have_prices": true,
                    "market": {
                        "title": null,
                        "uuid": null,
                        "folder": null,
                        "path": null
                    },
                    "_join_field_name": "doc_type",
                    "doc_type": {
                        "name": "item",
                        "parent": "doc-713"
                    }
                },
                "sort": [
                    0,
                    "\"водка №1\" 1"
                ]
            }

现在,我还想在“id_prcknd”处获得结果:167
修改后的查询如下所示

{
    "query": {
        "bool": {
            "must": [
                {
                    "has_parent": {
                        "parent_type": "doc",
                        "query": {
                            "bool": {
                                "must": [
                                    {
                                        "terms": {
                                            "id": [
                                                713
                                            ]
                                        }
                                    },
                                    {
                                        "range": {
                                            "created": {
                                                "lte": "now/d"
                                            }
                                        }
                                    },
                                    {
                                        "range": {
                                            "expires": {
                                                "gte": "now/d"
                                            }
                                        }
                                    }
                                ]
                            }
                        }
                    }
                },
                {
                    "term": {
                        "doc_type": "item"
                    }
                },
                {
                    "bool": {
                        "should": [
                            {
                                "term": {
                                    "have_prices": true
                                }
                            },
                            {
                                "term": {
                                    "is_folder": true
                                }
                            }
                        ]
                    }
                }
            ],
            "must_not": {
                "exists": {
                    "field": "folder"
                }
            }
        },
        "nested": {
                "path": "prices",
                "query": {
                    "bool": {
                        "must": [
                            {
                                "match": {
                                    "prices.id_prcknd": 167
                                }
                            }
                        ]
                    }
                }
    },
    "sort": [
        {
            "is_folder": {
                "order": "desc"
            }
        },
        {
            "title_low.order": {
                "order": "asc"
            }
        }
    ],
   "size": 1000 
}}

但我得到了一个错误Elasticsearch格式错误的查询,预期[END_OBJECT],但找到[FIELD_NAME]我哪里错了?我想匹配对象,其中“id_prcknd”:167 stackoverflow说我发布的主要是代码,但这是因为ElasticSearch中的大量查询。

xn1cxnb4

xn1cxnb41#

Elasticsearch想说的是,它不希望在字典中看到其他键,而只看到bool"query"下的值)。
在您的示例代码中,代码如下:

{
    "query": {
        "bool": {
            "must": [
                {...},
                {...},
                {...}
            ],
            "must_not": {...},
        "nested": {...}, // this should go under "must"
        "sort": [...],   // this should go on the same level as "query"
        "size": 1000     // this should go on the same level as "query"
    }
}

这里的"bool"指的是bool查询,并且应该是字典中的唯一键。
你应该做的是把"nested"移到它自己的字典和must数组的第四个元素中(如果我理解了你要实现的逻辑的话)。请注意,"sort""size"也应该被移动--这次,和"query"移到同一个级别。
完整的查询如下所示:

{
    "query": {
        "bool": {
            "must": [
                {
                    "has_parent": {
                        "parent_type": "doc",
                        "query": {
                            "bool": {
                                "must": [
                                    {
                                        "terms": {
                                            "id": [
                                                713
                                            ]
                                        }
                                    },
                                    {
                                        "range": {
                                            "created": {
                                                "lte": "now/d"
                                            }
                                        }
                                    },
                                    {
                                        "range": {
                                            "expires": {
                                                "gte": "now/d"
                                            }
                                        }
                                    }
                                ]
                            }
                        }
                    }
                },
                {
                    "term": {
                        "doc_type": "item"
                    }
                },
                {
                    "bool": {
                        "should": [
                            {
                                "term": {
                                    "have_prices": true
                                }
                            },
                            {
                                "term": {
                                    "is_folder": true
                                }
                            }
                        ]
                    }
                },
                {
                    "nested": {
                        "path": "prices",
                        "query": {
                            "bool": {
                                "must": [
                                    {
                                        "match": {
                                            "prices.id_prcknd": 167
                                        }
                                    }
                                ]
                            }
                        }
                    }
                }
            ],
            "must_not": {
                "exists": {
                    "field": "folder"
                }
            }
        }
    },
    "sort": [
        {
            "is_folder": {
                "order": "desc"
            }
        },
        {
            "title_low.order": {
                "order": "asc"
            }
        }
    ],
    "size": 1000
}
esbemjvw

esbemjvw2#

您的json不正确:错误:结尾处应为结束的}[代码22,结构183]
例如,使用json验证器(https://jsonformatter.curiousconcept.com/)。

相关问题