[Search] Poking around EasticSearch

Sucessfully queries elasticsearch, but displaying the
results poses problems because of asych things.
This commit is contained in:
shale 2015-07-13 16:49:06 -07:00
parent 5980575918
commit 3cb0b41b22
3 changed files with 152 additions and 32 deletions

View File

@ -15,22 +15,21 @@
{
"key": "SearchController",
"implementation": "SearchController.js",
"depends": [ "$scope", "queryService" ]
"depends": [ "$scope", "$http", "objectService", "queryService", "ELASTIC_ROOT" ]
}
],
"representations": [
{
"key": "search-item",
"templateUrl": "templates/search-item.html",
"uses": [ "type", "action", "composition" ],
"gestures": [ "info", "menu" ]
"uses": [ "grid-item" ]
}
],
"services": [
{
"key": "queryService",
"implementation": "QueryService.js",
"depends": [ "objectService" ]
"depends": [ "objectService", "ELASTIC_ROOT" ]
}
]
}

View File

@ -33,7 +33,7 @@ define(
* of objects in the filetree which is searchable.
* @constructor
*/
function QueryService(objectService) {
function QueryService(objectService, ROOT) {
// Recursive helper function to go through the tree
function listHelper(current) {
var composition;
@ -43,51 +43,116 @@ define(
// Base case.
return current;
}
// Recursive case. Is asynchronous.
return composition.invoke().then(function (children) {
var subList = [current],
i;
//console.log('children ', children);
for (i = 0; i < children.length; i++) {
//console.log('children[', i, ']', children[i]);
subList.push(listHelper(children[i]));
//console.log('subList', subList, 'index', i);
}
//console.log('sublist ', subList);
return subList;
});
}
// Recursive helper function to go through the tree
function listHelper2(current) {
return current.getCapability('composition').invoke().then(function (children) {
return [current].concat(children.forEach(function (child) {
if (child.hasCapability('composition')) {
return listHelper2(child);//.then(function (c) {
// return c;
//});
} else {
return child;
}
}));
});
}
// Converts the filetree into a list
// Eventually, plan to call search service (but do here for now)
function listify() {
function getItems() {
console.log('in getItems()');
debugger;
/*
var elasticsearch = require(['platform/persistence/elastic'], function (elasticsearch) {
debugger;
console.log('elasticsearch (inside) ', elasticsearch);
var client = new elasticsearch.Client({
host: ROOT
});
console.log('client', client);
var testsearch = client.search({q: 'name=test123'});//[params, [callback]]);
console.log('search', testsearch);
});
*/
var elasticsearch = require('elasticsearch');
debugger;
console.log('elasticsearch (outside) ', elasticsearch);
var client = new elasticsearch.Client({
host: ROOT
});
console.log('client', client);
var testsearch = client.search({q: 'name=test123'});//[params, [callback]]);
console.log('search', testsearch);
/*
console.log('elasticsearch', elasticsearch);
var client = new elasticsearch.Client({
host: 'localhost:9200'
});
console.log('client', client);
var test = client.search();//[params, [callback]]);
console.log('search', test);
*/
/*
var elasticApp = angular.module('elasticApp', ['elasticsearch']);
console.log('elasticApp', elasticApp);
var client = elasticApp.service('client', function (esFactory) {
return esFactory({
host: 'localhost:8080'
});
});
console.log('client', client);
*/
/*
var controller = elasticApp.controller('ElasticController', function ($scope, client, esFactory) {
client.cluster.state({
metric: [
'cluster_name',
'nodes',
'master_node',
'version'
]
}).then(function (resp) {
$scope.clusterState = resp;
$scope.error = null;
}).catch(function (err) {
$scope.clusterState = null;
$scope.error = err;
// if the err is a NoConnections error, then the client was not able to
// connect to elasticsearch. In that case, create a more detailed error
// message
if (err instanceof esFactory.errors.NoConnections) {
$scope.error = new Error('Unable to connect to elasticsearch. ' +
'Make sure that it is running and listening ' +
'at http://localhost:9200');
}
});
});
console.log('controller', controller);
*/
/*
var testSearch = client.search();//[params, [callback]]);
console.log('search', testSearch);
*/
// Aquire My Items (root folder)
return objectService.getObjects(['mine']).then(function (objects) {
return listHelper(objects.mine).then(function (c) {
//console.log('final result ', c);
return c;
});
});
}
return {
listify: listify
getItems: getItems
};
}

View File

@ -27,7 +27,11 @@
define(function () {
"use strict";
function SearchController($scope, queryService) {
// JSLint doesn't like underscore-prefixed properties,
// so hide them here.
var ID = "_id";
function SearchController($scope, $http, objectService, queryService, ROOT) {
// Search through items for items which contain the search term in the name
function search() {
@ -45,7 +49,7 @@ define(function () {
term = term.toLocaleLowerCase();
// Get items list
return queryService.listify().then(function (items) {
return queryService.getItems().then(function (items) {
// (slight time optimization)
itemsLength = items.length;
@ -69,8 +73,52 @@ define(function () {
});
}
function search2() {
var term = document.getElementById("searchinput").value;
$http({
method: "GET",
url: ROOT + "/_search",
data: {
query: {
term: {
name: term
}
}
}
}).then(function (raw) {
var id;
var output = raw.data.hits.hits;
var outputLength = output.length;
console.log('raw', raw);
console.log('output, pre', output);
var i;
for (i = 0; i < output.length; i++) {
output[i] = output[i][ID];
console.log('output [', i, ']', output[i]);
objectService.getObjects([ output[i] ]).then(function (obj) {
output[i] = obj;
// Manually get the member name to get to the actual object
for (var prop in output[i]) {
console.log('prop [', i, ']', output[i][prop]);
output[i] = output[i][prop];
debugger;
}
});
console.log('output [', i, ']', output[i]);
}
console.log('output, post', output);
$scope.results = output;
//return output;
});
}
return {
search: search,
search: search2,
// Check to see if there are any search results to display.
areResults: function () {
@ -79,6 +127,14 @@ define(function () {
} else {
return false;
}
},
getObjectByID: function (id) {
console.log('getObjectByID called');
objectService.getObjects([id]).then(function (out) {
console.log('object gotten by id', out[id]);
return out[id];
});
}
};
}