T53161 Proof of Concept working

This commit is contained in:
Stephan preeker 2017-11-17 14:05:24 +01:00
parent 5a9a2c4268
commit 235a88d613
3 changed files with 45 additions and 48 deletions

View File

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

View File

@ -24,8 +24,10 @@ def do_search(query: str) -> dict:
"""
should = [
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('term', media=query),
Q('term', tags=query),

View File

@ -5,46 +5,38 @@
$(document).ready(function() {
if (typeof algoliaIndex === 'undefined') return;
var getSearch = function(q, cb, async){
let newhits = [];
$.getJSON("/api/newsearch?q=" + q, function( data ) {
let hits = data.hits.hits;
newhits = hits.map(function(hit){
return hit._source;
});
cb(newhits.slice(0));
async(newhits.slice(0));
});
};
var elasticSearch = function() {
console.log('yay');
var elasticSearch = (function() {
return function findMatches(q, cb, async){
let newhits = [];
// do a query
console.log(q);
$.getJSON("/api/newsearch?q=" + q, function( data ) {
console.log(data.hits.hits);
let hits = data.hits.hits;
newhits = hits.map(function(hit){
console.log(hit._source);
return hit._source;
});
console.log(newhits);
cb(newhits);
console.log(cb);
async();
});
//api/newsearch?q=test%20dji
// return the matches..
//cb(matches);
if (!cb) { return; }
getSearch(q, cb, async);
};
});
};
var searchInput = $('#cloud-search');
var tu = searchInput.typeahead({hint: true}, {
//source: algoliaIndex.ttAdapter(),
source: elasticSearch(),
//source: elkBlood(),
async: true,
displayKey: 'name',
limit: 10,
minLength: 0,
templates: {
suggestion: function(hit) {
console.log('hit2');
console.log(hit);
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 hitPicture;
@ -55,7 +47,7 @@ $(document).ready(function() {
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 += '</div>';
};
}
var $span = $('<span>').addClass('project').text(hit.project.name);
var $searchHitName = $('<div>').addClass('search-hit-name')
.attr('title', hit.name)
@ -104,18 +96,14 @@ $(document).ready(function() {
});
searchInput.keyup(function(e) {
console.log('upupup');
if ( $('.tt-dataset').is(':empty') ){
if(e.keyCode == 13){
window.location.href = '/search#q='+ $("#cloud-search").val() + '&page=1';
};
};
}
}
});
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){
$('.tt-dataset').append(
'<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>'+
'</a>');
};
}
});
});