Skip to content

Instantly share code, notes, and snippets.

@lazaropj
Forked from strokine/EsId.java
Created August 20, 2016 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lazaropj/6e5b43033ee5a010100c37b8dced4fdf to your computer and use it in GitHub Desktop.
Save lazaropj/6e5b43033ee5a010100c37b8dced4fdf to your computer and use it in GitHub Desktop.
Utility to populate data from ElasticSearch results to POJO and from POJO to ElasticSearch Source for indexing
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface EsId {
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface EsType {
String value();
}
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.search.SearchHit;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class EsUtil {
private static final ObjectMapper MAPPER = new ObjectMapper();
public static XContentBuilder getSource(Object o) throws IOException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException{
XContentBuilder result=XContentFactory.jsonBuilder().startObject();
for(Field field:getAllFields(o.getClass())){
if(field.isAnnotationPresent(JsonProperty.class)){
Object value=PropertyUtils.getProperty(o, field.getName());
if(value!=null){
String name=field.getAnnotation(JsonProperty.class).value();
if(name==null || "".equals(name.trim())){
name=field.getName();
}
if(value instanceof Set){
Set<?> val=(Set<?>)value;
if(val.size()>0){
result.startArray(name);
for(Object f:val){
result.value(f.toString());
}
result.endArray();
}
}else{
result.field(name, value);
}
}
}
}
result.endObject();
return result;
}
public static <T> List<T> getObjects(SearchResponse response, Class<T> valueType) throws JsonParseException, JsonMappingException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, IOException{
List<T> res=new ArrayList<T>();
for(SearchHit hit:response.getHits()){
res.add(getObject(hit, valueType));
}
return res;
}
public static <T> T getObject(SearchResponse response, Class<T> valueType) throws JsonParseException, JsonMappingException, IllegalArgumentException, IllegalAccessException, IOException, InvocationTargetException {
for(SearchHit hit:response.getHits()){
return getObject(hit, valueType);
}
return null;
}
public static <T> T getObject(SearchHit hit, Class<T> valueType) throws JsonParseException, JsonMappingException, IOException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
T res=MAPPER.readValue(hit.getSourceAsString(), valueType);
setId(res, valueType, hit.getId());
return res;
}
public static <T> T getObject(GetResponse response, Class<T> valueType) throws JsonParseException, JsonMappingException, IllegalArgumentException, IllegalAccessException, IOException, InvocationTargetException {
T res=MAPPER.readValue(response.getSourceAsString(), valueType);
setId(res, valueType, response.getId());
return res;
}
public static String getId(Object obj) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException{
for(Field field : obj.getClass().getDeclaredFields()) {
if(field.isAnnotationPresent(EsId.class)){
return BeanUtils.getProperty(obj, field.getName());
}
}
return null;
}
public static String getType(Object obj){
return getType(obj.getClass());
}
public static String getType(Class<?> clazz){
return clazz.getAnnotation(EsType.class).value();
}
private static List<Field> getAllFields(Class<?> type) {
List<Field> fields = new ArrayList<Field>();
for (Class<?> c = type; c != null; c = c.getSuperclass()) {
fields.addAll(Arrays.asList(c.getDeclaredFields()));
}
return fields;
}
private static void setId(Object res, Class<?> valueType, String value) throws IllegalAccessException, InvocationTargetException{
for(Field field : getAllFields(valueType)){
if(field.isAnnotationPresent(EsId.class)){
BeanUtils.setProperty(res, field.getName(), value);
break;
}
}
}
}
import java.io.Serializable;
import java.util.Set;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
@EsType("user")
public class User extends Sequence implements Serializable{
private static final long serialVersionUID = 3353597507685172885L;
public enum Property{
NOT_CONFIRMED, FLAGGED
}
@EsId
@JsonIgnore
private String userId;
@JsonProperty
private String screenName;
@JsonProperty
private String password;
@JsonProperty
private String email;
@JsonProperty
private String avatarUrl;
@JsonProperty
private Integer avatarWidth;
@JsonProperty
private Integer avatarHeight;
@JsonProperty
private Set<Property> properties;
........ snippet getters and setters ............
....
public void store(Object obj) throws ElasticSearchException, IllegalArgumentException, IllegalAccessException, IOException, InvocationTargetException, NoSuchMethodException {
String type=EsUtil.getType(obj);
String id=EsUtil.getId(obj);
if(type!=null && id!=null){
clientProvider.getClient().prepareIndex(EsClientProvider.INDEX_ILF, type, id)
.setSource(EsUtil.getSource(obj)).execute().actionGet();
}else{
throw new IllegalArgumentException("The object doesn't have annotations or ID is null");
}
}
public User findUserByEmailAndPassword(String screenName, String encoddedPassword) throws JsonParseException, JsonMappingException, IllegalArgumentException, IllegalAccessException, ElasticSearchException, IOException, InvocationTargetException{
return EsUtil.getObject(clientProvider.getClient().prepareSearch("IndexName").setTypes(EsUtil.getType(User.class))
.setQuery(
QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),
FilterBuilders.andFilter(
FilterBuilders.termFilter("email", screenName.trim().toLowerCase()),
FilterBuilders.termFilter("password", encoddedPassword)
)
)
)
.execute().actionGet(), User.class);
}
.....
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment