def retrieve_document_by_intent(search_query, intent):
    relations = retrieve_query_relations(search_query, intent)
    recommends = {}
    document_count = 0
    for relation in relations:

        if document_count > 10:
            break

        transit_query = DBSession.query(Query).filter(
            Query.id == relation.second_id).first()
        # 1クエリにつき上位3件だけ取得
        d_relations = DBSession.query(DocumentRelation).filter(
            DocumentRelation.query_id == transit_query.id).order_by(
                desc(DocumentRelation.click_count)).limit(3).all()
        for d_relation in d_relations:
            document = DBSession.query(Document).filter(
                Document.id == d_relation.document_id).first()
            data = {
                'title': document.title,
                'url': document.url,
            }
            recommends.setdefault(transit_query.query, [])
            recommends[transit_query.query].append(data)
            document_count += 1

    return recommends
def register_queries_relation(first_query, queries, intent):
    for q in queries:
        p_query = DBSession.query(Query).filter(Query.query == first_query).first()
        n_query = DBSession.query(Query).filter(Query.query == q).first()

        relation = DBSession.query(Relation).filter(Relation.first_id == p_query.id).filter(
            Relation.second_id == n_query.id).first()

        if relation is None:
            relation = Relation(p_query.id, n_query.id, intent)

        relation.count += 1
        DBSession.add(relation)
def register_queries_relation(first_query, queries, intent):
    for q in queries:
        p_query = DBSession.query(Query).filter(
            Query.query == first_query).first()
        n_query = DBSession.query(Query).filter(Query.query == q).first()

        relation = DBSession.query(Relation).filter(
            Relation.first_id == p_query.id).filter(
                Relation.second_id == n_query.id).first()

        if relation is None:
            relation = Relation(p_query.id, n_query.id, intent)

        relation.count += 1
        DBSession.add(relation)
def retrieve_query_relations(search_query, intent):
    sql_query = DBSession.query(Relation).filter(
        Relation.first_id == search_query.id)
    sql_query = sql_query.filter(Relation.intent == intent)
    relations = sql_query.order_by(desc(Relation.count)).all()

    return relations
def retrieve_document_by_intent(search_query, intent):
    relations = retrieve_query_relations(search_query, intent)
    recommends = {}
    document_count = 0
    for relation in relations:

        if document_count > 10:
            break

        transit_query = DBSession.query(Query).filter(Query.id == relation.second_id).first()
        # 1クエリにつき上位3件だけ取得
        d_relations = DBSession.query(DocumentRelation).filter(DocumentRelation.query_id == transit_query.id).order_by(
            desc(DocumentRelation.click_count)).limit(3).all()
        for d_relation in d_relations:
            document = DBSession.query(Document).filter(Document.id == d_relation.document_id).first()
            data = {
                'title': document.title,
                'url': document.url,
            }
            recommends.setdefault(transit_query.query, [])
            recommends[transit_query.query].append(data)
            document_count += 1

    return recommends
def retrieve_recommend_data(request):
    query = request.GET.get('query', '')
    intent = request.GET.get('intent', '')

    # 0が絞込、1が汎化、2が関連
    search_query = DBSession.query(Query).filter(Query.query == query).first()
    if search_query is None:
        return [{}, {}, {}]

    if intent != '':
        recommend = [retrieve_document_by_intent(search_query, intent)]
    else:
        recommend = [
            retrieve_document_by_intent(search_query, Relation.SPECIFY),
            retrieve_document_by_intent(search_query, Relation.GENERALIZE),
            retrieve_document_by_intent(search_query, Relation.PARALLEL)
        ]

    return {'recommends': recommend}
def retrieve_recommend_data(request):
    query = request.GET.get('query', '')
    intent = request.GET.get('intent', '')

    # 0が絞込、1が汎化、2が関連
    search_query = DBSession.query(Query).filter(Query.query == query).first()
    if search_query is None:
        return [{}, {}, {}]

    if intent != '':
        recommend = [
            retrieve_document_by_intent(search_query, intent)
        ]
    else:
        recommend = [
            retrieve_document_by_intent(search_query, Relation.SPECIFY),
            retrieve_document_by_intent(search_query, Relation.GENERALIZE),
            retrieve_document_by_intent(search_query, Relation.PARALLEL)
        ]

    return {
        'recommends': recommend
    }
def register_query(json):
    # クエリを登録する
    for k, v in json.items():

        query = DBSession.query(Query).filter(Query.query == k).first()
        if query is None:
            query = Query(k)
        query.count += 1

        DBSession.add(query)
        DBSession.flush()

        documents = v['documents']
        # クリックしたドキュメントを登録
        for doc in documents:
            document = DBSession.query(Document).filter(Document.url == doc['url']).first()
            if document is None:
                document = Document(doc['url'], doc['title'], '')
            DBSession.add(document)
            DBSession.flush()

            d_relation = DBSession.query(DocumentRelation).filter(DocumentRelation.query_id == query.id).filter(
                DocumentRelation.document_id == document.id).first()
            if d_relation is None:
                d_relation = DocumentRelation(query.id, document.id)
            d_relation.click_count += 1
            DBSession.add(d_relation)
def retrieve_query_relations(search_query, intent):
    sql_query = DBSession.query(Relation).filter(Relation.first_id == search_query.id)
    sql_query = sql_query.filter(Relation.intent == intent)
    relations = sql_query.order_by(desc(Relation.count)).all()

    return relations
def register_query(json):
    # クエリを登録する
    for k, v in json.items():

        query = DBSession.query(Query).filter(Query.query == k).first()
        if query is None:
            query = Query(k)
        query.count += 1

        DBSession.add(query)
        DBSession.flush()

        documents = v['documents']
        # クリックしたドキュメントを登録
        for doc in documents:
            document = DBSession.query(Document).filter(
                Document.url == doc['url']).first()
            if document is None:
                document = Document(doc['url'], doc['title'], '')
            DBSession.add(document)
            DBSession.flush()

            d_relation = DBSession.query(DocumentRelation).filter(
                DocumentRelation.query_id == query.id).filter(
                    DocumentRelation.document_id == document.id).first()
            if d_relation is None:
                d_relation = DocumentRelation(query.id, document.id)
            d_relation.click_count += 1
            DBSession.add(d_relation)