Search code examples
javascriptnode.jsalgolia

Algolia Duplicates


I'm having a bit of trouble getting Algolia to work properly. I'm using NodeJS and am trying to have a bit of synchronization going between my database and Algolia, but for some reason a ton of duplicates seem to pop up randomly.

enter image description here

As you can see, in some cases two different entries are popping up with completely different data except for the topic name. I am not running the add-to-algolia code anywhere else, and the UUID is off as entries I put in have "topic-" in front of them.

function loadNewTweets(){
	console.log("Checking...");
	var tweets;
	var topics;
	var referenceTopics;
	Promise.all([
		//stuff
	])
	.then(function(data){
    
    topics = [
      //data
    ]
    
		return Promise.each(topics, function(topic, index){
			return new Promise(function(res,rej){
				Promise.all([
					//things
				])
				.then(function(r){
					var id = 'topic-'+uuid.v4();

					if(!topicDB){
						var obj = {
							//data
						}
						console.log("Adding", topic.topic, "to topic DB + Algolia");
						return new Promise(function(res,rej){
							var dbInstance;
							Database.models.Topic.create(obj)
							.then(function(topic){
								dbInstance = topic;
								return Search.addData('topics', [dbInstance])
							})
							.then(function(content){
								dbInstance.algoliaId = content.objectIDs[0];
								return dbInstance.save(['algoliaId']);
							})
							.then(function(){
								return res();
							})
						})
					}
				})
				.then(function(){
					return res();
				})
			})
			
		})
	})
	.then(function(){
		return Database.models.Topic.findAll({})
	})
	.then(function(topicsDB){
		//If a topic is in the database, but not the topics array.

		//Loop through each database entry.
		Promise.each(topicsDB, function(topic){
			var del = true;

			//Go through topics array
			for(var i=0;i<topics.length;i++){

				//If a topic in the array matches a database entry, dont remove it.
				if(topics[i].topic == topic.topic){
					del = false;
				}
			}

			//If no entry was found in the array for this topic in the database, remove it from the database and Algolia.
			if(del){
				console.log("Deleting", topic.topic, "from topic DB + Algolia", topic.algoliaId);
				Search.delete('topics', [topic.algoliaId])
				.then(function(){
					topic.destroy();
				})
			}
		})
	})
}

Is there some sort of option I'm missing? Any help would be appreciated.

EDIT: There seems to be some sort of relationship between the duplicate and the original, but I still cant figure out what's causing it.

enter image description here

(forgive the bar)


Solution

  • So this is embarrassing.

    I forgot about a staging server that was also contributing to the index.