Beispiel #1
0
    def _get_index_search_query():
        """Get index search query."""
        query_q = {
            "from": 0,
            "size": 10000,
            "_source": {
                "excludes": ["content", "_item_metadata"]
            },
            "query": {
                "bool": {
                    "must": [{
                        "match": {
                            "path.tree": "@index"
                        }
                    }, {
                        "match": {
                            "relation_version_is_last": "true"
                        }
                    }]
                }
            },
            "post_filter": {
                "bool": {
                    "must": [{
                        "match": {
                            "publish_status": "0"
                        }
                    }, {
                        "range": {
                            "publish_date": {
                                "lte": "now/d"
                            }
                        }
                    }]
                }
            }
        }

        q = str(index_id)
        if q != str(current_app.config.get("WEKO_ROOT_INDEX",
                                           WEKO_ROOT_INDEX)):
            post_filter = query_q['post_filter']

            if post_filter:
                list_path = Indexes.get_list_path_publish(index_id)
                post_filter['bool']['must'].append(
                    {"terms": {
                        "path": list_path
                    }})
            # create search query
            try:
                fp = Indexes.get_self_path(q)
                query_q = json.dumps(query_q).replace("@index", fp.path)
                query_q = json.loads(query_q)
            except BaseException:
                pass
        else:
            post_filter = query_q['post_filter']

            if post_filter:
                list_path = Indexes.get_list_path_publish(index_id)
                post_filter['bool']['must'].append(
                    {"terms": {
                        "path": list_path
                    }})
            wild_card = []
            child_list = Indexes.get_child_list(q)
            if child_list:
                for item in child_list:
                    wc = {"wildcard": {"path.tree": item.cid}}
                    wild_card.append(wc)
                query_q['query']['bool']['must'] = [{
                    "bool": {
                        "should": wild_card
                    }
                }, {
                    "match": {
                        "relation_version_is_last": "true"
                    }
                }]
        return query_q
Beispiel #2
0
    def _get_index_earch_query():

        query_q = {
            "_source": {
                "excludes": ['content']
            },
            "query": {
                "bool": {
                    "must": [{
                        "match": {
                            "path.tree": "@index"
                        }
                    }, {
                        "match": {
                            "relation_version_is_last": "true"
                        }
                    }]
                }
            },
            "aggs": {
                "path": {
                    "terms": {
                        "field": "path.tree",
                        "include": "@index|@index/[^/]+",
                        "size": "@count"
                    },
                    "aggs": {
                        "date_range": {
                            "filter": {
                                "match": {
                                    "publish_status": "0"
                                }
                            },
                            "aggs": {
                                "available": {
                                    "range": {
                                        "field":
                                        "publish_date",
                                        "ranges": [{
                                            "from": "now+1d/d"
                                        }, {
                                            "to": "now+1d/d"
                                        }]
                                    },
                                }
                            }
                        },
                        "no_available": {
                            "filter": {
                                "bool": {
                                    "must_not": [{
                                        "match": {
                                            "publish_status": "0"
                                        }
                                    }]
                                }
                            }
                        }
                    }
                }
            },
            "post_filter": {}
        }

        q = request.values.get('q') or '0' if index_id is None else index_id

        if q != '0':
            # add item type aggs
            query_q['aggs']['path']['aggs']. \
                update(get_item_type_aggs(search._index[0]))
            if q:
                mut = get_permission_filter(q)
            else:
                mut = get_permission_filter()

            if mut:
                mut = list(map(lambda x: x.to_dict(), mut))
                post_filter = query_q['post_filter']
                if mut[0].get('bool'):
                    post_filter['bool'] = mut[0]['bool']
                else:
                    post_filter['bool'] = {'must': mut}

            # create search query
            if q:
                try:
                    fp = Indexes.get_self_path(q)

                    query_q = json.dumps(query_q).replace("@index", fp.path)
                    query_q = json.loads(query_q)
                except BaseException:
                    pass
            count = str(Indexes.get_index_count())

            query_q = json.dumps(query_q).replace("@count", count)
            query_q = json.loads(query_q)

            return query_q
        else:
            # add item type aggs
            wild_card = []
            child_list = Indexes.get_child_list(q)
            if child_list:
                for item in child_list:
                    wc = {"wildcard": {"path.tree": item.cid}}
                    wild_card.append(wc)

            query_not_q = {
                "_source": {
                    "excludes": ["content"]
                },
                "query": {
                    "bool": {
                        "must": [{
                            "bool": {
                                "should": wild_card
                            }
                        }, {
                            "match": {
                                "relation_version_is_last": "true"
                            }
                        }]
                    }
                },
                "aggs": {
                    "path": {
                        "terms": {
                            "field": "path.tree",
                            "size": "@count"
                        },
                        "aggs": {
                            "date_range": {
                                "filter": {
                                    "match": {
                                        "publish_status": "0"
                                    }
                                },
                                "aggs": {
                                    "available": {
                                        "range": {
                                            "field":
                                            "publish_date",
                                            "ranges": [{
                                                "from": "now+1d/d"
                                            }, {
                                                "to": "now+1d/d"
                                            }]
                                        },
                                    }
                                }
                            },
                            "no_available": {
                                "filter": {
                                    "bool": {
                                        "must_not": [{
                                            "match": {
                                                "publish_status": "0"
                                            }
                                        }]
                                    }
                                }
                            }
                        }
                    }
                },
                "post_filter": {}
            }

            query_not_q['aggs']['path']['aggs']. \
                update(get_item_type_aggs(search._index[0]))

            if q:
                mut = get_permission_filter(q)
            else:
                mut = get_permission_filter()

            if mut:
                mut = list(map(lambda x: x.to_dict(), mut))
                post_filter = query_not_q['post_filter']
                if mut[0].get('bool'):
                    post_filter['bool'] = mut[0]['bool']
                else:
                    post_filter['bool'] = {'must': mut}

            # create search query
            count = str(Indexes.get_index_count())
            query_not_q = json.dumps(query_not_q).replace("@count", count)
            query_not_q = json.loads(query_not_q)

            return query_not_q