def search(): if not (is_authenticated()): flash('Sorry you are not logged in. Login to continue') return redirect(url_for('auth.login')) if not g.search_form.validate(): return redirect(url_for('dashboard.dashboard')) if not (elasticsearch): flash("Search is not available now.") return redirect(url_for('dashboard.dashboard')) groups, total_groups = query_index('group_table', g.search_form.q.data, 1, 5) messages, total_message = query_index('message', g.search_form.q.data, 1, 5) users, total_users = query_index('user_table', g.search_form.q.data, 1, 5) current_user_group_id = get_list_of_group_id() groups_searched = [] for group in groups: if (group in current_user_group_id): groups_searched.append( GroupTable.query.filter_by(id=group).first()) users_searched = [] for user in users: users_searched.append(UserTable.query.filter_by(id=user).first()) message_searched = [] for message in messages: if (Message.query.filter_by( id=message).first().user_id == current_user.id): message_searched.append( Message.query.filter_by(id=message).first()) return render_template('search.html', title='Search', users=users_searched, groups=groups_searched, messages=message_searched)
def search(cls, expression, page, per_page): """ Class method to execute an Elasticsearch search of the input "expression" for the associated input "cls" (with input options values "page" and "per_page") :param cls: a class :type cls: class :param expression: the searched text :type expression: str :param page: the page number from the query results :type page: int :param per_page: the number of results per page from the query results :type per_page: int :return: ... :rtype: tuple(, int) """ ids, total = query_index(cls.__tablename__, expression, page, per_page) if total == 0: return cls.query.filter_by(id=0), 0 when = [] for list_index, list_value in enumerate(ids): when.append((list_value, list_index)) return cls.query.filter(cls.id.in_(ids)).order_by( db.case(when, value=cls.id)), total
def search(cls, expression, page, per_page): ids, total = query_index(cls.__tablename__, expression, page, per_page) if total == 0: return cls.query.filter_by(id=0), 0 when = [(id, i) for i, id in enumerate(ids)] return cls.query.filter(cls.id.in_(ids)).order_by( db.case(when, value=cls.id)), total
def search(search_text): likes_all = Likes.query.with_entities( Likes.movieid).filter_by(userid=current_user.userid).all() likes = [i[0] for i in likes_all] mylist_all = MyList.query.with_entities( MyList.movieid).filter_by(userid=current_user.userid).all() mylist = [i[0] for i in mylist_all] x = query_index('movies', search_text, 1, 100) s = [i['_id'] for i in x] search_results = Movies.query.filter(Movies.movieid.in_(s)).all() form = SearchForm() if form.validate_on_submit(): return redirect(url_for('search', search_text=form.search_field.data)) image_file = url_for('static', filename='profile_pics/' + current_user.image_file) return render_template('search.html', title='Search Results', search_text=search_results, image_file=image_file, form=form, likes=likes, my_list=mylist)
def search(cls, expression, page, per_page): ids, total = query_index(cls.__tablename__, expression, page, per_page) if total == 0: # return empty return cls.query.filter_by(id=0), 0 """ CASE x_field WHEN 'f' THEN 1 WHEN 'p' THEN 2 WHEN 'i' THEN 3 WHEN 'a' THEN 4 ELSE 5 --needed only is no IN clause above. eg when = 'b' END, id case-when-value statements below is equivalent to: supose when[0]=(1000, 1), value=cls.id WHEN cls.id==1000 THEN 1 ensure the return of sql query are the same order in ids. that is, the result are sorted form the most relevant (same as ES's return) """ when = [] for i in range(len(ids)): when.append((ids[i], i)) return cls.query.filter(cls.id.in_(ids)).order_by( db.case(when, value=cls.id)), total
def test(self): create_app() for post in Post.query.all(): add_to_index('post', post) print(post.id, post.body) # remove_from_index('posts', post) print(query_index('post', '今天', 1, 100))
def search(cls, expression, page, per_page): ids, total = query_index(cls.__tablename__, expression, page, per_page) if total == 0: return cls.query.filter_by(id=0), 0 when = [] for i in range(len(ids)): when.append((ids[i], i)) return cls.query.filter(cls.id.in_(ids)).order_by(db.case(when, value=Post.id)), total
def search(email_id): email = query_index(cls.__tablename__, email) if total == 0: return cls.query.filter_by(id=0), 0 when = [] for i in range(len(ids)): when.append((ids[i], i)) return cls.query.filter(cls.email_id.in_(ids)).order_by( db.case(when, value=cls.email_id)), total
def search(cls, expression, page, per_page): ids, total = query_index(cls.__tablename__, expression, page, per_page) if total == 0: return cls.query.filter_by(id=0), 0 when = [] for i in range(len(ids)): when.append((ids[i], i)) return cls.query.filter(cls.id.in_(ids)).order_by( db.case(when, value=cls.id)), total
def search(cls, expression, page, per_page): ids, total = query_index(cls.__tablename__, expression, page, per_page) if total == 0: return cls.query.filter_by(id=0), 0 when = [] for counter, value in enumerate(ids): when.append((value, counter)) return cls.query.filter(cls.id.in_(ids)).order_by( db.case(when, value=cls.id)), total
def search(cls, expression, page, per_page): # cls.search, not self.search ids, total = query_index(cls.__tablename__, expression, page, per_page) if total == 0: return cls.query.filter_by(id=0), 0 when = [] # this list must be called "when" for i in range(len(ids)): when.append((ids[i], i)) return cls.query.filter(cls.id.in_(ids)).order_by( db.case(when, value=cls.id)), total
def search(cls, expression, page, per_page): # calls query_index() with cls.__tablename__ to use SQLAlchemy table names as index names in Elastic ids, total = query_index(cls.__tablename__, expression, page, per_page) #cls used instead of self because this method recieves a class, not an instance of it's first arg. if total == 0: # for instance, once Post class is attached to search() as Post.search() we don't need to have an instance of the Post class return cls.query.filter_by(id=0), 0 when = [] for i in range(len(ids)): when.append((ids[i], i)) return cls.query.filter(cls.id.in_(ids)).order_by(db.case(when, value=cls.id)), total
def search(cls, expression, page, per_page): # Wrapper for the query_index() function from app/search.py. It replaces id's by actual objects. ids, total = query_index(cls.__tablename__, expression, page, per_page) if total == 0: return cls.query.filter_by(id=0), 0 when = [] for i in range(len(ids)): when.append((ids[i], i)) return cls.query.filter(cls.id.in_(ids)).order_by( db.case(when, value=cls.id)), total
def search(cls, expression, page, per_page): ids, total = query_index(cls.__tablename__, expression, page, per_page) # all indexes will be named with name Flask-SQLAlchemy assigned to the relational table. if total == 0: return cls.query.filter_by(id=0), 0 when = [] for i in range(len(ids)): when.append((ids[i], i)) return cls.query.filter(cls.id.in_(ids)).order_by( db.case(when, value=cls.id)), total
def search(cls, expression, page, per_page): """ Replaces the list of object IDs with actual objects. """ ids, total = query_index(cls.__tablename__, expression, page, per_page) if total == 0: return cls.query.filter_by(id=0), 0 when = [] for i in range(len(ids)): when.append((ids[i], i)) return cls.query.filter(cls.id.in_(ids)).order_by( db.case(when, value=cls.id)), total
def search(cls, expression, page, per_page): ids, total = query_index(cls.__tablename__, expression, page, per_page) if total == 0: return cls.query.filter_by(id=0), 0 when = [] for i in range(len(ids)): when.append((ids[i], i)) # case sql - results from the database come in the same order as the IDs are given return cls.query.filter(cls.id.in_(ids)).order_by( db.case(when, value=cls.id)), total
def search(cls, qstring): ids, total = query_index(qstring) if total == 0: return cls.query.filter_by(id=0), 0 when = [] for i in range(len(ids)): when.append((ids[i], i)) return cls.query.filter(cls.id.in_(ids)).order_by( db.case(when, value=cls.id)), total
def search(cls, expression, page, per_page): """""" ids, total = query_index(cls.__tablename__, expression, page, per_page) if total == 0: return cls.query.filter_by(id=0), 0 when = [] for i in range(len(ids)): when.append((ids[i], i)) # case 该语句需要用于确保数据库中的结果与给定ID的顺序相同 return cls.query.filter(cls.id.in_(ids)).order_by( db.case(when, value=cls.id)), total
def search(cls, expression: str, page: int, per_page: int) -> Tuple[List[Tuple[int]], int]: ids, total = query_index(cls.__tablename__, expression, page, per_page) if total == 0: return (cls.query.filter_by(id=0), 0) when = [] for i in range(len(ids)): when.append((ids[i], i)) query_set: List[Tuple[int]] = cls.query.filter( cls.id.in_(ids)).order_by(db.case(when, value=cls.id)) return (query_set, total)
def search(cls, expression, page, per_page): # first argument is a class, for example Post model ids, total = query_index(cls.__tablename__, expression, page, per_page) if total == 0: return cls.query.filter_by(id=0), 0 when = [ ] # list of tuples, where tuples are pairs of id index and id value at that index for i in range(len(ids)): when.append((ids[i], i)) return cls.query.filter(cls.id.in_(ids)).order_by( db.case(when, value=cls.id)), total
def search(cls, expression, amount): ids, total = query_index(cls.__tablename__, expression, amount=amount) if total == 0: return cls.query.filter_by(id=0).all(), 0 when = [] for i, id in enumerate(ids): when.append((id, i)) return ( cls.query.filter(cls.id.in_(ids)).order_by( db.case(when, value=cls.id)).all(), total, )
def search(cls, expression, page, per_page): # 返回结果ID列表和总数 ids, total = query_index(cls.__tablename__, expression, page, per_page) if total == 0: return cls.query.filter_by(id=0), 0 when = [] for i in range(len(ids)): when.append((ids[i], i)) return cls.query.filter(cls.id.in_(ids)).order_by( # 使用case语句,确保数据库中的结果与给定ID的顺序相同 # 因为使用elasticsearch搜索返回的结果不是有序的 db.case(when, value=cls.id)), total
def search(cls, expression, page, per_page): try: ids, total = query_index(cls.__tablename__, expression, page, per_page) if total == 0: return cls.query.filter_by(id=0), 0 when = [] for i in range(len(ids)): when.append((ids[i], i)) return cls.query.filter(cls.id.in_(ids)).order_by(db.case(when, value=cls.id)), total except Exception as error: flash(f'Search server not running.') return [], 0
def search(cls, expression, to_search): __doctype__ = cls.__tablename__ if cls.__tablename__ == 'fifa': __doctype__ = 'fifa_players' ids, total = query_index(cls.__tablename__, __doctype__, expression, to_search) if total == 0: return cls.query.filter_by(id=0), 0 when = [] for i in range(len(ids)): when.append((ids[i], i)) return cls.query.filter(cls.id.in_(ids)).order_by( db.case(when, value=cls.id)), total
def search(cls, expression, page, per_page): #cls is the model class in question """invoke a search in the elasticsearch database and converts the recieved indices into a SQLalchemy object""" ids, total = query_index(cls.__tablename__, expression, page, per_page) if total == 0: return cls.query.filter_by(id=0), 0 when = [] for i in range(len(ids)): when.append( (ids[i], i)) #maintains order from elasticsearch search return cls.query.filter(cls.id.in_(ids)).order_by( db.case(when, value=cls.id) ), total #filters database queries to only select the indices included in search results
def search(cls, expression, page, per_page): ids, total = query_index(cls.__tablename__, expression, page, per_page) if total == 0: # No results found, return 0 return cls.query.filter_by(id=0), 0 when = [] # Fabricating `when` statement for the query for i in range(len(ids)): when.append((ids[i], i)) # when = [({ '_index': '', '_id': '', '_score': ''}, 1), ({ } ,2), ...] # order_by/case syntax: https://goo.gl/t8mwSR return cls.query.filter(cls.id.in_(ids)).order_by( db.case(when, value=cls.id)), total
def search(cls, expression, page, per_page): """ 将查询返回的动态ID,以及查询到的总数,重新组装数据 返回 查询到的模型,查询到的总数 """ ids, total = query_index(cls.__tablename__, expression, page, per_page) if total == 0: return cls.query.filter_by(id=0), 0 when = [] for i in range(len(ids)): when.append((ids[i], i)) return cls.query.filter(cls.id.in_(ids)).order_by( db.case(when, value=cls.id)), total
def search(cls, expression, page, per_page): """ query_index возвращает список ID результатов и их общее количество. Функция search() возвращает запрос, который заменяет список идентификаторов, а также передает общее количество результатов поиска в качестве второго возвращаемого значения. """ ids, total = query_index(cls.__tablename__, expression, page, per_page) if total == 0: return cls.query.filter_by(id=0), 0 when = [] for i in range(len(ids)): when.append((ids[i], i)) return cls.query.filter(cls.id.in_(ids)).order_by( db.case(when, value=cls.id)), total
def search(cls, expression): page = 1 per_page = 200 ids, total = query_index(cls.__tablename__, expression, page, per_page) if total == 0: # print('why is the total 0') # print(ids) return cls.query.filter_by( id=0), 0 # put a filter by date option later when = [] for i in range(len(ids)): when.append((ids[i], i)) return cls.query.filter(cls.id.in_(ids)).order_by( db.case(when, value=cls.id)), total
def search(cls, expression, page, per_page): # cls.__tablename__ = Flask-SQLAlchemy relational table name # ids = list of result IDs & total = total number of results ids, total = query_index(cls.__tablename__, expression, page, per_page) if total == 0: return cls.query.filter_by(id=0), 0 when = [] for i in range(len(ids)): when.append((ids[i], i)) # db.case() is used to retrieve a list of objects by their id. return cls.query.filter(cls.id.in_(ids)).order_by( db.case(when, value=cls.id)), total
def search(cls, expression, page, per_page): #query the index of the models tablename ids, total = query_index(cls.__tablename__, expression, page, per_page) if total == 0: return cls.query.filter_by(id=0), 0 when = [] #list of tuples (search query id,index) for i in range(len(ids)): when.append((ids[i], i)) #retrieves the list of objects by their IDs, based on a CASE statement from SQL, #needs to be used to ensure that the results from the database come in the same order as the IDs are given. # This is important because the Elasticsearch query returns results sorted from more to less relevant. return cls.query.filter(cls.id.in_(ids)).order_by( db.case(when, value=cls.id)), total