Searching with solr, need some help


#1

Hi, looking for some help with my app.
Is there a better way to implement this ? When I click on the back button on my app I keep getting any previously queried results appearing also … how can I clear the collection on each re-render?

Thanks in advance.

collection.js

Docs = new Meteor.Collection('docs');

flow-router.js:

 action: function(params, queryParams) {
   BlazeLayout.render('layout', { main: 'home' });
   Tracker.autorun(function() {
     Meteor.subscribe('allTables', Session.get('searchText'));
   });
 },
 name: 'home'
});```


```FlowRouter.route('/table/:tname/:tid', {
 action: function(params, queryParams) {
   BlazeLayout.render('layout', { main: 'table' });
   Tracker.autorun(function() {
     Meteor.subscribe('singleTable', params);
   });
 },
 name: 'table'
});```


## publish.js

```Meteor.publish('allTables', function(searchText) {
	var self = this;
	try {
		var result = HTTP.get('http://localhost:8983/solr/dmadm3/select', {
			params: {
				q: searchText,
				fq: "{!parent which=\"tnatural:*\"}",
				wt: "json"
			}
		}).content;
		var data = JSON.parse(result).response;
		_.each(data.docs, function(doc) {
			var table = {
				tname: doc.tname,
				tid: doc.id,
				tcomment: doc.tcomment,
				tnatural: doc.tnatural
			};
			self.added('docs', Random.id(), table);
		});
	} catch (error) {
		console.log(error);
	}
});```

```Meteor.publish('singleTable', function(params) {
	var self = this;
	try {
		var result = HTTP.get("http://localhost:8983/solr/dmadm3/select?q=%7B!parent+which%3D%22id%3A" + params.tid + "%22%7D&wt=json&indent=true&expand=true&expand.field=tname&expand.q=*:*", {
			params: {}
		}).content;
		var data = JSON.parse(result).expanded[params.tname];
		Docs.remove({});
		_.each(data.docs, function(doc) {
			var columns =  {
				cname: doc.cname,
				cid: doc.id,
				ccomment: doc.ccomment,
				cnatural: doc.cnatural
			};
			self.added('docs', Random.id(), columns);
		});
	} catch(e) {
		console.log('Error: ' + e);
	}
});```


## subscribers.js

```Template.searchBox.events({
 "submit form": function(events, template) {
   event.preventDefault();
   var searchText = template.$("input[type=text]").val();
   if (searchText) {
     Session.set("searchText", searchText);
   }
 }
});```

```Template.searchResults.helpers({
 "allDocs": function() {
   return Docs.find();
 }
});```

```Template.table.helpers({
 'getTable': function() {
   return Docs.find();
 }
});```


## templates

```<template name="searchResults">
	{{#if allDocs.count}}
	<hr>
	<div class="tab">
		<h5>Tables</h5>
		<table>
			{{#each allDocs}}
			<hr>
			<div class="results">
				<div class="docs">
					<div class="tname">
						<a href="{{pathFor 'table' tname=tname tid=tid }}">{{tname}}</a>
					</div>
					<div class="tid">{{tid}}</div>
					<div class="tcomment">{{tcomment}}</div>
				</div>
			</div>
			{{/each}}
		</table>
	</div>
	{{/if}}
</template>```

```<template name="searchBox">
	<div class="search">
	<form>
		<input type="text" name="query"/>
		<input type="submit" value="Search"/>
	</form>
	</div>
</template>```

#2

add flow router trigger to clear that Session variable before entering route ?


#3

I tried this but it doesn’t seem to work as (search now doesn’t return results), any ideas on what might be wrong here?

  action: function(params, queryParams) {
    BlazeLayout.render('layout', { main: 'home' });
    Meteor.subscribe('allTables', Session.get('searchText'));
  },
  triggersEnter: [function(context, redirect) {
    Session.set('searchText', "");
  }],
  name: 'home'
});```