Search in sources :

Example 1 with DocumentFactory

use of com.rbmhtechnology.vind.model.DocumentFactory in project vind by RBMHTechnology.

the class RESTMetadataProvider method getObject.

@Override
public <T> T getObject(T o) throws IOException {
    DocumentFactory factory = AnnotationUtil.createDocumentFactory(o.getClass());
    Document doc = AnnotationUtil.createDocument(o);
    return AnnotationUtil.createPojo(getDocumentById(doc.getId(), factory), (Class<T>) o.getClass());
}
Also used : DocumentFactory(com.rbmhtechnology.vind.model.DocumentFactory) Document(com.rbmhtechnology.vind.api.Document)

Example 2 with DocumentFactory

use of com.rbmhtechnology.vind.model.DocumentFactory in project vind by RBMHTechnology.

the class RESTMetadataProviderTest method testGetDocument2.

@Test
// ignored because of the username, password
@Ignore
public void testGetDocument2() throws IOException {
    MetadataProvider metadataProvider = new RESTMetadataProvider("https://mediamanager-staging.redbullmediahouse.com", "rbmh", "admin", "global", "1315204862832-1123067022", "asset", "user", "passw");
    SingleValueFieldDescriptor.TextFieldDescriptor<String> title = new FieldDescriptorBuilder().setFullText(true).setFacet(// notwendig für suggestion, da wirds noch ein setSuggestion(bool) geben
    true).putMetadata(RESTMetadataProvider.ID, "1319102420792-686346531").buildTextField("title");
    SingleValueFieldDescriptor.TextFieldDescriptor<String> description = new FieldDescriptorBuilder().setFullText(true).setFacet(// notwendig für suggestion, da wirds noch ein setSuggestion(bool) geben
    true).putMetadata(RESTMetadataProvider.ID, "1315204278582-8008411").buildTextField("description");
    DocumentFactory factory = new DocumentFactoryBuilder("asset").addField(title, description).build();
    Document document = factory.createDoc("1359078847993-766700833");
    document = metadataProvider.getDocument(document, factory);
    Assert.assertEquals("Sean Pettit - Portrait", document.getValue(title));
}
Also used : DocumentFactory(com.rbmhtechnology.vind.model.DocumentFactory) SingleValueFieldDescriptor(com.rbmhtechnology.vind.model.SingleValueFieldDescriptor) Document(com.rbmhtechnology.vind.api.Document) FieldDescriptorBuilder(com.rbmhtechnology.vind.model.FieldDescriptorBuilder) DocumentFactoryBuilder(com.rbmhtechnology.vind.model.DocumentFactoryBuilder) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with DocumentFactory

use of com.rbmhtechnology.vind.model.DocumentFactory 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 4 with DocumentFactory

use of com.rbmhtechnology.vind.model.DocumentFactory in project vind by RBMHTechnology.

the class SolrSearchServer method execute.

@Override
public <T> BeanGetResult<T> execute(RealTimeGet search, Class<T> c) {
    DocumentFactory documentFactory = AnnotationUtil.createDocumentFactory(c);
    final GetResult result = this.execute(search, documentFactory);
    return result.toPojoResult(result, c);
}
Also used : DocumentFactory(com.rbmhtechnology.vind.model.DocumentFactory)

Example 5 with DocumentFactory

use of com.rbmhtechnology.vind.model.DocumentFactory in project vind by RBMHTechnology.

the class SolrSearchServer method execute.

@Override
public void execute(Delete delete, DocumentFactory factory) {
    String query = SolrUtils.Query.buildFilterString(delete.getQuery(), factory, delete.getUpdateContext(), true);
    try {
        solrClientLogger.debug(">>> delete query({})", query);
        // Finding the ID of the documents to delete
        final SolrQuery solrQuery = new SolrQuery();
        solrQuery.setParam(CommonParams.Q, "*:*");
        solrQuery.setParam(CommonParams.FQ, query.trim().replaceAll("^\\+", "").split("\\+"));
        final QueryResponse response = solrClient.query(solrQuery, SolrRequest.METHOD.POST);
        if (Objects.nonNull(response) && CollectionUtils.isNotEmpty(response.getResults())) {
            final List<String> idList = response.getResults().stream().map(doc -> (String) doc.get(ID)).collect(Collectors.toList());
            solrClient.deleteById(idList);
            // Deleting nested documents
            solrClient.deleteByQuery("_root_:(" + StringUtils.join(idList, " OR ") + ")");
        }
    } catch (SolrServerException | IOException e) {
        log.error("Cannot delete with query {}", query, e);
        throw new SearchServerException("Cannot delete with query", 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) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrServerException(org.apache.solr.client.solrj.SolrServerException) IOException(java.io.IOException) SearchServerException(com.rbmhtechnology.vind.SearchServerException) SolrQuery(org.apache.solr.client.solrj.SolrQuery)

Aggregations

DocumentFactory (com.rbmhtechnology.vind.model.DocumentFactory)15 SolrQuery (org.apache.solr.client.solrj.SolrQuery)8 Document (com.rbmhtechnology.vind.api.Document)7 SolrClient (org.apache.solr.client.solrj.SolrClient)6 Test (org.junit.Test)6 SearchServer (com.rbmhtechnology.vind.api.SearchServer)5 ExecutableSuggestionSearch (com.rbmhtechnology.vind.api.query.suggestion.ExecutableSuggestionSearch)5 FieldDescriptor (com.rbmhtechnology.vind.model.FieldDescriptor)5 Resources (com.google.common.io.Resources)4 SearchServerException (com.rbmhtechnology.vind.SearchServerException)4 AnnotationUtil (com.rbmhtechnology.vind.annotations.AnnotationUtil)4 ServiceProvider (com.rbmhtechnology.vind.api.ServiceProvider)4 com.rbmhtechnology.vind.api.query (com.rbmhtechnology.vind.api.query)4 Delete (com.rbmhtechnology.vind.api.query.delete.Delete)4 Page (com.rbmhtechnology.vind.api.query.division.Page)4 Slice (com.rbmhtechnology.vind.api.query.division.Slice)4 Facet (com.rbmhtechnology.vind.api.query.facet.Facet)4 Interval (com.rbmhtechnology.vind.api.query.facet.Interval)4 RealTimeGet (com.rbmhtechnology.vind.api.query.get.RealTimeGet)4 DescriptorSuggestionSearch (com.rbmhtechnology.vind.api.query.suggestion.DescriptorSuggestionSearch)4