Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
public class Customer implements Serializable {
  private String name;
  private Person contact; // search nested object 
  ......
}
public class Person implements Serializable {
  private String name;
  private String email;
  private int revenue;
  private String address;
  private Page homepage;
  .......
}
public class Page implements Serializable {
  private int id; // search integer in int format
  private String title;
  private String content;
  ......
}

 

Example to index on nested fields: following Following example demos how to index on nested field such as fields: contact.homepagename, contact.titleemail, or contact.email, address, contact.homepage.title. Note: each segment is a field name, not the type name. This will tell the system to find the parent and grandparent field ,  because it's possible that several fields will have the same type. For example, because Customer class could have 2 Person fields: Person contact and Person deliveryman. The email Use field to be indexed is from contact field, not deliveryman field.name is to identify the parent field.

 

Code Block
// Get LuceneService
LuceneService luceneService = LuceneServiceProvider.get(cache);

// Create Index on fields, some are fields in nested objects:
luceneService.createIndexFactory().setLuceneSerializer(new FlatformatSeralizer()) /* an out-of-box LuceneSerializer implementation */
      .addField("name").addField("contact.name").addField("contact.email").addField("contact.address").addField("contact.homepage.title")
      .create("customerIndex", "Customer");

// Now to create region
Region CustomerRegion = ((Cache)cache).createRegionFactory(shortcut).create("Customer");


In query, the syntax for the nested field is the same as top level field, but to use qualified name as the field name, such as "customercontact.email:2name:tzhou11*", otherwise, it's hard to distinguish which "name" field, because in flatformat, all the fields (including nested fields) are in one document. There're 2 "name" field. We have to specify querying on which "name" field

Code Block
LuceneQuery query = luceneService.createLuceneQueryFactory().create("customerIndex", "Customer", "contact.emailname:tzhou11*", "name");
 
PageableLuceneQueryResults<K,Object> results = query.findPages();

...

For example, the FlatformatSerializer will convert a Customer object into a document as

(name:John11), (symbol:123), (revenue:121939), (SSN:1233227171),(contact.name:tzhou11), (contact.email:tzhou11@gmail.com), (contact.address:15220 Wall St), (contact.homepage.id:11), (contact.homepage.title: Mr. tzhou11), (contact.homepage.content: xxx)

...