org.codelibs.elasticsearch.taste.writer.UserWriter Maven / Gradle / Ivy
package org.codelibs.elasticsearch.taste.writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.codelibs.elasticsearch.taste.TasteConstants;
import org.codelibs.elasticsearch.taste.recommender.SimilarUser;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.cache.Cache;
public class UserWriter extends ObjectWriter {
protected String targetIdField;
protected String userIdField = TasteConstants.USER_ID_FIELD;
protected String valueField = TasteConstants.VALUE_FIELD;
protected String usersField = TasteConstants.USERS_FILED;
protected boolean verbose = false;
protected String userIndex;
protected String userType;
protected Cache> cache;
public UserWriter(final Client client, final String index,
final String type, final String targetIdField) {
super(client, index, type);
this.targetIdField = targetIdField;
}
public void write(final long userID,
final List mostSimilarUsers) {
final Map rootObj = new HashMap<>();
rootObj.put(targetIdField, userID);
if (verbose) {
final GetResponse response = client
.prepareGet(userIndex, userType, Long.toString(userID))
.execute().actionGet();
if (response.isExists()) {
final Map map = response.getSourceAsMap();
map.remove(targetIdField);
rootObj.putAll(map);
}
}
final List