超级复杂的 es bool 查询

2020-08-06  本文已影响0人  潘旭
    def _build_post_data(self, keywords: List[str] = None):

        query = {
            "sort": [
                {
                    "publish_time_str": {
                        "order": "desc"
                    }
                },
                {
                    "read_count": {
                        "order": "desc"
                    }
                }
            ],
            "size": 10
        }

        if keywords:
            query["query"] = {
                "bool": {
                    "must": [
                        {
                            "range": {
                                "publish_time_str": {"from": "2020-08-01 00:00:00",
                                                     "to": "2020-08-06 23:00:00"}
                            }
                        },

                        {
                            "bool": {
                                "should": [

                                ]
                            }
                        }
                    ]
                }
            }

            should = query["query"]["bool"]["must"][1]["bool"]["should"]

            for keyword in keywords:
                match = {"match_phrase": {
                    "title": keyword
                }}
                should.append(match)

        return json.dumps(query)
上一篇 下一篇

猜你喜欢

热点阅读