-
-
Save fcheung/dd996f7b4a5529162199 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/////// MongoScoreScript.java | |
/////// Looks up score values in the map it is constructed with | |
package com.dressipi.elastic; | |
import org.elasticsearch.script.AbstractLongSearchScript; | |
import com.mongodb.BasicDBObject; | |
public class MongoScorerScript extends AbstractLongSearchScript { | |
BasicDBObject score_map; | |
public MongoScorerScript(BasicDBObject a_score_map){ | |
score_map = a_score_map; | |
} | |
@Override | |
public long runAsLong() { | |
String g_id = Integer.toString(doc().numeric("id").getIntValue()); | |
Integer value = (Integer)score_map.get(g_id); | |
if(value == null){ | |
return 0; | |
}else{ | |
return value.intValue(); | |
} | |
} | |
} | |
///////// MongoScorerFactory | |
///////// Factory class for the native script | |
package com.dressipi.elastic; | |
import org.elasticsearch.common.Nullable; | |
import org.elasticsearch.script.ExecutableScript; | |
import org.elasticsearch.script.NativeScriptFactory; | |
import java.util.Map; | |
import com.dressipi.elastic.MongoScorerScript; | |
import com.dressipi.elastic.ScoreMapCache; | |
import com.mongodb.BasicDBObject; | |
public class MongoScorerFactory implements NativeScriptFactory { | |
ScoreMapCache cache; | |
@Override public ExecutableScript newScript(@Nullable Map<String, Object> params) { | |
Integer profileId = (Integer)params.get("profile_id"); | |
BasicDBObject map = cache.get(profileId); | |
return new MongoScorerScript(map); | |
} | |
public MongoScorerFactory() throws java.net.UnknownHostException{ | |
cache = new ScoreMapCache(); | |
} | |
} | |
//// ScoreMapCache: looks up our maps of doc-id -> score from mongo | |
//// Implements a very simple LRU cache of those maps | |
//// | |
package com.dressipi.elastic; | |
import com.mongodb.Mongo; | |
import com.mongodb.DB; | |
import com.mongodb.DBCollection; | |
import com.mongodb.BasicDBObject; | |
import com.mongodb.DBObject; | |
import com.mongodb.DBCursor; | |
import java.util.LinkedHashMap; | |
import java.util.Map; | |
public class ScoreMapCache { | |
public static final int CACHE_SIZE = 100; | |
DBCollection recommendation_score_maps; | |
LinkedHashMap<Integer,BasicDBObject> cache; | |
BasicDBObject get(Integer profileId){ | |
BasicDBObject result; | |
synchronized(this){ | |
result = cache.get(profileId); | |
if(result == null){ | |
result = fetchMapForProfileId(profileId); | |
cache.put(profileId, result); | |
} | |
} | |
return result; | |
} | |
BasicDBObject fetchMapForProfileId(Integer profileId){ | |
BasicDBObject query = new BasicDBObject(); | |
query.put("profile_id", profileId); | |
DBCursor cur = recommendation_score_maps.find(query); | |
if(cur.hasNext()){ | |
BasicDBObject recommendation_score_map = (BasicDBObject)cur.next(); | |
return (BasicDBObject)recommendation_score_map.get("map"); | |
}else{ | |
return new BasicDBObject(); | |
} | |
} | |
public ScoreMapCache() throws java.net.UnknownHostException{ | |
cache = new LinkedHashMap<Integer,BasicDBObject>(20, 0.75f, true){ | |
@Override protected boolean removeEldestEntry (Map.Entry<Integer,BasicDBObject> eldest) { | |
return size() > ScoreMapCache.CACHE_SIZE; | |
} | |
}; | |
Mongo m = new Mongo("127.0.0.1"); | |
DB db = m.getDB("dressipi-development"); | |
recommendation_score_maps = db.getCollection("recommendation_score_maps"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment