From 381e72ee991cc8c5e591f6edf0baf5ed231792a9 Mon Sep 17 00:00:00 2001 From: Mokaddem Date: Wed, 15 Mar 2017 10:07:46 +0100 Subject: [PATCH] Added display of number of elements inside the index + changed variables names --- var/www/Flasks/Flask_search.py | 32 +++++++++++++++++++------------- var/www/templates/search.html | 8 ++++---- var/www/templates/searchbox.html | 2 +- 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/var/www/Flasks/Flask_search.py b/var/www/Flasks/Flask_search.py index 3d28b4cc..231e89d7 100644 --- a/var/www/Flasks/Flask_search.py +++ b/var/www/Flasks/Flask_search.py @@ -11,6 +11,9 @@ import flask from flask import Flask, render_template, jsonify, request import Paste +from whoosh import index +from whoosh.fields import Schema, TEXT, ID +from whoosh.qparser import QueryParser # ============ VARIABLES ============ import Flask_config @@ -40,7 +43,12 @@ def get_index_list(selected_index=""): index_list = [] for dirs in os.listdir(baseindexpath): if os.path.isdir(os.path.join(baseindexpath, dirs)): - index_list.append([ dirs, dirs + " - " + str(get_dir_size(dirs) / (1000*1000)) + " Mb", dirs==selected_index.split('/')[-1]]) + value = dirs + name = dirs + " - " + \ + str(get_dir_size(dirs) / (1000*1000)) + " Mb " + \ + "(" + str(get_item_count(dirs)) + " Items" + ")" + flag = dirs==selected_index.split('/')[-1] + index_list.append([ value, name, flag]) return index_list def get_dir_size(directory): @@ -49,6 +57,10 @@ def get_dir_size(directory): cur_sum += sum(os.path.getsize(os.path.join(directory, name)) for name in files) return cur_sum +def get_item_count(dirs): + ix = index.open_dir(os.path.join(baseindexpath, dirs)) + return ix.doc_count_all() + # ============ ROUTES ============ @@ -61,14 +73,14 @@ def search(): c = [] #preview of the paste content paste_date = [] paste_size = [] - index_num = request.form['index_num'] + index_name = request.form['index_name'] num_elem_to_get = 50 # select correct index - if index_num is None or index_num == "0": + if index_name is None or index_name == "0": selected_index = get_current_index() else: - selected_index = os.path.join(baseindexpath, index_num) + selected_index = os.path.join(baseindexpath, index_name) # Search filename for path in r_serv_pasteName.smembers(q[0]): @@ -83,12 +95,9 @@ def search(): paste_size.append(paste._get_p_size()) # Search full line - from whoosh import index - from whoosh.fields import Schema, TEXT, ID schema = Schema(title=TEXT(stored=True), path=ID(stored=True), content=TEXT) ix = index.open_dir(selected_index) - from whoosh.qparser import QueryParser with ix.searcher() as searcher: query = QueryParser("content", ix.schema).parse(" ".join(q)) results = searcher.search_page(query, 1, pagelen=num_elem_to_get) @@ -121,26 +130,23 @@ def get_more_search_result(): q = [] q.append(query) page_offset = int(request.form['page_offset']) - index_num = request.form['index_num'] + index_name = request.form['index_name'] num_elem_to_get = 50 # select correct index - if index_num is None or index_num == "0": + if index_name is None or index_name == "0": selected_index = get_current_index() else: - selected_index = os.path.join(baseindexpath, index_num) + selected_index = os.path.join(baseindexpath, index_name) path_array = [] preview_array = [] date_array = [] size_array = [] - from whoosh import index - from whoosh.fields import Schema, TEXT, ID schema = Schema(title=TEXT(stored=True), path=ID(stored=True), content=TEXT) ix = index.open_dir(selected_index) - from whoosh.qparser import QueryParser with ix.searcher() as searcher: query = QueryParser("content", ix.schema).parse(" ".join(q)) results = searcher.search_page(query, page_offset, num_elem_to_get) diff --git a/var/www/templates/search.html b/var/www/templates/search.html index 7d78c83e..cbbbcf3c 100644 --- a/var/www/templates/search.html +++ b/var/www/templates/search.html @@ -92,7 +92,7 @@
Index: - {% for indexElem in index_list %} {% endfor %} @@ -166,14 +166,14 @@ $("#load_more_json_button1").show(); } - $('#index_num').on('change', function() { + $('#index_name').on('change', function() { var form = document.createElement('form'); form.setAttribute("method", 'post'); form.setAttribute("action", "{{ url_for('search') }}"); var input1 = document.createElement('input'); input1.setAttribute("type", "hidden"); - input1.setAttribute("name", "index_num"); + input1.setAttribute("name", "index_name"); input1.setAttribute("value", this.value); form.appendChild(input1); @@ -201,7 +201,7 @@ } function load_search_50_data() { - var options = { query: query, page_offset: page_offset, index_num: $("#index_num").val() }; + var options = { query: query, page_offset: page_offset, index_name: $("#index_name").val() }; $.post( "{{ url_for('get_more_search_result') }}", options).done(function( data ) { for(i=0; i