【问题标题】:malformed query, expected [END_OBJECT] but found [FIELD_NAME]格式错误的查询,应为 [END_OBJECT],但找到了 [FIELD_NAME]
【发布时间】:2020-02-26 20:32:30
【问题描述】:

原来的查询是这样的

{
    "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"
                ]
            }

现在我也想得到结果 where "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


    【解决方案1】:

    您的 json 不正确:Error:Expecting close } at end[Code 22, Structure 183]

    例如使用 json 验证器 (https://jsonformatter.curiousconcept.com/)。

    【讨论】:

    • 你是对的,我没有复制它,我编辑了答案,谢谢
    【解决方案2】:

    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
    }
    

    希望有帮助!

    【讨论】:

      猜你喜欢
      • 2021-04-22
      • 2018-01-30
      • 2018-02-10
      • 2018-04-25
      • 1970-01-01
      • 2022-10-25
      • 2022-01-14
      • 2017-12-01
      • 2020-08-30
      相关资源
      最近更新 更多