Algolia Duplicates

Sam*_*hly 1 javascript node.js algolia

我在让Algolia正常工作方面遇到了一些麻烦.我正在使用NodeJS,并试图在我的数据库和Algolia之间进行一些同步,但由于某种原因,似乎有大量的重复项随机弹出.

在此输入图像描述

如您所见,在某些情况下,除了主题名称之外,两个不同的条目会弹出完全不同的数据.我没有在其他任何地方运行add-to-algolia代码,并且UUID关闭,因为我输入的条目在他们面前有"topic-".

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();
				})
			}
		})
	})
}
Run Code Online (Sandbox Code Playgroud)

我有什么选择吗?任何帮助,将不胜感激.

编辑:复制品和原件之间似乎存在某种关系,但我仍然无法弄清楚是什么导致了它.

在此输入图像描述

(原谅吧)

bob*_*ito 6

有重复项,因为您没有使用objectID来唯一标识您的记录.通常,主键可以作为objectID正常工作.如果你没有指定一个,Algolia将自动分配一个,这意味着它很难没有重复.