moremore0812 / cqengine

Automatically exported from code.google.com/p/cqengine
0 stars 0 forks source link

Support wildcard queries #15

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Wildcard support is being added to concurrent-trees: 
http://code.google.com/p/concurrent-trees/issues/detail?id=3

CQEngine should have an explicit way to perform queries with wildcards.

For reference, the following is a workaround to perform waldcard queries in 
CQEngine <= 1.0.3:

import static com.googlecode.cqengine.query.QueryFactory.*;
public class Wildcard {

   public static void main(String[] args) {
       IndexedCollection<String> collection = CQEngine.copyFrom(Arrays.asList("TEAM", "TEST", "TOAST", "T", "TT"));
       collection.addIndex(RadixTreeIndex.onAttribute(SELF));
       collection.addIndex(ReversedRadixTreeIndex.onAttribute(SELF));

       for (String match : retrieveWildcardMatches(collection, "T", "T")) { // ...wildcard query "T*T"
           System.out.println(match); // TOAST, TEST, TT
       }
   }

   public static ResultSet<String> retrieveWildcardMatches(IndexedCollection<String> collection, final String prefix, final String suffix) {
       ResultSet<String> candidates = collection.retrieve(and(startsWith(SELF, prefix), endsWith(SELF, suffix)));
       // Post-filter...
       return new FilteringResultSet<String>(candidates) {
           @Override
           public boolean isValid(String candidate) {
               return candidate.length() >= prefix.length() + suffix.length();
           }
       };
   }

   static Attribute<String, String> SELF = new SimpleAttribute<String, String>() {
       public String getValue(String object) { return object; }
   };
}

Original issue reported on code.google.com by ni...@npgall.com on 24 Mar 2013 at 11:40