Skip to content
nathankopp edited this page Dec 15, 2010 · 1 revision
@D2Entity( alias="Person", bucketName="people")
public class Person
{
    protected D2Metadata md;
    
    @D2Id public String id;
    @D2Indexed public String firstName;
    @D2Indexed public String lastName;
    
    public Person(String firstName, String lastName)
    {
        super();
        this.firstName = firstName;
        this.lastName = lastName;
    }
}

=========================

public class Example1
{
    public static void main(String[] args)
    {
        StorageSystem storage = new LocalFileStorage("testdb");
        Indexer indexer = new LuceneIndexer("testdb");
        D2 d2 = new D2Impl(storage, indexer);
        d2.registerBucket(new D2Bucket(Person.class));
        
        D2Context context1 = new D2Context();
        D2Dao<Person> dao = new D2Dao<Person>(d2, Person.class, context1);
        
        dao.save(new Person("Nathan","Kopp"));
        dao.save(new Person("Lael","Watkins"));
        dao.save(new Person("John","Smith"));
        dao.save(new Person("Jane","Smith"));
        
        D2QB builder = new D2QB();
        builder.exact("firstName", "Nathan", Occurs.MUST);
        Person person = dao.loadOneForQuery(builder.getQuery());
        
        System.out.println("Found person: " + person.firstName + " " + person.lastName);
        
        List<Person> theSmiths = 
             dao.loadForQuery(D2QB.start().exact("lastName", "Smith").getQuery());
        
        for(Person p : theSmiths)
        {
            System.out.println("Found person: " + p.firstName + " " + p.lastName);
        }
        
        Util.deleteDirectory(new File("testdb"));
    }
}

=========================

@D2Entity( alias="Person", bucketName="people")
public class Person
{
    protected D2Metadata md;
    
    @D2Id public String id;
    @D2Indexed public String firstName;
    @D2Indexed(analyzed=true) public String lastName;
    
    public Person(String firstName, String lastName)
    {
        super();
        this.firstName = firstName;
        this.lastName = lastName;
    }
}

=========================

Now you can search case-insensitive on the last name:

        List<Person> theSmiths = 
             dao.loadForQuery(D2QB.start().exact("lastName", "sMiTh").getQuery());
        
        for(Person p : theSmiths)
        {
            System.out.println("Found person: " + p.firstName + " " + p.lastName);
        }

Clone this wiki locally