Search in sources :

Example 46 with Document

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);
    }
}
Also used : Document(com.rbmhtechnology.vind.api.Document) Field(java.lang.reflect.Field) ComplexField(com.rbmhtechnology.vind.annotations.ComplexField)

Example 47 with Document

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);
}
Also used : SearchResult(com.rbmhtechnology.vind.api.result.SearchResult) Document(com.rbmhtechnology.vind.api.Document) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrInputDocument(org.apache.solr.common.SolrInputDocument) ZonedDateTime(java.time.ZonedDateTime) Test(org.junit.Test)

Example 48 with Document

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");
}
Also used : Document(com.rbmhtechnology.vind.api.Document) Test(org.junit.Test)

Example 49 with Document

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);
}
Also used : ArrayList(java.util.ArrayList) Document(com.rbmhtechnology.vind.api.Document) Test(org.junit.Test)

Example 50 with Document

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);
}
Also used : Document(com.rbmhtechnology.vind.api.Document) Test(org.junit.Test)

Aggregations

Document (com.rbmhtechnology.vind.api.Document)50 Test (org.junit.Test)40 SolrInputDocument (org.apache.solr.common.SolrInputDocument)36 SearchServer (com.rbmhtechnology.vind.api.SearchServer)33 SearchResult (com.rbmhtechnology.vind.api.result.SearchResult)27 FulltextSearch (com.rbmhtechnology.vind.api.query.FulltextSearch)23 ZonedDateTime (java.time.ZonedDateTime)20 Interval (com.rbmhtechnology.vind.api.query.facet.Interval)13 Delete (com.rbmhtechnology.vind.api.query.delete.Delete)12 MultiValueFieldDescriptor (com.rbmhtechnology.vind.model.MultiValueFieldDescriptor)12 PageResult (com.rbmhtechnology.vind.api.result.PageResult)11 LatLng (com.rbmhtechnology.vind.model.value.LatLng)11 IOException (java.io.IOException)11 SolrQuery (org.apache.solr.client.solrj.SolrQuery)11 SolrServerException (org.apache.solr.client.solrj.SolrServerException)11 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)11 SuggestionResult (com.rbmhtechnology.vind.api.result.SuggestionResult)10 SearchConfiguration (com.rbmhtechnology.vind.configure.SearchConfiguration)10 java.util (java.util)10 SolrClient (org.apache.solr.client.solrj.SolrClient)10