Skip to content

Commit

Permalink
Merge pull request #612 from matteodem/v2.2.0
Browse files Browse the repository at this point in the history
v2.2.0
  • Loading branch information
matteodem authored Jun 13, 2017
2 parents bea554e + fa9c4b1 commit 65d883a
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 32 deletions.
6 changes: 3 additions & 3 deletions packages/easy:search/package.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package.describe({
name: 'easy:search',
summary: "Easy-to-use search with Blaze Components (+ Elastic Search Support)",
version: "2.1.10",
version: "2.2.0",
git: "https://github.com/matteodem/meteor-easy-search.git",
documentation: "../../README.md"
});
Expand All @@ -11,8 +11,8 @@ Package.onUse(function(api) {

api.use([
'ecmascript',
'easysearch:core@2.1.10',
'easysearch:components@2.1.10',
'easysearch:core@2.2.0',
'easysearch:components@2.2.0',
]);

api.export('EasySearch');
Expand Down
6 changes: 3 additions & 3 deletions packages/easysearch:autosuggest/package.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package.describe({
name: 'easysearch:autosuggest',
summary: "Selectize Autosuggest Component for EasySearch",
version: "2.1.9",
version: "2.2.0",
git: "https://github.com/matteodem/meteor-easy-search.git",
documentation: 'README.md'
});
Expand All @@ -12,8 +12,8 @@ Package.onUse(function(api) {
// Dependencies
api.use(['check', 'ecmascript', '[email protected]', '[email protected]']);
api.use([
'easysearch:core@2.1.9',
'easysearch:components@2.1.9',
'easysearch:core@2.2.0',
'easysearch:components@2.2.0',
'jeremy:[email protected]_4',
]);

Expand Down
6 changes: 5 additions & 1 deletion packages/easysearch:components/lib/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ BaseComponent = class BaseComponent extends BlazeComponent {
* Setup component on created.
*/
onCreated() {
this.autorun(() => this.initializeBase());
}

initializeBase() {
let index = this.getData().index,
indexes = [index];

Expand All @@ -37,7 +41,7 @@ BaseComponent = class BaseComponent extends BlazeComponent {
throw new Meteor.Error('no-index', 'Please provide an index for your component');
}

if (indexes.filter((index) => index instanceof EasySearch.Index).length != indexes.length) {
if (indexes.filter((index) => index instanceof EasySearch.Index).length !== indexes.length) {
throw new Meteor.Error(
'invalid-configuration',
`Did not receive an index or an array of indexes: "${indexes.toString()}"`
Expand Down
12 changes: 10 additions & 2 deletions packages/easysearch:components/lib/component-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ EasySearch._getComponentMethods = function (dict, index) {
*/
getCursor: () => {
const searchDefinition = dict.get('searchDefinition') || '',
options = dict.get('searchOptions'),
options = dict.get('searchOptions') || {},
showDocuments = dict.get('showDocuments');

check(options, Match.Optional(Object));
Expand Down Expand Up @@ -148,6 +148,14 @@ EasySearch._getComponentMethods = function (dict, index) {

dict.set('searchOptions', options);
this.paginate(1);
}
},
/**
* Reset the search.
*/
reset() {
this.search('');
this.paginate(1);
dict.set('searchOptions', {});
},
};
};
4 changes: 3 additions & 1 deletion packages/easysearch:components/lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ EasySearch.Index = class Index extends EasySearch.Index {
* @param {String} componentName Optional name of the component
*/
registerComponent(componentName = EasySearch.Index.COMPONENT_DEFAULT_NAME) {
this.components[componentName] = new ReactiveDict(`easySearchComponent_${this.config.name}_${componentName}_${Random.id()}`);
this.components[componentName] = new ReactiveDict(
`easySearchComponent_${this.config.name}_${componentName}_${Random.id()}`
);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/easysearch:components/package.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package.describe({
name: 'easysearch:components',
summary: "Blaze Components for EasySearch",
version: "2.1.10",
version: "2.2.0",
git: "https://github.com/matteodem/meteor-easy-search.git",
documentation: 'README.md'
});
Expand Down
1 change: 1 addition & 0 deletions packages/easysearch:components/tests/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ TestHelpers = {
var c = new component();

c.data = function () { return data; };
c.autorun = (f) => f();

return c;
}
Expand Down
23 changes: 19 additions & 4 deletions packages/easysearch:core/lib/core/search-collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class SearchCollection {
}

/**
* Get the mongo cursor.
* Get the mongo cursor on the client.
*
* @param {Object} searchDefinition Search definition
* @param {Object} options Search options
Expand All @@ -106,6 +106,8 @@ class SearchCollection {
* @private
*/
_getMongoCursor(searchDefinition, options) {
const clientSort = this.engine.callConfigMethod('clientSort', searchDefinition, options);

return this._collection.find(
{ __searchDefinition: JSON.stringify(searchDefinition), __searchOptions: JSON.stringify(options.props) },
{
Expand All @@ -118,7 +120,7 @@ class SearchCollection {

return doc;
},
sort: ['__sortPosition']
sort: (clientSort ? clientSort : ['__sortPosition'])
}
);
}
Expand Down Expand Up @@ -218,7 +220,10 @@ class SearchCollection {

this.added(collectionName, collectionScope.generateId(doc), doc);

// reorder all observed docs to keep valid sorting
/*
* Reorder all observed docs to keep valid sorting. Here we adjust the
* sortPosition number field to give space for the newly added doc
*/
if (observedDocs.map(d => d.__sortPosition).includes(atIndex)) {
observedDocs = observedDocs.map((doc, docIndex) => {
if (doc.__sortPosition >= atIndex) {
Expand Down Expand Up @@ -280,7 +285,17 @@ class SearchCollection {
});
this.removed(collectionName, collectionScope.generateId(doc));

observedDocs = observedDocs.filter(
/*
* Adjust sort position for all docs after the removed doc and
* remove the doc from the observed docs array
*/
observedDocs = observedDocs.map(doc => {
if (doc.__sortPosition > atIndex) {
doc.__sortPosition -= 1;
}

return doc;
}).filter(
d => collectionScope.generateId(d) !== collectionScope.generateId(doc)
);
}
Expand Down
10 changes: 7 additions & 3 deletions packages/easysearch:core/lib/engines/mongo-db.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,12 @@ class MongoDBEngine extends ReactiveEngine {
*/
getFindOptions(searchDefinition, options) {
return {
sort: this.callConfigMethod('sort', searchDefinition, options),
limit: options.search.limit,
skip: options.search.skip,
limit: options.search.limit,
disableOplog: this.config.disableOplog,
pollingIntervalMs: this.config.pollingIntervalMs,
pollingThrottleMs: this.config.pollingThrottleMs,
sort: this.callConfigMethod('sort', searchDefinition, options),
fields: this.callConfigMethod('fields', searchDefinition, options)
};
}
Expand All @@ -84,7 +87,8 @@ class MongoDBEngine extends ReactiveEngine {
'selector',
searchDefinition,
options,
this.config.aggregation),
this.config.aggregation
),
findOptions = this.getFindOptions(searchDefinition, options),
collection = options.index.collection;

Expand Down
2 changes: 1 addition & 1 deletion packages/easysearch:core/package.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package.describe({
name: 'easysearch:core',
summary: "Javascript Core for EasySearch",
version: "2.1.10",
version: "2.2.0",
git: "https://github.com/matteodem/meteor-easy-search.git",
documentation: 'README.md'
});
Expand Down
33 changes: 22 additions & 11 deletions packages/easysearch:elasticsearch/lib/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ if (Meteor.isServer) {
*/
static defaultElasticsearchConfiguration() {
return {
indexName: 'easysearch',
/**
* Return the fields to index in ElasticSearch.
*
Expand Down Expand Up @@ -117,33 +118,36 @@ if (Meteor.isServer) {
* Default ES client configuration.
*/
client: {
host: 'localhost:9200'
}
host: 'localhost:9200',
},
};
}

/**
* Put mapping according to mapping field provided when creating an EasySearch index
*
* @param {Object} indexConfig Index configuration
* @param {Object} indexConfig Index configuration
* @param {Function} cb callback on finished mapping
*/
putMapping(indexConfig = {}, cb) {
const {
mapping: body,
elasticSearchClient,
name: type
} = indexConfig;

if (!body) {
return cb();
}

const { indexName } = this.config
const type = this.getIndexType(indexConfig)

elasticSearchClient.indices.create({
updateAllTypes: false,
index: 'easysearch'
index: indexName,
}, Meteor.bindEnvironment(() => {
elasticSearchClient.indices.getMapping({
index: 'easysearch',
index: indexName,
type
}, Meteor.bindEnvironment((err, res) => {
const isEmpty = Object.keys(res).length === 0 && res.constructor === Object;
Expand All @@ -153,14 +157,21 @@ if (Meteor.isServer) {

elasticSearchClient.indices.putMapping({
updateAllTypes: false,
index: 'easysearch',
index: indexName,
type,
body
}, cb);
}));
}));
}

/**
* @returns {String}
*/
getIndexType(indexConfig) {
return this.config.indexType || indexConfig.name;
}

/**
* Act on index creation.
*
Expand All @@ -173,8 +184,8 @@ if (Meteor.isServer) {
indexConfig.elasticSearchClient = new elasticsearch.Client(this.config.client);
this.putMapping(indexConfig, Meteor.bindEnvironment(() => {
indexConfig.elasticSearchSyncer = new ElasticSearchDataSyncer({
indexName: 'easysearch',
indexType: indexConfig.name,
indexName: this.config.indexName,
indexType: this.getIndexType(indexConfig),
collection: indexConfig.collection,
client: indexConfig.elasticSearchClient,
beforeIndex: (doc) => this.callConfigMethod('getElasticSearchDoc', doc, this.callConfigMethod('fieldsToIndex', indexConfig))
Expand Down Expand Up @@ -202,8 +213,8 @@ if (Meteor.isServer) {
body = this.callConfigMethod('body', body, options);

options.index.elasticSearchClient.search({
index: 'easysearch',
type: options.index.name,
index: this.config.indexName,
type: this.getIndexType(options.index),
body: body,
size: options.search.limit,
from: options.search.skip
Expand Down
4 changes: 2 additions & 2 deletions packages/easysearch:elasticsearch/package.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package.describe({
name: 'easysearch:elasticsearch',
summary: "Elasticsearch Engine for EasySearch",
version: "2.1.9",
version: "2.2.0",
git: "https://github.com/matteodem/meteor-easy-search.git",
documentation: 'README.md'
});
Expand All @@ -15,7 +15,7 @@ Package.onUse(function(api) {

// Dependencies
api.use(['check', 'ecmascript']);
api.use(['easysearch:core@2.1.9', 'erasaur:[email protected]']);
api.use(['easysearch:core@2.2.0', 'erasaur:[email protected]']);

api.addFiles([
'lib/data-syncer.js',
Expand Down

0 comments on commit 65d883a

Please sign in to comment.