diff options
author | Andreas Baumann <mail@andreasbaumann.cc> | 2017-03-03 19:57:20 +0100 |
---|---|---|
committer | Andreas Baumann <mail@andreasbaumann.cc> | 2017-03-03 19:57:20 +0100 |
commit | f0dbe295d7d4f8282c2bde86bc4387b367bef589 (patch) | |
tree | 4f09c448098b57656c789d7dd358271498c34bc5 /themes/new_theme/layouts | |
parent | 6050f5e92d0386944cddd0adc9163bfe0f88fea3 (diff) | |
download | www-andreasbaumann-cc-f0dbe295d7d4f8282c2bde86bc4387b367bef589.tar.gz www-andreasbaumann-cc-f0dbe295d7d4f8282c2bde86bc4387b367bef589.tar.bz2 |
added a first strus search to the site
Diffstat (limited to 'themes/new_theme/layouts')
-rw-r--r-- | themes/new_theme/layouts/_default/list.html | 7 | ||||
-rw-r--r-- | themes/new_theme/layouts/index/single.html | 0 | ||||
-rw-r--r-- | themes/new_theme/layouts/partials/ranklist.html | 218 | ||||
-rw-r--r-- | themes/new_theme/layouts/partials/sidebar.html | 2 | ||||
-rw-r--r-- | themes/new_theme/layouts/partials/widgets/google_site_search.html (renamed from themes/new_theme/layouts/partials/widgets/search.html) | 4 | ||||
-rw-r--r-- | themes/new_theme/layouts/partials/widgets/strus_search.html | 21 |
6 files changed, 247 insertions, 5 deletions
diff --git a/themes/new_theme/layouts/_default/list.html b/themes/new_theme/layouts/_default/list.html index 0f5e063..82f03fc 100644 --- a/themes/new_theme/layouts/_default/list.html +++ b/themes/new_theme/layouts/_default/list.html @@ -9,7 +9,11 @@ <h1 class="post__title">{{ .Title }}</h1> </header> <div class="post__content clearfix"> - {{ .Content }} + {{ if ( eq $section "search" ) }} + {{ partial "ranklist.html" . }} + {{ else }} + {{ .Content }} + {{ end }} </div> </article> </div> @@ -56,7 +60,6 @@ {{ partial "pagination.html" . }} </div> - {{ end }} {{ partial "sidebar.html" . }} diff --git a/themes/new_theme/layouts/index/single.html b/themes/new_theme/layouts/index/single.html new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/themes/new_theme/layouts/index/single.html diff --git a/themes/new_theme/layouts/partials/ranklist.html b/themes/new_theme/layouts/partials/ranklist.html new file mode 100644 index 0000000..29027bc --- /dev/null +++ b/themes/new_theme/layouts/partials/ranklist.html @@ -0,0 +1,218 @@ +<script src="/js/jquery.js"></script> +<script src="/js/url-search-params.js"></script> +<script language="Javascript"> + + function normalize_term( s ) { + return s.toLowerCase( ); + } + + function get_terms( s ) { + var terms = []; + var words = s.trim( ).split( ' ' ); + for( var i in words ) { + terms.push( + { + term: { + type: "word", + value: normalize_term( words[i] ) + } + } + ); + } + return terms; + } + + function get_features( query ) { + var features = []; + var words = query.trim( ).split( ' ' ); + for( var i in words ) { + features.push( + { + name: "feat", + value: { + term: { + type: "word", + value: normalize_term( words[i] ) + } + }, + weight: 1.0 + } + ); + } + features.push( + { + name: "sel", + value: { + expression : { + operator: "contains", + range: 0, + cardinality: 0, + terms: get_terms( query ) + } + }, + weight: 1.0 + } + ) + return features; + } + + function draw_ranklist( query, data ) { + var html = ''; + + if( data.ranklist.ranks.length == 0 ) { + return "<p>No hits for query '" + query + "'...</p>"; + } else { + html += "<p>" + data.ranklist.documents_ranked + " hits found</p>"; + } + + var start = 1; + html += "<ol class=\"ranklist\" start=\"" + start + "\">"; + for( var i = 0; i < data.ranklist.ranks.length; i++ ) { + var rank = data.ranklist.ranks[i]; + var abstract = ''; + var slug = ''; + var docid = ''; + html += "<li>"; + for( var j = 0; j < rank.attributes.length; j++ ) { + var attribute = rank.attributes[j]; + if( attribute.key == 'filename' ) { + slug = attribute.value; + } else if( attribute.key == 'docid' ) { + docid = attribute.value; + } else { + abstract += ' ' + attribute.value; + } + } + html += "<div class='title'>" + html += "<a href=\"" + docid + "\">" + slug + "</a></div>"; + if( abstract != '' ) { + html += "<div class='abstract'>" + abstract + "</div>"; + } + html += "</li>"; + } + html += "</ol>"; + return html; + } + + function executeQuery( query ) { + var request = { + query: { + first_rank: 0, + nof_ranks: 10, + features: get_features( query ), + select: [ + "sel" + ], + restrict: [ + ], + exclude: [ + ], + weighting: [ + { + name: "bm25", + params: [ + { + key : "match", + value : "feat" + } + ], + weight : 1.0 + } + ], + summarizer: [ + { + attribute: "docid", + name: "attribute", + params: [ + { + key : "name", + value : "docid" + } + ] + }, + { + attribute: "slug", + name: "attribute", + params: [ + { + key : "name", + value : "filename" + } + ] + }, + { + attribute: "abstract", + name: "test", + params: [ + { + key: "type", + value: "word" + }, + { + key: "match", + value: "feat" + }, + { + key: "N", + value: 50 + }, + { + key: "add_dots", + value: 1 + }, + { + key: "start_first_match", + value: 0 + }, + { + key: "mark", + value: "<b>%1%</b>" + } + ] + } + ] + } + }; + + var json = JSON.stringify( request, null, " " ); + console.log( "debug request: " + json ); + + $.ajax( { + type : 'POST', + url : 'http://eeepc.lan:8080/strus/query/wwwandreasbaumanncc', + data : json, + contentType : 'application/json' + } ) + .error( function( x, t, m ) { + var html = "<pre><font color='red'>" + x.responseText + "</font></pre>\n"; + $( '#ranklist' ).html( html ); + + } ) + .success( function( data ) { + var json = JSON.stringify( data, null, " " ); + console.log( "debug answer: " + json ); + + if( data.result == 'ok' ) { + var html = draw_ranklist( query, data ); + $( '#ranklist' ).html( html ); + } else { + var json = JSON.stringify( data, null, " " ); + var html = "<p>The strusWebService returned the following error:</p><pre><font color='red'>" + json + "</font></pre>"; + $( '#ranklist' ).html( html ); + } + } ); + }; + + $( window ).load( function( ) { + var paramsString = window.location.search; + var searchParams = new URLSearchParams( paramsString ); + if( searchParams.has( "q" ) ) { + executeQuery( searchParams.get( "q" ) ); + } else { + $( '#ranklist' ).html( "new search" ); + } + } ); + +</script> +<div id="ranklist"> +</div> diff --git a/themes/new_theme/layouts/partials/sidebar.html b/themes/new_theme/layouts/partials/sidebar.html index b621bb2..123c544 100644 --- a/themes/new_theme/layouts/partials/sidebar.html +++ b/themes/new_theme/layouts/partials/sidebar.html @@ -1,5 +1,5 @@ <aside class="sidebar" itemscope="itemscope" itemtype="http://schema.org/WPSideBar"> - {{ partial "widgets/search.html" . }} + {{ partial "widgets/strus_search.html" . }} {{ $section := .Section }} {{ if or ( eq $section "blog" ) ( eq $section "" ) }} {{ partial "widgets/recent.html" . }} diff --git a/themes/new_theme/layouts/partials/widgets/search.html b/themes/new_theme/layouts/partials/widgets/google_site_search.html index 9d3676f..352adc7 100644 --- a/themes/new_theme/layouts/partials/widgets/search.html +++ b/themes/new_theme/layouts/partials/widgets/google_site_search.html @@ -3,10 +3,10 @@ <form class="widget-search__form" role="search" method="get" action="//google.com/search"> <label> <span class="screen-reader-text">Search for:</span> - <input class="widget-search__field" type="search" placeholder="SEARCH..." value="" name="q"> + <input class="widget-search__field" type="search" placeholder="GOOGLE SEARCH..." value="" name="q"> </label> <input class="widget-search__submit" type="submit" value="Search"> <input type="hidden" name="sitesearch" value="{{ .Site.BaseURL }}" /> </form> </div> -{{- end }}
\ No newline at end of file +{{- end }} diff --git a/themes/new_theme/layouts/partials/widgets/strus_search.html b/themes/new_theme/layouts/partials/widgets/strus_search.html new file mode 100644 index 0000000..0a552b2 --- /dev/null +++ b/themes/new_theme/layouts/partials/widgets/strus_search.html @@ -0,0 +1,21 @@ +{{- if .Site.Params.widgets.search }} +<div class="widget-search widget"> + <form class="widget-search__form" role="search" method="get" action="/search/"> + <label> + <span class="screen-reader-text">Search for:</span> + <input class="widget-search__field" type="search" placeholder="SEARCH..." value="" id="query" name="q"> + </label> + <input class="widget-search__submit" type="submit" value="Search"> + </form> +</div> +<script language="Javascript"> + $( window ).load( function( ) { + var paramsString = window.location.search; + var searchParams = new URLSearchParams( paramsString ); + if( searchParams.has( "q" ) ) { + $( "#query" ).val( searchParams.get( "q" ) ); + $( "#query" ).removeAttr( "placeholder" ); + } + } ); +</script> +{{- end }} |