Browse Source

add related query/content to fess-ss

yfujita 7 năm trước cách đây
mục cha
commit
015acd8db3

+ 73 - 18
src/main/assemblies/extension/fess-ss/src/controller.js

@@ -8,35 +8,25 @@ export default class {
     this.fessContextPath = this.fessUrl.slice(0, this.fessUrl.indexOf('/json'));
     this.searchPagePath = FessJQuery('script#fess-ss').attr('fess-search-page-path');
     this.enableLabels = FessJQuery('script#fess-ss').attr('enable-labels') === 'true' ? true : false;
+    this.enableRelated = FessJQuery('script#fess-ss').attr('enable-related') === 'true' ? true : false;
     this.labelsCache = null
-    this.urlParams = function() {
-      var params = {};
-      var array = location.search.replace(/\?/g, '').split('&');
-      array.forEach(function(param){
-        var tpl = param.split('=');
-        if (tpl.length == 1) {
-          params[decodeURIComponent(tpl[0])] = '';
-        } else if (tpl.length == 2) {
-          params[decodeURIComponent(tpl[0])] = decodeURIComponent(tpl[1].replace(/\+/g, ' '));
-        }
-      })
-      return params;
-    }();
+    this.urlParams = this._getParameters();
   }
 
   start() {
     this.FessView.init();
     this.FessView.renderForm(this.searchPagePath, this.urlParams);
     this._bindForm();
-    if (this.urlParams.q !== undefined) {
+    if (this.urlParams.q !== undefined && this.urlParams.q.length > 0) {
+      var query = this.urlParams.q[0];
       if (FessJQuery('.fessWrapper .fessForm form input').length > 0) {
-        FessJQuery('.fessWrapper .fessForm form input').val(this.urlParams.q);
+        FessJQuery('.fessWrapper .fessForm form input').val(query);
       }
       if (FessJQuery('.fessWrapper .fessFormOnly form input').length > 0) {
-        FessJQuery('.fessWrapper .fessFormOnly form input').val(this.urlParams.q);
+        FessJQuery('.fessWrapper .fessFormOnly form input').val(query);
       }
       try {
-        this._search({q: this.urlParams.q});
+        this._search({q: query});
       } catch (e) {
         console.log(e);
       }
@@ -76,7 +66,7 @@ export default class {
     });
   }
 
-  _search(params) {
+    _search(params) {
     var $fessResult = FessJQuery('.fessWrapper .fessResult');
     $fessResult.css('display', 'none');
 
@@ -129,6 +119,18 @@ export default class {
       response.labels = this.labelsCache;
     }
 
+    if (!this.enableRelated) {
+      delete response.related_query;
+      delete response.related_content;
+    }
+
+    if (response.related_query !== undefined) {
+      for (var i=0;i<response.related_query.length;i++) {
+        var relatedQuery = response.related_query[i];
+        response.related_query[i] = {"query": relatedQuery, "link": this._getRelatedQueryLink(relatedQuery)};
+      }
+    }
+
     var $fessResult = FessJQuery('.fessWrapper .fessResult');
     if (response.record_count > 0) {
       this.FessView.renderResult(this.fessContextPath, response, params);
@@ -139,4 +141,57 @@ export default class {
       $fessResult.css('display', 'block');
     }
   }
+
+  _getParameters() {
+    var path = location.href.replace(/\?.*$/, '');
+    var hash = '';
+    var url = location.href;
+    if (url.indexOf('#') != -1) {
+      var array = url.split('#');
+      url = array[0];
+      hash = array[1];
+    }
+
+    var params = function(url) {
+      var params = {};
+      if (url.indexOf('?') != -1) {
+        var array = url.split('?');
+        var paramArray = array[1].split('&');
+        paramArray.forEach(function(val, index, ar) {
+          var tpl = val.split('=');
+          var key = decodeURIComponent(tpl[0]);
+          var value = '';
+          if (tpl.length > 1) {
+            value = decodeURIComponent(tpl[1]);
+          }
+
+          if (params[key] === undefined) {
+            params[key] = [value];
+          } else {
+            params[key].push(value);
+          }
+        });
+      }
+      return params;
+    }(url);
+
+    params['fess_url_hash'] = hash;
+    return params;
+  }
+
+  _getRelatedQueryLink(query) {
+    var url = location.href.replace(/\?.*$/, '') + '?q=' + encodeURIComponent(query);
+    var hash = this.urlParams['fess_url_hash'];
+    Object.keys(this.urlParams).forEach(function(key) {
+      if (key !== 'q' && key !== 'fess_url_hash') {
+        this[key].forEach(function(val) {
+          url = url + '&' + encodeURIComponent(key) + '=' + encodeURIComponent(val);
+        });
+      }
+    }, this.urlParams);
+    if (hash !== undefined && hash !== '') {
+      url = url + '#' + hash;
+    }
+    return url;
+  }
 }

+ 4 - 2
src/main/assemblies/extension/fess-ss/src/fess-messages.js

@@ -13,7 +13,8 @@ export default class {
         'result.order.last_modified': 'Last modified',
         'result.pagination.prev': 'prev',
         'result.pagination.next': 'next',
-        'result.did_not_match': 'Your search - <b>{{q}}</b> - did not match any documents.'
+        'result.did_not_match': 'Your search - <b>{{q}}</b> - did not match any documents.',
+        'result.related_query_label': 'Related Words:'
       },
       ja: {
         'form.search.button': '検索',
@@ -27,7 +28,8 @@ export default class {
         'result.order.last_modified': '更新日時',
         'result.pagination.prev': '前へ',
         'result.pagination.next': '次へ',
-        'result.did_not_match': '<b>{{q}}</b>に一致する情報は見つかりませんでした。'
+        'result.did_not_match': '<b>{{q}}</b>に一致する情報は見つかりませんでした。',
+        'result.related_query_label': '関連ワード:'
       }
     }
   }

+ 17 - 0
src/main/assemblies/extension/fess-ss/src/templates/fess-no-result.hbs

@@ -28,6 +28,23 @@
               </select>
             </div>
           </td>
+          {{#if related_query}}
+          <tr>
+            <td>
+              {result.related_query_label}
+              {{#each related_query}}
+              &nbsp;<a href="{{link}}">{{query}}</a>
+              {{/each}}
+            </td>
+          </tr>
+          {{/if}}
+          {{#if related_content}}
+          <tr>
+            <td>
+              {{{related_content}}}
+            </td>
+          </tr>
+          {{/if}}
         </tr>
       </tbody>
     </table>

+ 17 - 0
src/main/assemblies/extension/fess-ss/src/templates/fess-result.hbs

@@ -29,6 +29,23 @@
             </div>
           </td>
         </tr>
+        {{#if related_query}}
+        <tr>
+          <td>
+            {result.related_query_label}
+            {{#each related_query}}
+            &nbsp;<a href="{{link}}">{{query}}</a>
+            {{/each}}
+          </td>
+        </tr>
+        {{/if}}
+        {{#if related_content}}
+        <tr>
+          <td>
+            {{{related_content}}}
+          </td>
+        </tr>
+        {{/if}}
       </tbody>
     </table>
   </div>

+ 5 - 11
src/main/assemblies/extension/fess-ss/src/view.js

@@ -72,17 +72,7 @@ export default class {
     $fessResult.html(this.FessMessages.render(html, response));
     var $pagination = this._createPagination(response.record_count, response.page_size, response.page_number, params);
     FessJQuery('.fessWrapper .paginationNav').append($pagination);
-    if (FessJQuery('.fessWrapper .fessForm').length > 0) {
-      if (params.sort !== undefined) {
-        FessJQuery('.fessWrapper select.sort').val(params.sort);
-      }
-      if (params['fields.label'] !== undefined){
-        FessJQuery('.fessWrapper select.field-labels').val(params['fields.label']);
-      }
-    } else {
-      FessJQuery('.fessWrapper .fessResult table .order').css('display', 'none');
-      FessJQuery('.fessWrapper .fessResult table .labels').css('display', 'none');
-    }
+    this._setSearchOptions(response, params);
     this._loadThumbnail(contextPath);
   }
 
@@ -90,6 +80,10 @@ export default class {
     var $fessResult = FessJQuery('.fessWrapper .fessResult');
     var html = noResultTemplate(response);
     $fessResult.html(this.FessMessages.render(html, response));
+    this._setSearchOptions(response, params);
+  }
+
+  _setSearchOptions(response, params) {
     if (FessJQuery('.fessWrapper .fessForm').length > 0) {
       if (params.sort !== undefined) {
         FessJQuery('.fessWrapper select.sort').val(params.sort);