Algolia Duplicates

时间:2017-07-13 04:54:43

标签: javascript node.js algolia

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

enter image description here

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

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();
				})
			}
		})
	})
}

我缺少某种选择吗?任何帮助将不胜感激。

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

enter image description here

(原谅吧)

2 个答案:

答案 0 :(得分:6)

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

答案 1 :(得分:0)

所以这很令人尴尬。

我忘记了一个也为索引做贡献的登台服务器。