Search in sources :

Example 21 with Document

use of com.rbmhtechnology.vind.api.Document in project vind by RBMHTechnology.

the class AnnotationUtilTest method testPojo2RoundTrip.

@Test
public void testPojo2RoundTrip() {
    Pojo2 p2 = new Pojo2();
    p2.id = UUID.randomUUID().toString();
    p2.title = "Title";
    p2.someInternalData = UUID.randomUUID().toString();
    p2.content = "Content";
    p2.categories = new HashSet<>(Arrays.asList("cat1", "cat2", "cat3"));
    p2.counter = 17;
    p2.tax = new Taxonomy("id-3", "term 3", Arrays.asList("term 3", "third term"));
    final Document doc = AnnotationUtil.createDocument(p2);
    assertThat("doc.id", doc.getId(), is(p2.id));
    assertThat("doc.type", doc.getType(), is(p2.getClass().getSimpleName()));
    assertThat("doc.field(title)", doc.getValue("title"), allOf(instanceOf(String.class), is(p2.title)));
    assertThat("doc.field(content)", doc.getValue("data"), allOf(instanceOf(String.class), is(p2.content)));
    assertThat("doc.field(counter)", doc.getValue("counter"), allOf(instanceOf(int.class), is(p2.counter)));
    assertThat("doc.filed(categories)", doc.getValue("cats"), instanceOf(Collection.class));
    final Pojo2 pojo = AnnotationUtil.createPojo(doc, Pojo2.class);
    assertThat("pojo.id", pojo.id, is(p2.id));
    assertThat("pojo.title", pojo.title, is(p2.title));
    assertThat("pojo.content", pojo.content, is(p2.content));
    assertThat("pojo.counter", pojo.counter, is(p2.counter));
    assertThat("pojo.categories", pojo.categories, hasSize(3));
    assertThat("pojo.categories", pojo.categories, CoreMatchers.<Collection<String>>allOf(hasSize(3), containsInAnyOrder(p2.categories.toArray())));
    assertThat("pojo.someInternalData", pojo.someInternalData, nullValue());
}
Also used : Document(com.rbmhtechnology.vind.api.Document) Test(org.junit.Test)

Example 22 with Document

use of com.rbmhtechnology.vind.api.Document in project vind by RBMHTechnology.

the class AnnotationUtilTest method testCreateDocument1.

@Test
public void testCreateDocument1() throws Exception {
    Pojo1 p = new Pojo1();
    p.id = "foo";
    p.title = "title";
    p.content = "content";
    p.someInternalData = UUID.randomUUID().toString();
    p.categories = new HashSet<>(Arrays.asList("cat1", "cat2", "cat3"));
    p.tax = new Taxonomy("id-1", "term 1", Arrays.asList("first Term", "term 1", "term one"));
    final Document doc = AnnotationUtil.createDocument(p);
    assertThat("id", doc.getId(), equalTo(p.id));
    assertThat("type", doc.getType(), equalTo("Pojo"));
    assertThat("title", doc.getValue("title"), allOf(instanceOf(String.class), CoreMatchers.<Object>equalTo(p.title)));
    assertThat("content", doc.getValue("data"), allOf(instanceOf(String.class), equalTo(p.content)));
    assertFalse("someInternalData", doc.hasValue("someInternalData"));
    assertThat("categories", doc.getValue("cats"), instanceOf(Collection.class));
}
Also used : Document(com.rbmhtechnology.vind.api.Document) Test(org.junit.Test)

Example 23 with Document

use of com.rbmhtechnology.vind.api.Document in project vind by RBMHTechnology.

the class SolrSearchServer method execute.

