Table of Contents |
---|
Goals
User can specified fields in nested object to be indexed.
Query on these nested fields as well as top level fields.
...
- Item in collection will not be index and searched.
API
- Use fieldnameAtLevel1.fieldnameAtLevel2 to specify a field in nested object both for indexing and querying.
For example, a Customer object contains a Person field. A Person object contains a Page field.
Code Block |
---|
public class Customer implements Serializable { private String name; private String symbol; // search integer in string format private int revenue; private int SSN; // search int 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; final String desc = "At client and server JVM, initializing cache will create the LuceneServiceImpl object," +" which is a singleton at each JVM."; ...... } |
Example to index on nested fields: in following example , a demos how to index on nested field such as contact.homepage.title, or contact.email, each segment is a field name, not the type name. This will tell the system to find the parent and grandparent . Itfield, because it's possible that several fields will have the same type. Such as For example, Customer class could have 2 Person fields: Person contact and Person deliveryman. The email field to be indexed is from contact field, not deliveryman field.
Code Block |
---|
// Get LuceneService LuceneService luceneService = LuceneServiceProvider.get(cache); // Create Index on fields, some are fields in nested objects: luceneService.createIndexFactory().addField("name").addField("symbol").addField("revenue").addField("SSN") .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"); |
2. When querying, the syntax of nested field is the same as toplevel field, i.e. without parent field's name. For example: "email:tzhou12*".
In order to
Code Block |
---|
// Create Query with nested objects final StandardQueryParser queryParser = new StandardQueryParser(new KeywordAnalyzer()); LuceneQuery query = luceneService.createLuceneQueryFactory().setLimit(200).setPageSize(20) .create(indexName, regionName, querystring, "field1" /* default field */); // Search using Query PageableLuceneQueryResults<K,Object> results = query.findPages(); // Pagination while (results.hasNext()) { results.next().stream().forEach(struct -> { Object value = struct.getValue(); System.out.println("Key is "+struct.getKey()+", value is "+value); }); } |