use of com.rbmhtechnology.vind.api.Document in project vind by RBMHTechnology.
the class AnnotationUtil method createDocument.
/**
* Creates a new Document based on the given Object.
* @param pojo Object from which values an instance of Document will be created.
* @return Document based on the fields and values from the given pojo Object.
*/
public static Document createDocument(Object pojo) {
try {
final Class<?> pojoClass = pojo.getClass();
final Field idField = getIdField(pojoClass);
final String typeVal = getType(pojoClass);
final DocumentFactoryBuilder docFactoryBuilder = new DocumentFactoryBuilder(typeVal);
final Map<FieldDescriptor, Object> valueCache = new HashMap<>();
for (Field field : getFields(pojoClass)) {
final FieldDescriptor fd = createFieldDescriptor(field);
if (fd == null)
continue;
field.setAccessible(true);
final Object val = field.get(pojo);
docFactoryBuilder.addField(fd);
valueCache.put(fd, val);
}
final DocumentFactory docFactory = docFactoryBuilder.build();
final Id id = idField.getAnnotation(Id.class);
final String composedId = id.generator().newInstance().compose((String) idField.get(pojo), idField, pojoClass);
final Document doc = docFactory.createDoc(composedId);
// FIXME: Does this work as expected with collections?
valueCache.forEach((key, val) -> {
if (key.isMultiValue())
doc.setValues(key.getName(), (Collection) val);
else
doc.setValue(key.getName(), val);
});
return doc;
} catch (InstantiationException | IllegalAccessException e) {
log.error("Unable to create Document from pojo", e);
throw new RuntimeException("Unable to create Document from pojo", e);
}
}
use of com.rbmhtechnology.vind.api.Document in project vind by RBMHTechnology.
the class SolrSearchServerTest method testIndex.
@Test
public void testIndex() throws Exception {
FieldDescriptor<String> title = new FieldDescriptorBuilder<>().setFullText(true).buildTextField("title");
SingleValueFieldDescriptor.DateFieldDescriptor<ZonedDateTime> created = new FieldDescriptorBuilder<>().setFacet(true).buildDateField("created");
MultiValueFieldDescriptor.NumericFieldDescriptor<Integer> category = new FieldDescriptorBuilder<>().setFacet(true).buildMultivaluedNumericField("category", Integer.class);
final DocumentFactoryBuilder docFactoryBuilder = new DocumentFactoryBuilder("asset");
DocumentFactory documents = docFactoryBuilder.addField(title).addField(created).addField(category).build();
final ZonedDateTime creationDate = ZonedDateTime.of(2016, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault());
Document d1 = documents.createDoc("1").setValue(title, "Hello World").setValue(created, creationDate).setValues(category, Arrays.asList(1, 2));
Document d2 = documents.createDoc("2").setValue(title, "Hello Austria").setValue(created, creationDate).setValue(category, 4);
server.index(d1);
ArgumentCaptor<SolrInputDocument> argument = ArgumentCaptor.forClass(SolrInputDocument.class);
verify(solrClient).add(argument.capture());
SolrInputDocument doc = argument.getValue();
assertThat(doc.get(SolrUtils.Fieldname.ID), solrInputField(SolrUtils.Fieldname.ID, "1"));
assertThat(doc.get(SolrUtils.Fieldname.TYPE), solrInputField(SolrUtils.Fieldname.TYPE, "asset"));
assertThat(doc.get("dynamic_multi_int_category"), solrInputField("dynamic_multi_int_category", Matchers.containsInAnyOrder(1, 2)));
assertThat(doc.get("dynamic_single_string_title"), solrInputField("dynamic_single_string_title", "Hello World"));
assertThat(doc.get("dynamic_single_date_created"), solrInputField("dynamic_single_date_created", Date.from(creationDate.toInstant())));
server.commit();
SearchResult result = server.execute(Search.fulltext("hello").filter(or(category.between(3, 5), created.before(ZonedDateTime.now()))), documents);
}
use of com.rbmhtechnology.vind.api.Document in project vind by RBMHTechnology.
the class DocumentFactoryTest method getValueTest.
@Test
public void getValueTest() {
Document doc = factory.createDoc("idTest");
doc.setValues("multipleStringField", "1", "2", "3");
doc.setValue("singleStringField", "4");
Assert.assertEquals("get single value", doc.getValue("singleStringField"), "4");
exception.expect(IllegalArgumentException.class);
doc.getValue("imaginaryField");
}
use of com.rbmhtechnology.vind.api.Document in project vind by RBMHTechnology.
the class DocumentFactoryTest method setValuesTest.
@Test
public void setValuesTest() {
Document doc = factory.createDoc("idTest");
doc.setValues("multipleStringField", "1", "2", "3");
Collection<Object> values = new ArrayList<>();
values.add("4");
values.add("5");
doc.setValues("multipleStringField", values);
Collection<Object> invalidValues = new ArrayList<>();
invalidValues.add("6");
invalidValues.add(7);
exception.expect(IllegalArgumentException.class);
doc.setValues("multipleStringField", invalidValues);
doc.setValues("multipleStringField", 1);
}
use of com.rbmhtechnology.vind.api.Document in project vind by RBMHTechnology.
the class DocumentFactoryTest method removeValueTest.
@Test
public void removeValueTest() {
Document doc = factory.createDoc("idTest");
doc.setValues("multipleStringField", "1", "2", "3");
doc.removeValue("multipleStringField", "1");
exception.expect(IllegalArgumentException.class);
doc.removeValue("multipleStringField", 1);
}
Aggregations