/** * Recursively serialize each indexed field's value into a field of lucene document. The field * name will be in the same format as its indexed, such as contacts.homepage.content * * @param index lucene index * @param value user object to be serialized into index */ @Override public Collection<Document> toDocuments(LuceneIndex index, Object value) { String[] fields = index.getFieldNames(); Document doc = new Document(); for (String indexedFieldName : fields) { List<String> tokenizedFields = tokenizeField(indexedFieldName); addFieldValue(doc, indexedFieldName, value, tokenizedFields); } if (logger.isDebugEnabled()) { logger.debug("FlatFormatSerializer.toDocuments: " + doc); } return Collections.singleton(doc); }
private void addFieldValue(Document doc, String indexedFieldName, Object value, List<String> tokenizedFields) { String currentLevelField = tokenizedFields.get(0); Object fieldValue = getFieldValue(value, currentLevelField); if (fieldValue == null) { return; } if (fieldValue.getClass().isArray()) { for (int i = 0; i < Array.getLength(fieldValue); i++) { Object item = Array.get(fieldValue, i); addFieldValueForNonCollectionObject(doc, indexedFieldName, item, tokenizedFields); } } else if (fieldValue instanceof Collection) { Collection collection = (Collection) fieldValue; for (Object item : collection) { addFieldValueForNonCollectionObject(doc, indexedFieldName, item, tokenizedFields); } } else { addFieldValueForNonCollectionObject(doc, indexedFieldName, fieldValue, tokenizedFields); } }
@Test public void shouldParseSecondTopLevelPdxStringField() { String[] fields = new String[] {"positions.secId"}; FlatFormatSerializer serializer = new FlatFormatSerializer(); PdxInstance pdx = createPdxInstance(); Document doc1 = SerializerTestHelper.invokeSerializer(serializer, pdx, fields); IndexableField[] fieldsInDoc = doc1.getFields("positions.secId"); Collection<Object> results = getResultCollection(fieldsInDoc, false); assertEquals(2, results.size()); assertTrue(results.contains("IBM")); assertTrue(results.contains("AAPL")); }
private void addFieldValueForNonCollectionObject(Document doc, String indexedFieldName, Object fieldValue, List<String> tokenizedFields) { if (tokenizedFields.size() == 1) { SerializerUtil.addField(doc, indexedFieldName, fieldValue); } else { addFieldValue(doc, indexedFieldName, fieldValue, tokenizedFields.subList(1, tokenizedFields.size())); } }
@Test public void shouldParseSecondTopLevelPdxDoubleField() { String[] fields = new String[] {"positions.sharesOutstanding"}; FlatFormatSerializer serializer = new FlatFormatSerializer(); PdxInstance pdx = createPdxInstance(); Document doc1 = SerializerTestHelper.invokeSerializer(serializer, pdx, fields); IndexableField[] fieldsInDoc = doc1.getFields("positions.sharesOutstanding"); Collection<Object> results = getResultCollection(fieldsInDoc, true); assertEquals(2, results.size()); assertTrue(results.contains(5000.0)); assertTrue(results.contains(4000.0)); }
@Test public void shouldParseRegionValueFieldForString() { String[] fields = new String[] {"name", "contacts.name", "contacts.email", "contacts.revenue", "contacts.address", "contacts.homepage.id", "contacts.homepage.title", "contacts.homepage.content", LuceneService.REGION_VALUE_FIELD}; FlatFormatSerializer serializer = new FlatFormatSerializer(); String str = new String("Hello world"); Document doc1 = SerializerTestHelper.invokeSerializer(serializer, str, fields); assertEquals(1, doc1.getFields().size()); assertEquals("Hello world", doc1.getField(LuceneService.REGION_VALUE_FIELD).stringValue()); }
@Test public void shouldParseTopLevelPdxStringField() { String[] fields = new String[] {"status"}; FlatFormatSerializer serializer = new FlatFormatSerializer(); PdxInstance pdx = createPdxInstance(); Document doc1 = SerializerTestHelper.invokeSerializer(serializer, pdx, fields); IndexableField[] fieldsInDoc = doc1.getFields("status"); Collection<Object> results = getResultCollection(fieldsInDoc, false); assertEquals(1, results.size()); assertTrue(results.contains("active")); }
@Test public void shouldParseTopLevelPdxIntArray() { String[] fields = new String[] {"description", "status", "names", "intArr", "position1.country", "position1.sharesOutstanding", "position1.secId", "positions.country", "positions.sharesOutstanding", "positions.secId"}; FlatFormatSerializer serializer = new FlatFormatSerializer(); PdxInstance pdx = createPdxInstance(); Document doc1 = SerializerTestHelper.invokeSerializer(serializer, pdx, fields); assertEquals(17, doc1.getFields().size()); IndexableField[] fieldsInDoc = doc1.getFields("intArr"); Collection<Object> results = getResultCollection(fieldsInDoc, true); assertEquals(2, results.size()); assertTrue(results.contains(2001)); assertTrue(results.contains(2017)); }
@Test public void shouldQueryOnIntFieldInCollectionObject() { String[] fields = new String[] {"contacts.revenue"}; FlatFormatSerializer serializer = new FlatFormatSerializer(); HashSet<Person> contacts1 = createCollectionObjectContacts(); Customer customer = new Customer("Tommy Jackson", null, contacts1, null); Document doc1 = SerializerTestHelper.invokeSerializer(serializer, customer, fields); IndexableField[] fieldsInDoc = doc1.getFields("contacts.revenue"); Collection<Object> intResults = getResultCollection(fieldsInDoc, true); assertEquals(2, intResults.size()); assertTrue(intResults.contains(100)); assertTrue(intResults.contains(200)); }
@Test public void shouldQueryOnFieldInThirdLevelObject() { String[] fields = new String[] {"contacts.homepage.title"}; FlatFormatSerializer serializer = new FlatFormatSerializer(); HashSet<Person> contacts1 = createCollectionObjectContacts(); Customer customer = new Customer("Tommy Jackson", null, contacts1, null); Document doc1 = SerializerTestHelper.invokeSerializer(serializer, customer, fields); IndexableField[] fieldsInDoc = doc1.getFields("contacts.homepage.title"); Collection<Object> results = getResultCollection(fieldsInDoc, false); assertEquals(2, results.size()); assertTrue(results.contains("developer")); assertTrue(results.contains("manager")); }
@Test public void shouldNotParseNestedObjectWithoutFields() { String[] fields = new String[] {"name", "contacts", "contacts.homepage", "contacts.missing", "missing2"}; FlatFormatSerializer serializer = new FlatFormatSerializer(); HashSet<Person> contacts1 = new HashSet(); Person contact1 = new Person("Tommi Jackson", new String[] {"5036330001", "5036330002"}, 1); Person contact2 = new Person("Tommi2 Skywalker", new String[] {"5036330003", "5036330004"}, 2); contacts1.add(contact1); contacts1.add(contact2); ArrayList<String> phoneNumbers = new ArrayList(); phoneNumbers.add("5035330001"); phoneNumbers.add("5035330002"); Page[] myHomePages1 = new Page[] {new Page(131), new Page(132)}; Customer customer = new Customer("Tommy Jackson", phoneNumbers, contacts1, myHomePages1); Document doc1 = SerializerTestHelper.invokeSerializer(serializer, customer, fields); assertEquals(1, doc1.getFields().size()); assertEquals("Tommy Jackson", doc1.getField("name").stringValue()); }
@Test public void shouldQueryOnFieldInCollectionObject() { String[] fields = new String[] {"contacts.name"}; FlatFormatSerializer serializer = new FlatFormatSerializer(); HashSet<Person> contacts1 = createCollectionObjectContacts(); Customer customer = new Customer("Tommy Jackson", null, contacts1, null); Document doc1 = SerializerTestHelper.invokeSerializer(serializer, customer, fields); IndexableField[] fieldsInDoc = doc1.getFields("contacts.name"); Collection<Object> results = getResultCollection(fieldsInDoc, false); assertEquals(2, results.size()); assertTrue(results.contains("Tommi Jackson")); assertTrue(results.contains("Tommi2 Skywalker")); }
@Test public void shouldQueryOnFieldWithAnalyzerInCollectionObject() { String[] fields = new String[] {"contacts.email"}; FlatFormatSerializer serializer = new FlatFormatSerializer(); HashSet<Person> contacts1 = createCollectionObjectContacts(); Customer customer = new Customer("Tommy Jackson", null, contacts1, null); Document doc1 = SerializerTestHelper.invokeSerializer(serializer, customer, fields); IndexableField[] fieldsInDoc = doc1.getFields("contacts.email"); Collection<Object> results = getResultCollection(fieldsInDoc, false); assertEquals(2, results.size()); assertTrue(results.contains("Tommi.Jackson@pivotal.io")); assertTrue(results.contains("Tommi2.Skywalker@pivotal.io")); }
@Test public void verifyFieldCountsInDocument() { String[] fields = new String[] {"name", "phoneNumbers", "myHomePages.content", "contacts.name", "contacts.email", "contacts.phoneNumbers", "contacts.address", "contacts.revenue", "contacts.homepage.id", "contacts.homepage.title", "contacts.homepage.content", LuceneService.REGION_VALUE_FIELD}; FlatFormatSerializer serializer = new FlatFormatSerializer(); Person contact1 = new Person("Tommi Jackson", new String[] {"5036330001", "5036330002"}, 1); Person contact2 = new Person("Tommi2 Skywalker", new String[] {"5036330003", "5036330004"}, 2); HashSet<Person> contacts1 = new HashSet(); contacts1.add(contact1); contacts1.add(contact2); ArrayList<String> phoneNumbers = new ArrayList(); phoneNumbers.add("5035330001"); phoneNumbers.add("5035330002"); Page[] myHomePages1 = new Page[] {new Page(131), new Page(132)}; Customer customer = new Customer("Tommy Jackson", phoneNumbers, contacts1, myHomePages1); Document doc1 = SerializerTestHelper.invokeSerializer(serializer, customer, fields); assertEquals(23, doc1.getFields().size()); assertEquals("Tommy Jackson", doc1.getField("name").stringValue()); }
@Test public void shouldQueryOnFieldInArrayObject() { String[] fields = new String[] {"myHomePages.content"}; FlatFormatSerializer serializer = new FlatFormatSerializer(); Page[] myHomePages1 = new Page[] {new Page(131), new Page(132)}; Customer customer = new Customer("Tommy Jackson", null, null, myHomePages1); Document doc1 = SerializerTestHelper.invokeSerializer(serializer, customer, fields); IndexableField[] fieldsInDoc = doc1.getFields("myHomePages.content"); Collection<Object> results = getResultCollection(fieldsInDoc, false); assertEquals(2, results.size()); assertTrue(results.contains("Hello world no 131")); assertTrue(results.contains("Hello world no 132")); }
@Test public void shouldParseRegionValueFieldForInteger() { String[] fields = new String[] {"name", "contacts.name", "contacts.email", "contacts.revenue", "contacts.address", "contacts.homepage.id", "contacts.homepage.title", "contacts.homepage.content", LuceneService.REGION_VALUE_FIELD}; FlatFormatSerializer serializer = new FlatFormatSerializer(); Integer integer = 15; Document doc1 = SerializerTestHelper.invokeSerializer(serializer, integer, fields); assertEquals(1, doc1.getFields().size()); assertEquals(15, doc1.getField(LuceneService.REGION_VALUE_FIELD).numericValue()); }