T53161 Proof of Concept working

This commit is contained in:
Stephan preeker 2017-11-17 14:05:24 +01:00
parent 41eb5e256f
commit 76bb68dcc8
3 changed files with 45 additions and 48 deletions

View File

@ -51,22 +51,29 @@ class Node(es.DocType):
analyzer=autocomplete analyzer=autocomplete
) )
user_id = es.Keyword() user = es.Object({
user_name = es.String( 'fields': {
'id': es.Keyword(),
'name': es.String(
fielddata=True, fielddata=True,
analyzer=autocomplete analyzer=autocomplete)
) }
})
description = es.String() description = es.String()
is_free = es.Boolean() is_free = es.Boolean()
project_id = es.Keyword() project = es.Object({
project_name = es.String() 'fields': {
'id': es.Keyword(),
'name': es.Keyword(),
}
})
media = es.Keyword() media = es.Keyword()
picture_url = es.Keyword() picture = es.Keyword()
tags = es.Keyword(multi=True) tags = es.Keyword(multi=True)
license_notes = es.String() license_notes = es.String()
@ -92,15 +99,15 @@ def create_doc_from_node_data(node_to_index):
doc.node_type = node_to_index['node_type'] doc.node_type = node_to_index['node_type']
doc.name = node_to_index['name'] doc.name = node_to_index['name']
doc.user_id = str(node_to_index['user']['_id']) doc.user.id = str(node_to_index['user']['_id'])
doc.user_name = node_to_index['user']['full_name'] doc.user.name = node_to_index['user']['full_name']
doc.project_id = str(node_to_index['project']['_id']) doc.project.id = str(node_to_index['project']['_id'])
doc.project_name = node_to_index['project']['name'] doc.project.name = node_to_index['project']['name']
if node_to_index['node_type'] == 'asset': if node_to_index['node_type'] == 'asset':
doc.media = node_to_index['media'] doc.media = node_to_index['media']
doc.picture_url = node_to_index.get('picture') doc.picture = node_to_index.get('picture')
doc.tags = node_to_index.get('tags') doc.tags = node_to_index.get('tags')
doc.license_notes = node_to_index.get('license_notes') doc.license_notes = node_to_index.get('license_notes')

View File

@ -24,8 +24,10 @@ def do_search(query: str) -> dict:
""" """
should = [ should = [
Q('match', name=query), Q('match', name=query),
Q('match', user_name=query),
Q('match', project_name=query), {"match": {"project.name": query}},
{"match": {"user.name": query}},
Q('match', description=query), Q('match', description=query),
Q('term', media=query), Q('term', media=query),
Q('term', tags=query), Q('term', tags=query),

View File

@ -5,46 +5,38 @@
$(document).ready(function() { $(document).ready(function() {
if (typeof algoliaIndex === 'undefined') return; var getSearch = function(q, cb, async){
var elasticSearch = function() {
console.log('yay');
return function findMatches(q, cb, async){
let newhits = []; let newhits = [];
// do a query
console.log(q);
$.getJSON("/api/newsearch?q=" + q, function( data ) { $.getJSON("/api/newsearch?q=" + q, function( data ) {
console.log(data.hits.hits);
let hits = data.hits.hits; let hits = data.hits.hits;
newhits = hits.map(function(hit){ newhits = hits.map(function(hit){
console.log(hit._source);
return hit._source; return hit._source;
}); });
console.log(newhits); cb(newhits.slice(0));
cb(newhits); async(newhits.slice(0));
console.log(cb);
async();
}); });
//api/newsearch?q=test%20dji
// return the matches..
//cb(matches);
}; };
var elasticSearch = (function() {
return function findMatches(q, cb, async){
if (!cb) { return; }
getSearch(q, cb, async);
}; };
});
var searchInput = $('#cloud-search'); var searchInput = $('#cloud-search');
var tu = searchInput.typeahead({hint: true}, { var tu = searchInput.typeahead({hint: true}, {
//source: algoliaIndex.ttAdapter(), //source: algoliaIndex.ttAdapter(),
source: elasticSearch(), source: elasticSearch(),
//source: elkBlood(),
async: true,
displayKey: 'name', displayKey: 'name',
limit: 10, limit: 10,
minLength: 0, minLength: 0,
templates: { templates: {
suggestion: function(hit) { suggestion: function(hit) {
console.log('hit2');
console.log(hit);
var hitMedia = (hit.media ? ' · <span class="media">'+hit.media+'</span>' : ''); var hitMedia = (hit.media ? ' · <span class="media">'+hit.media+'</span>' : '');
var hitFree = (hit.is_free ? '<div class="search-hit-ribbon"><span>free</span></div>' : ''); var hitFree = (hit.is_free ? '<div class="search-hit-ribbon"><span>free</span></div>' : '');
var hitPicture; var hitPicture;
@ -55,7 +47,7 @@ $(document).ready(function() {
hitPicture = '<div class="search-hit-thumbnail-icon">'; hitPicture = '<div class="search-hit-thumbnail-icon">';
hitPicture += (hit.media ? '<i class="pi-' + hit.media + '"></i>' : '<i class="dark pi-'+ hit.node_type + '"></i>'); hitPicture += (hit.media ? '<i class="pi-' + hit.media + '"></i>' : '<i class="dark pi-'+ hit.node_type + '"></i>');
hitPicture += '</div>'; hitPicture += '</div>';
}; }
var $span = $('<span>').addClass('project').text(hit.project.name); var $span = $('<span>').addClass('project').text(hit.project.name);
var $searchHitName = $('<div>').addClass('search-hit-name') var $searchHitName = $('<div>').addClass('search-hit-name')
.attr('title', hit.name) .attr('title', hit.name)
@ -104,18 +96,14 @@ $(document).ready(function() {
}); });
searchInput.keyup(function(e) { searchInput.keyup(function(e) {
console.log('upupup');
if ( $('.tt-dataset').is(':empty') ){ if ( $('.tt-dataset').is(':empty') ){
if(e.keyCode == 13){ if(e.keyCode == 13){
window.location.href = '/search#q='+ $("#cloud-search").val() + '&page=1'; window.location.href = '/search#q='+ $("#cloud-search").val() + '&page=1';
}; }
}; }
}); });
searchInput.bind('typeahead:render', function(event, suggestions, async, dataset) { searchInput.bind('typeahead:render', function(event, suggestions, async, dataset) {
console.log('woot');
console.log(suggestions);
console.log(dataset);
if( suggestions != undefined && $('.tt-all-results').length <= 0){ if( suggestions != undefined && $('.tt-all-results').length <= 0){
$('.tt-dataset').append( $('.tt-dataset').append(
'<a id="search-advanced" href="/search#q='+ $("#cloud-search").val() + '&page=1" class="search-site-result advanced tt-suggestion">' + '<a id="search-advanced" href="/search#q='+ $("#cloud-search").val() + '&page=1" class="search-site-result advanced tt-suggestion">' +
@ -130,7 +118,7 @@ $(document).ready(function() {
'</div>' + '</div>' +
'</div>'+ '</div>'+
'</a>'); '</a>');
}; }
}); });
}); });