/** * This method will always add these fields to the solr index: * * <ul> * <li>id * <li>tags * <li>type * </ul> * * It will also try to add the keywords list and name properties if they exist. * * @param resource * @param additionalFields * @throws Exception */ public void add(final AbstractEntity resource, final Map<String, Object> additionalFields) { addField("id", resource.getId()); addField("tags", resource.getTags()); addTypeField(resource); maybeAddNamedProperty("keywords", resource); maybeAddNamedProperty("name", resource); if (additionalFields != null) { for (Map.Entry<String, Object> entry : additionalFields.entrySet()) { addField(entry.getKey(), entry.getValue()); } } }
private void addField(String name, Object value) { if (value == null) { return; } if (value instanceof LocalDate) { // TODO: is this correct? Shouldnt we use LocalTime here? DateTime time = ((LocalDate) value).toDateTimeAtStartOfDay(); addDateTimeInUTC(name, time); } else if (value instanceof Interval) { Interval interval = (Interval) value; DateTime start = interval.getStart(); DateTime end = interval.getEnd(); addDateTimeInUTC(name + "_start", start); addDateTimeInUTC(name + "_end", end); } else if (value instanceof ValueObject) { ValueObject object = (ValueObject) value; inputDocument.addField(name, object.getIndexingValue()); } else if (value instanceof AbstractEntity) { AbstractEntity object = (AbstractEntity) value; inputDocument.addField(name, object.getId()); } else if (value instanceof Collection) { Collection list = (Collection) value; if (!list.isEmpty()) { for (Object object : list) { addField(name, object); } } } else { inputDocument.addField(name, value.toString()); } }
private void maybeAddNamedProperty(final String pName, AbstractEntity resource) { try { Object object = PropertyUtils.getProperty(resource, pName); addField(pName, object); } catch (Exception e) { // there was not field with that name. } }
private void addTypeField(AbstractEntity resource) { String type; if (resource instanceof Session) { type = ObjectType.session.name(); } else if (resource instanceof Person) { type = ObjectType.person.name(); } else if (resource instanceof Event) { type = ObjectType.event.name(); } else { throw new IllegalArgumentException("I cannot index this object type: " + resource.getClass()); } addField("type", type); }