0

I want to ask if there is a better way to search in an ehcache. I've created a cache with key and list of values, the list is a arraylist of strings. Now I want to search wihtin the list of strings e.g. email adresses and return the whole java object.

key: ID12345678 value: List of emails [test@test.de, test1@test.de, ...]

Currently I do as following:

public static class EMailAttributeExtractor implements AttributeExtractor {

public Object attributeFor(Element element) {
     VzdData vzdData = ((VzdData) element.getObjectValue());
    return String.join(";", vzdData.getServiceData().getMail());
}

@Override
public Object attributeFor(Element element, String attributeName) throws AttributeExtractorException {
  return this.attributeFor(element);
    }
}

Searchable searchable = new Searchable();
searchable.addSearchAttribute(new SearchAttribute().name("email").className(EMailAttributeExtractor.class.getName()));
searchable.setAllowDynamicIndexing(true);
public VzdData getVzdDatabyEMail(String email) throws VZDException {
 log.entry(email);

     VzdData vzdData = null;
    
     Attribute<String> emailSearchAttribute = this.cache.getSearchAttribute("email");
    Query query = this.cache.createQuery().addCriteria(emailSearchAttribute.ilike("*" + email + "*"));
    query.includeValues();
    
    Results results = query.execute();
    List<Result> resultList = results.all();
            
    return log.exit(vzdData);
}

I wrote a test case, was looking into the source code, but don't find a better solution as to flatten the list to a comma separated string a do a like search.

1 Answers1

-1

As per description, I'm not quite sure what is your exact goal/issue, but maybe this is a way to search email in list of string values:

class Entity implements Serializable {
/**
 * Simple Entity class that can be searched on by the Ehcache Search API
 */
private static final long serialVersionUID = 1L;
private final String name;
private final String emails;

public Entity(String name, String emails) {
    this.name = name;
    this.emails = emails;
}

public String getName() {
    return name;
}

public String getEmails() {
    return emails;
}

@Override
public String toString() {
    return getClass().getSimpleName() + "(name:" + name + ", emails:" + emails + ")";
}
}    
public class EhcacheSearch {

public static void main(String[] args) {

    Configuration cacheManagerConfig = new Configuration();
    CacheConfiguration cacheConfig = new CacheConfiguration("test", 0)
            .eternal(true);
    Searchable searchable = new Searchable();
    cacheConfig.addSearchable(searchable);

    searchable.addSearchAttribute(new SearchAttribute().name("name")
            .className(NameAttributeExtractor.class.getName()));

    searchable.addSearchAttribute(new SearchAttribute().name("emails")
            .className(EmailAttributeExtractor.class.getName()));

    cacheManagerConfig.addCache(cacheConfig);

    CacheManager cacheManager = new CacheManager(cacheManagerConfig);
    Ehcache cache = cacheManager.getEhcache("test");

    cache.put(new Element(1, new Entity("Tom", new ArrayList<>(Arrays.asList("tom@gmail.com", "elam@gmail.com")).toString())));
    cache.put(new Element(2, new Entity("Jerry", new ArrayList<>(Arrays.asList("jerry@gmail.com", "reet@gmail.com")).toString())));

    Attribute<String> emailIds = cache.getSearchAttribute("emails");

    Query query = cache.createQuery();
    query.includeKeys();
    query.includeValues();
    query.addCriteria(emailIds.ilike("*reet@gmail.com*"));

    System.out.println("Searching records for reet@gmail.com email address");

    Results results = query.execute();
    System.out.println("Number of records found: " + results.size());
    System.out.println("----Records-----\n");
    for (Result result : results.all()) {
        System.out.println("Got: Key[" + result.getKey()
                + "] Value class [" + result.getValue().getClass()
                + "] Value [" + result.getValue() + "]");
    }
}

public static class NameAttributeExtractor implements AttributeExtractor {
    public Object attributeFor(Element element) {
        return ((Entity) element.getValue()).getName();
    }

    @Override
    public Object attributeFor(Element element, String arg1)
            throws AttributeExtractorException {
        return attributeFor(element);
    }
}

public static class EmailAttributeExtractor implements AttributeExtractor {
    public Object attributeFor(Element element) {
        return ((Entity) element.getValue()).getEmails();
    }

    @Override
    public Object attributeFor(Element element, String arg1)
            throws AttributeExtractorException {
        return attributeFor(element);
    }
}
}

Output:

Searching records for reet@gmail.com email address Number of records found: 1 ----Records-----

Got: Key[2] Value class [class org.example.Entity] Value [Entity(name:Jerry, emails:[jerry@gmail.com, reet@gmail.com])]