org.codelibs.elasticsearch.taste.writer.ItemWriter 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.RecommendedItem;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.cache.Cache;
public class ItemWriter extends ObjectWriter {
protected String targetIdField;
protected String itemIdField = TasteConstants.ITEM_ID_FIELD;
protected String valueField = TasteConstants.VALUE_FIELD;
protected String itemsField = TasteConstants.ITEMS_FILED;
protected boolean verbose = false;
protected String targetIndex;
protected String targetType;
protected String itemIndex;
protected String itemType;
protected Cache> cache;
public ItemWriter(final Client client, final String index,
final String type, final String targetIdField) {
super(client, index, type);
this.targetIdField = targetIdField;
}
public void write(final long id,
final List recommendedItems) {
final Map rootObj = new HashMap<>();
rootObj.put(targetIdField, id);
if (verbose) {
final GetResponse response = client
.prepareGet(targetIndex, targetType, Long.toString(id))
.execute().actionGet();
if (response.isExists()) {
final Map map = response.getSourceAsMap();
map.remove(targetIdField);
rootObj.putAll(map);
}
}
final List