@Override
public boolean execute(Update update, DocumentFactory factory) {
    // Check if document is updatable and all its fields are stored.
    final boolean isUpdatable = factory.isUpdatable() && factory.getFields().values().stream().allMatch(descriptor -> descriptor.isUpdate());
    if (isUpdatable) {
        final SolrInputDocument sdoc = new SolrInputDocument();
        sdoc.addField(ID, update.getId());
        sdoc.addField(TYPE, factory.getType());
        HashMap<FieldDescriptor<?>, HashMap<String, SortedSet<UpdateOperation>>> updateOptions = update.getOptions();
        updateOptions.keySet().forEach(fieldDescriptor -> Stream.of(UseCase.values()).forEach(useCase -> updateOptions.get(fieldDescriptor).keySet().stream().forEach(context -> {
            // NOTE: Backwards compatibility
            final String updateContext = Objects.isNull(context) ? update.getUpdateContext() : context;
            final String fieldName = getFieldname(fieldDescriptor, useCase, updateContext);
            if (fieldName != null) {
                final Map<String, Object> fieldModifiers = new HashMap<>();
                updateOptions.get(fieldDescriptor).get(context).stream().forEach(entry -> {
                    UpdateOperations opType = entry.getType();
                    if (fieldName.startsWith("dynamic_single_") && useCase.equals(UseCase.Sort) && opType.equals(UpdateOperations.add)) {
                        opType = set;
                    }
                    fieldModifiers.put(opType.name(), toSolrJType(SolrUtils.FieldValue.getFieldCaseValue(entry.getValue(), fieldDescriptor, useCase)));
                });
                sdoc.addField(fieldName, fieldModifiers);
            }
        })));
        try {
            if (solrClientLogger.isTraceEnabled()) {
                solrClientLogger.debug(">>> add({}): {}", update.getId(), sdoc);
            } else {
                solrClientLogger.debug(">>> add({})", update.getId());
            }
            SolrInputDocument finalDoc = sdoc;
            // Get the original document
            final SolrDocument updatedDoc = solrClient.getById(update.getId());
            // Setting the document version for optimistic concurrency
            final Object version = updatedDoc.getFieldValue("_version_");
            if (Objects.nonNull(version)) {
                finalDoc.setField("_version_", version);
            } else {
                log.warn("Error updating document '{}': " + "Atomic updates in nested documents are not supported by Solr", updatedDoc.get(ID));
                return false;
            }
            // Get the nested docs of the document if existing
            final NamedList<Object> paramList = new NamedList<>();
            paramList.add(CommonParams.Q, "!( _id_:" + update.getId() + ")&(_root_:" + update.getId() + ")");
            final QueryResponse query = solrClient.query(SolrParams.toSolrParams(paramList), SolrRequest.METHOD.POST);
            // if the document has nested docs solr does not support atomic updates
            if (CollectionUtils.isNotEmpty(query.getResults())) {
                log.info("Update document `{}`: doc has {} nested documents, changing from partial update to full index.", finalDoc.getFieldValue(SolrUtils.Fieldname.ID), query.getResults().size());
                // Get the list of nested documents
                final List<SolrInputDocument> childDocs = query.getResults().stream().map(nestedDoc -> ClientUtils.toSolrInputDocument(nestedDoc)).collect(Collectors.toList());
                finalDoc = this.getUpdatedSolrDocument(sdoc, updatedDoc, childDocs);
            }
            try {
                log.info("Updating document `{}`: current version `{}`", finalDoc.getFieldValue(SolrUtils.Fieldname.ID), version);
                solrClient.add(finalDoc);
                return true;
            } catch (HttpSolrClient.RemoteSolrException e) {
                log.warn("Error updating document {}: {}", finalDoc.getFieldValue(ID), e.getMessage(), e);
                return false;
            }
        } catch (SolrServerException | IOException e) {
            log.error("Unable to perform solr partial update on document with id [{}]", update.getId(), e);
            throw new SearchServerException("Can not execute solr partial update.", e);
        }
    } else {
        Exception e = new SearchServerException("It is not safe to execute solr partial update: Document contains non stored fields");
        log.error("Unable to perform solr partial update on document with id [{}]", update.getId(), e);
        throw new RuntimeException("Can not execute solr partial update.", e);
    }
}
Also used : Delete(com.rbmhtechnology.vind.api.query.delete.Delete) SearchServerException(com.rbmhtechnology.vind.SearchServerException) URISyntaxException(java.net.URISyntaxException) ZonedDateTime(java.time.ZonedDateTime) LoggerFactory(org.slf4j.LoggerFactory) Asserts(org.apache.http.util.Asserts) StringUtils(org.apache.commons.lang3.StringUtils) SolrServerException(org.apache.solr.client.solrj.SolrServerException) Fieldname(com.rbmhtechnology.vind.solr.backend.SolrUtils.Fieldname) Facet(com.rbmhtechnology.vind.api.query.facet.Facet) UpdateOperation(com.rbmhtechnology.vind.api.query.update.UpdateOperation) ParseException(java.text.ParseException) FileSystemUtils(com.rbmhtechnology.vind.utils.FileSystemUtils) Path(java.nio.file.Path) SearchServer(com.rbmhtechnology.vind.api.SearchServer) Slice(com.rbmhtechnology.vind.api.query.division.Slice) AnnotationUtil(com.rbmhtechnology.vind.annotations.AnnotationUtil) SchemaResponse(org.apache.solr.client.solrj.response.schema.SchemaResponse) Update(com.rbmhtechnology.vind.api.query.update.Update) SchemaRequest(org.apache.solr.client.solrj.request.schema.SchemaRequest) DescriptorSuggestionSearch(com.rbmhtechnology.vind.api.query.suggestion.DescriptorSuggestionSearch) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) com.rbmhtechnology.vind.api.query(com.rbmhtechnology.vind.api.query) RealTimeGet(com.rbmhtechnology.vind.api.query.get.RealTimeGet) Stream(java.util.stream.Stream) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) SolrPingResponse(org.apache.solr.client.solrj.response.SolrPingResponse) SolrQuery(org.apache.solr.client.solrj.SolrQuery) com.rbmhtechnology.vind.api.result(com.rbmhtechnology.vind.api.result) ClientUtils(org.apache.solr.client.solrj.util.ClientUtils) ServiceProvider(com.rbmhtechnology.vind.api.ServiceProvider) SolrInputDocument(org.apache.solr.common.SolrInputDocument) Interval(com.rbmhtechnology.vind.api.query.facet.Interval) SolrRequest(org.apache.solr.client.solrj.SolrRequest) java.util(java.util) ExecutableSuggestionSearch(com.rbmhtechnology.vind.api.query.suggestion.ExecutableSuggestionSearch) FieldDescriptor(com.rbmhtechnology.vind.model.FieldDescriptor) NumberFormat(java.text.NumberFormat) UpdateOperations.set(com.rbmhtechnology.vind.api.query.update.Update.UpdateOperations.set) CollectionUtils(org.apache.commons.collections.CollectionUtils) SearchConfiguration(com.rbmhtechnology.vind.configure.SearchConfiguration) Logger(org.slf4j.Logger) StringSuggestionSearch(com.rbmhtechnology.vind.api.query.suggestion.StringSuggestionSearch) LatLng(com.rbmhtechnology.vind.model.value.LatLng) Resources(com.google.common.io.Resources) DateUtil(org.apache.solr.common.util.DateUtil) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) org.apache.solr.common.params(org.apache.solr.common.params) IOException(java.io.IOException) NamedList(org.apache.solr.common.util.NamedList) Document(com.rbmhtechnology.vind.api.Document) SolrClient(org.apache.solr.client.solrj.SolrClient) SolrDocument(org.apache.solr.common.SolrDocument) DocumentFactory(com.rbmhtechnology.vind.model.DocumentFactory) Page(com.rbmhtechnology.vind.api.query.division.Page) UpdateOperations(com.rbmhtechnology.vind.api.query.update.Update.UpdateOperations) NamedList(org.apache.solr.common.util.NamedList) SolrServerException(org.apache.solr.client.solrj.SolrServerException) IOException(java.io.IOException) SearchServerException(com.rbmhtechnology.vind.SearchServerException) URISyntaxException(java.net.URISyntaxException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) ParseException(java.text.ParseException) IOException(java.io.IOException) FieldDescriptor(com.rbmhtechnology.vind.model.FieldDescriptor) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrDocument(org.apache.solr.common.SolrDocument) UpdateOperations(com.rbmhtechnology.vind.api.query.update.Update.UpdateOperations) UpdateOperation(com.rbmhtechnology.vind.api.query.update.UpdateOperation) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SearchServerException(com.rbmhtechnology.vind.SearchServerException)

