...
Code Block |
---|
public class Customer implements Serializable { private String name; private PersonCollection<String> contact; // search nested object phoneNumbers; private Collection<Person> contacts; private Page[] myHomePages; ...... } public class Person implements Serializable { private String name; private String email; private int revenue; private String address; private String[] phoneNumbers; private Page homepage; ....... } public class Page implements Serializable { private int id; // search integer in int format private String title; private String content; ...... } |
...
The following example demonstrates how to index nested fields: contactcontacts.name, contactcontacts.email, contactcontacts.address, contactcontacts.homepage.title.
Note: each segment is a field name, not a field type, because Customer class could have more than one field of type Person; e.g. Person contact contacts and Person deliveryman. The field name is used 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("contactcontacts.name").addField("contactcontacts.email").addField("contactcontacts.address").addField("contactcontacts.homepage.title") .create("customerIndex", "Customer"); // Now to create region Region CustomerRegion = ((Cache)cache).createRegionFactory(shortcut).create("Customer"); |
The syntax for querying the nested field is the same as for a top level field, but with the additional qualifying parent field name, such as "contactcontacts.name:tzhou11*". This distinguishes which "name" field when there can potentially be more than one 'name' field at different hierarchical levels in the object.
Code Block |
---|
LuceneQuery query = luceneService.createLuceneQueryFactory().create("customerIndex", "Customer", "contactcontacts.name:tzhou11*", "name"); PageableLuceneQueryResults<K,Object> results = query.findPages(); |
...
For example, the FlatFormatSerializer will convert a Customer object into a document as
(name:John11),(contactcontacts.name:tzhou11), (contactcontacts.email:tzhou11@gmail.com), (contactcontacts.address:15220 Wall St), (contactcontacts.homepage.id:11), (contactcontacts.homepage.title: Mr. tzhou11), (contactcontacts.homepage.content: xxx)
Risks and Mitigations
...