Example 24 with Document

use of com.rbmhtechnology.vind.api.Document in project vind by RBMHTechnology.

the class SolrSearchServer method index.

@Override
public void index(Document... docs) {
    Asserts.notNull(docs, "Document to index should not be null.");
    Asserts.check(docs.length > 0, "Should be at least one document to index.");
    for (Document doc : docs) {
        indexSingleDocument(doc);
    }
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) Document(com.rbmhtechnology.vind.api.Document) SolrDocument(org.apache.solr.common.SolrDocument)

Example 25 with Document

use of com.rbmhtechnology.vind.api.Document in project vind by RBMHTechnology.

the class SolrSearchServerTest method testExecute.

@Test
public void testExecute() throws Exception {
    Asset asset = new Asset();
    // FIXME: Asset has no @Id field
    server.indexBean(asset);
    // query
    BeanSearchResult<Asset> result = server.execute(Search.fulltext("hello world").filter(eq("category", "test")), Asset.class);
    // suggestion
    SuggestionResult suggestions = server.execute(Search.suggest("he").fields("category"), Asset.class);
    FieldDescriptor<String> title = new FieldDescriptorBuilder().setBoost(2).setLanguage(Language.German).buildTextField("title");
    // complex
    DocumentFactory factory = new DocumentFactoryBuilder("asset").addField(title).build();
    Document document = factory.createDoc("1234");
    server.index(document);
    // suggestion
    SuggestionResult suggestionsFromFactory = server.execute(Search.suggest("he").fields("title"), factory);
}
Also used : DocumentFactory(com.rbmhtechnology.vind.model.DocumentFactory) SuggestionResult(com.rbmhtechnology.vind.api.result.SuggestionResult) Asset(com.rbmhtechnology.vind.solr.backend.utils.Asset) Document(com.rbmhtechnology.vind.api.Document) FieldDescriptorBuilder(com.rbmhtechnology.vind.model.FieldDescriptorBuilder) DocumentFactoryBuilder(com.rbmhtechnology.vind.model.DocumentFactoryBuilder) 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